i18next is not loading the translation file
In which folder do you store translations file? Default behavior for i18n is, that it tries to find localization file in specific path: /locales/{lang-code}/{namespace}.json
If you keep file in root, try to change initialization code to following:
i18n.init({
lang: 'en',
debug: true,
fallbackLng: false,
load:'unspecific',
resGetPath: "__ns__-__lng__.json",
ns: {
namespaces: ['translation'],
defaultNs: 'translation'
}
});
This will try to load file from following url: http://localhost:8000/translation-en.json
Basically, try to check location of translations file, name of translation file and construct 'regGenPath' accordingly, more info can be found in i18n documentation http://i18next.com/node/pages/doc_init.html
Related videos on Youtube
Comments
-
Ashwin Hegde almost 2 years
I am working on some Backbone based project where i am using i18next for locales.
Following is my app.js code:
/* This file is used to initialize your application. */ require(['i18n','application','handlebars_Helpers'], function(i18n, Application) { i18n.init({ lng: 'en', debug: true, fallbackLng: false, load:'unspecific', resGetPath: "locales/__lng__/__ns__.json", ns: { namespaces: ['translation'] } }); (new Application()).initialize(); });
Translation file:
{ "loginModule": { "signin": "Sign In" } }
Following is my helper file:
/** * Set of generic handlebars helpers */ define(['i18n'], function(i18n) { /** * This helper provides i18Next in templates * * * Usage: span {{t "my.key" }} */ Handlebars.registerHelper('t', function(i18n_key) { var result = i18n.t(i18n_key); return new Handlebars.SafeString(result); }); return Handlebars; });
When i am loading my page through localhost it shows me following message in console:
currentLng set to: en i18n.js:490 GET http://localhost:8000/locales/en/translation.json?_=1374495189376 404 (Not Found) i18n.js:376 failed loading: locales/en/translation.json
Don't understand what i am missing? or why this error is show?
-
Ashwin Hegde almost 11 yearswhen i copy the URL from firebug and pasted it in address bar of browser it showed me 404; And i removed the "?_=1374495189376" it displayed me the file correctly. Why this extra number is getting added?
-
Marian Polacek almost 11 yearsOk, "?_=1374495189376" is added by jQuery to prevent caching issues. It is just some random number, and it forces browser to download file, even if it was in browser cache previously. You will have to check server side of application. There has to be some setting which prevents download of static files, if there is some query string passed.
-
Ashwin Hegde almost 11 yearsGot it. I have updated my i18N.js with i18next.amd.withJQuery-1.6.3.min.js file & it working now. thanks