Skip to content

Commit

Permalink
feat: Inject $i18n into Vuex Store as this.$i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Sep 18, 2019
1 parent d91e89f commit bb31cb0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
22 changes: 20 additions & 2 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ In `no_prefix` strategy, passing `locale` other than the current one is not supp
## Extension of VueI18n

::: tip
Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class) (see its [properties and methods](http://kazupon.github.io/vue-i18n/api/#properties)) is exposed as `$i18n` on Vue instance and as `i18n` on Nuxt `context.app`.
Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class) (see its [properties and methods](http://kazupon.github.io/vue-i18n/api/#properties)) is exposed as `$i18n` on Vue instance and Vuex Store but as `i18n` on Nuxt `context.app`.
:::

### Methods
Expand Down Expand Up @@ -137,7 +137,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class

### context.app.i18n

- **Type**: `VueI18n`
- **Type**: [`VueI18n`](#extension-of-vuei18n)

See also [Nuxt context](https://nuxtjs.org/api/context#the-context).

Expand All @@ -156,3 +156,21 @@ export default Vue.extend({
}
})
````

## Extension of Vuex

### $i18n

- **Type**: [`VueI18n`](#extension-of-vuei18n)

Can be accessed in store's actions and mutations as `this.$i18n`.
Example use:
```js
export const actions = {
nuxtServerInit({ commit }) {
commit('LOCALE', this.$i18n.locale)
}
}
````
4 changes: 4 additions & 0 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { getLocaleDomain, getLocaleFromRoute, syncVuex, validateRouteParams } fr

Vue.use(VueI18n)

/** @type {import('@nuxt/types').Plugin} */
export default async (context) => {
const { app, route, store, req, res, redirect } = context

Expand Down Expand Up @@ -188,6 +189,9 @@ export default async (context) => {
// Inject seo function
Vue.prototype.$nuxtI18nSeo = nuxtI18nSeo

// Inject in store.
store.$i18n = app.i18n

if (store && store.state.localeDomains) {
app.i18n.locales.forEach(locale => {
locale.domain = store.state.localeDomains[locale.code]
Expand Down
2 changes: 1 addition & 1 deletion types/nuxt-i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ declare namespace NuxtVueI18n {
seo?: boolean
strategy?: 'no_prefix' | 'prefix_except_default' | 'prefix' | 'prefix_and_default'
vueI18n?: VueI18n.I18nOptions
vuex?: VuexInterface
vuex?: VuexInterface | false
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import Vuex from 'vuex'
import * as types from '../index'

const vm = new Vue()
Expand Down Expand Up @@ -26,3 +27,9 @@ const routeBaseName: string = vm.getRouteBaseName(vm.$route)
const cookieLocale: string | undefined = vm.$i18n.getLocaleCookie()
vm.$i18n.setLocaleCookie(locale)
vm.$i18n.setLocale(locale)

// Store

const store = new Vuex.Store({})

store.$i18n.setLocale(locale)
6 changes: 6 additions & 0 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ declare module '@nuxt/types' {
readonly i18n: VueI18n & IVueI18n
}
}

declare module 'vuex/types/index' {
interface Store<S> {
readonly $i18n: VueI18n & IVueI18n
}
}

0 comments on commit bb31cb0

Please sign in to comment.