Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18next is not loading the translation file

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?

like image 558
Ashwin Hegde Avatar asked Jul 22 '13 12:07

Ashwin Hegde


People also ask

What is namespace in i18next?

Namespaces are a feature in i18next internationalization framework which allows you to separate translations that get loaded into multiple files. While in a smaller project it might be reasonable to just put everything in one file you might get at a point where you want to break translations into multiple files.

What is i18next HTTP backend?

With i18next you can configure a fallback backend to be used in the browser. It will try to load from your primary backend (in this case from your http backend) and if the primary backend is not reachable or does not serve translations, your second backend (in this case local or bundled translations) will be used.

What is i18next in angular?

Angular Internationalizationlink Internationalization, sometimes referenced as i18n, is the process of designing and preparing your project for use in different locales around the world. Localization is the process of building versions of your project for different locales.


1 Answers

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

like image 104
Marian Polacek Avatar answered Sep 21 '22 08:09

Marian Polacek