i18next is not loading the translation file

Ashwin Hegde picture Ashwin Hegde · Jul 22, 2013 · Viewed 13.2k times · Source

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?

Answer

Marian Polacek picture Marian Polacek · Jul 22, 2013

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