Skip to content

Commit ffc4751

Browse files
committed
plugin: Get translate from XHR and not from require method
1 parent 1a6d1bd commit ffc4751

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

plugin.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
1-
const path = require('path')
2-
31
import Vue from 'vue'
42
import VueI18n from 'vue-i18n'
3+
import axios from 'axios'
54

65
Vue.use(VueI18n)
76

8-
export default ({ app, store }) => {
7+
function generateUrl(language) {
8+
const path = '/i18n/<%= options.outputFilePrefix %>' + '-' + language + '.json'
9+
10+
let url = path
11+
const host = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
12+
const port = process.env.PORT || process.env.npm_package_config_nuxt_port || 3000
13+
14+
return 'http://' + host + ':' + port + url
15+
}
16+
17+
export default async ({ app, store, beforeNuxtRender }) => {
918
const languages = '<%= options.languages %>'.split(',')
10-
const languagesSrc = {}
19+
let languagesSrc = {}
20+
21+
// On server side
22+
if (process.server) {
23+
for (const language of languages) {
24+
const { data } = await axios(generateUrl(language))
25+
26+
languagesSrc[language] = data
27+
}
28+
29+
beforeNuxtRender(({ nuxtState }) => {
30+
nuxtState.i18n = languagesSrc
31+
})
32+
}
33+
34+
// On client side
35+
if (process.client) {
36+
languagesSrc = window.__NUXT__.i18n
37+
}
1138

12-
languages.forEach((language) => {
13-
languagesSrc[language] = require('<%= options.path %>' + '/' + '<%= options.outputFilePrefix %>' + '-' + language + '.json')
14-
})
1539
// Set i18n instance on app
1640
app.i18n = new VueI18n({
1741
locale: store.state['<%= options.localeNamespaceStore %>'].locale,

0 commit comments

Comments
 (0)