gpt4 book ai didi

javascript - i18next 从 json 文件加载翻译

转载 作者:行者123 更新时间:2023-11-30 06:22:09 25 4
gpt4 key购买 nike

我在路径中有英文 json 翻译文件:translation/en.json

我这样初始化 i18next:

i18next.init({
lng: navigator.language,
fallbackLng : "en",
backend: {
loadPath: '/translation/{{lng}}.json',
}
});

运行后

i18next.t(KEY);

将打印“KEY”而不是它在翻译文件中的值

当翻译位于 i18next 对象的“资源”参数内时,它运行良好。如下所示:

i18next.init({
lng: navigator.language,
fallbackLng : "en",
resources: {
en: {
translation: {
"KEY": "keyValue"
}
}
}
});

我用的是i18next框架

最佳答案

我使用了 initImmediate:false,它将等待翻译被加载。

i18next
.use(i18nextXHRBackend)
.init({
//debug:true,
initImmediate: false, // set initImmediate false -> init method finished only when all resources/translation finish loading (async behaviour)
lng: "en",
fallbackLng : "en",
backend:{
loadPath: chrome.runtime.getURL('translation/{{lng}}.json')
}
});

我使用了 chrome.runtime.getURL 因为我在 chrome 扩展中使用了 i18next 并且翻译文件应该加载到这个文件夹“translation”

关于javascript - i18next 从 json 文件加载翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com