【发布时间】:2019-03-07 07:41:47
【问题描述】:
我正在构建一个多语言 Nuxt 网络应用程序。
使用来自官方documentation (Codepen link) 的这个例子,我不想再使用保存我的翻译的本地 JSON 文件来按照以下代码中的定义工作:
messages: {
'en': require('~/locales/en.json'), # I want to get this asynchronously from an HTTP URL
'fr': require('~/locales/fr.json') # I want to get this asynchronously from an HTTP URL
}
我想知道有哪些替代方法可以通过从 URL 读取 JSON 数据来异步设置 en 和 fr 值?
插件/i18n.js:
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
export default ({ app, store }) => {
// Set i18n instance on app
// This way we can use it in middleware and pages asyncData/fetch
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': require('~/locales/en.json'), # How to get this asynchronously?
'fr': require('~/locales/fr.json') # # How to get this asynchronously?
}
})
app.i18n.path = (link) => {
if (app.i18n.locale === app.i18n.fallbackLocale) {
return `/${link}`
}
return `/${app.i18n.locale}/${link}`
}
}
我尝试了什么:
messages: {
'en': axios.get(url).then((res) => {
return res.data
} ),
'fr': require('~/locales/fr.json')
}
url 指向托管在我的 Github 个人资料上的 /locals/en.json 文件。
【问题讨论】:
标签: javascript asynchronous nuxt.js nuxt-i18n