-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.js
45 lines (35 loc) · 1.15 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import axios from 'axios'
Vue.use(VueI18n)
function generateUrl(language) {
const path = '/i18n/<%= options.outputFilePrefix %>' + '-' + language + '.json'
let url = path
const host = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
const port = process.env.PORT || process.env.npm_package_config_nuxt_port || 3000
return 'http://' + host + ':' + port + url
}
export default async ({ app, store, beforeNuxtRender }) => {
const languages = '<%= options.languages %>'.split(',')
let languagesSrc = {}
// On server side
if (process.server) {
for (const language of languages) {
const { data } = await axios(generateUrl(language))
languagesSrc[language] = data
}
beforeNuxtRender(({ nuxtState }) => {
nuxtState.i18n = languagesSrc
})
}
// On client side
if (process.client) {
languagesSrc = window.__NUXT__.i18n
}
// Set i18n instance on app
app.i18n = new VueI18n({
locale: store.state['<%= options.localeNamespaceStore %>'].locale,
fallbackLocale: '<%= options.fallbackLocale %>',
messages: languagesSrc
})
}