我正在构建一个多语言的Nuxt网络应用程序。
使用官方文档 (Codepen 链接)中的这个示例,我不再希望使用本地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值的可用替代方法是什么?
plugins/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指向/locals/en.json文件,该文件托管在我的Github配置文件上。
发布于 2020-03-12 09:38:40
可以在构造函数中直接将axios与await一起使用:
export default async ({ app, store }) => {
app.i18n = new VueI18n({ //construction a new VueI18n
locale: store.state.i18n.locale,
fallbackLocale: 'de',
messages: {
'de': await axios.get('http://localhost:3000/lang/de.json').then((res) => {
return res.data
}),
'en': await axios.get('http://localhost:3000/lang/en.json').then((res) => {
return res.data
})
}
})
}发布于 2018-10-05 14:39:32
我有一个localise.biz和交叉获取的解决方案
首先,将async添加到插件plugins / i18n.js函数中,然后将await添加到远程转换调用中:
import Vue from 'vue';
import VueI18n from 'vue-i18n';
import getMessages from './localize';
Vue.use(VueI18n);
export default async ({ app, store }) => {
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': await getMessages('en'),
'fr': await getMessages('fr')
}
});
app.i18n.path = (link) => {
if (app.i18n.locale === app.i18n.fallbackLocale) return `/${link}`;
return `/${app.i18n.locale}/${link}`;
}
}并为获取远程翻译创建新功能:
import fetch from 'cross-fetch';
const LOCALIZE_API_KEY = 'XXXXXXXXXXX';
const LOCALIZE_URL = 'https://localise.biz/api/export/locale';
const HEADERS = {
'Authorization': `Loco ${LOCALIZE_API_KEY}`
};
const getMessages = async (locale) => {
const res = await fetch(`${LOCALIZE_URL}/${locale}.json`, { headers: HEADERS });
if (res.status >= 400) throw new Error("Bad response from server");
return await res.json();
};
export default getMessages;发布于 2021-03-19 00:48:17
这就是我最后的结局:
async asyncData(context){
// fetch translation for your source
var translation = await fetch('/translation')
// get locale of current page
var locale = context.app.i18n.locale
// set translation for Server Side Rendering
context.app.i18n.mergeLocaleMessage(locale, translation)
// save it for use on client side
return {translation: translation}
},
created(){
// prevent reverting back to not found after hard-loading page.
this.$i18n.mergeLocaleMessage(this.$i18n.locale, this.translation)
}https://stackoverflow.com/questions/52603941
复制相似问题