javascript - i18next Load translations from json files - Stack Overflow

I have English json translation file in path: translationen.jsonI init i18next like this:i18next.init(

I have English json translation file in path: translation/en.json

I init i18next like this:

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

after running

i18next.t(KEY);

will print "KEY" and not its value in the translation file

it was working well when the translation were inside 'resource' parameter in i18next object. like below:

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

I use i18next framework

I have English json translation file in path: translation/en.json

I init i18next like this:

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

after running

i18next.t(KEY);

will print "KEY" and not its value in the translation file

it was working well when the translation were inside 'resource' parameter in i18next object. like below:

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

I use i18next framework

Share Improve this question edited Mar 26, 2020 at 14:55 Olcay Ertaş 6,2568 gold badges82 silver badges116 bronze badges asked Sep 27, 2018 at 13:58 ShereinSherein 1,8974 gold badges17 silver badges26 bronze badges 3
  • Can you confirm your .json file gets loaded if you look into the network tab of devtools? Also load: 'languageOnly' should be used if you only use the primary language. – JoeriSmits Commented Sep 27, 2018 at 14:59
  • json file isn't loaded. Also, I use more than one language. – Sherein Commented Sep 30, 2018 at 6:34
  • Here's an example I use perhaps it will help you resolve your problem: pastebin./cbu0jCnZ – JoeriSmits Commented Oct 1, 2018 at 7:23
Add a ment  | 

1 Answer 1

Reset to default 2

I used initImmediate:false which will wait translation to be loaded.

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')
    }
});

I used chrome.runtime.getURL since I use i18next in chrome extension and translation files should be loaded to this folder "translation"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745498654a4630311.html

相关推荐

  • javascript - i18next Load translations from json files - Stack Overflow

    I have English json translation file in path: translationen.jsonI init i18next like this:i18next.init(

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信