i18next 从 json 文件加载翻译

She*_*ren 5 javascript jquery translation i18next

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

我像这样初始化 i18next:

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

跑完后

i18next.t(KEY);
Run Code Online (Sandbox Code Playgroud)

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

当翻译在 i18next 对象中的“资源”参数内时,它运行良好。像下面这样:

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

我使用 i18next 框架

She*_*ren 1

我使用 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')
    }
});
Run Code Online (Sandbox Code Playgroud)

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