2
votes

How can I make i18next load all languages from just one file?

I managed to do it by putting each language in a seperate file (translation-en.json, translation-no.json, etc), and also managed to input languages with the resStore option, but putting it all in a seperate .json file is really not documented anywhere (I've searched for 4 hours+ now)

My js code:

i18n.init({
    debug: true,
    lng: 'en',
    resGetPath: 'translation.json'
},
function(t) {
    console.log(t('test'));
});

My translation.json file:

{
    en: { 
        translation: {
            test: "some string"
        }
    },
    no: { 
        translation: {
            test: "litt tekst"
        }
    }
}

Ok, so I managed to "hack" it byt putting an object into a seperate .js file, include it in a script tag and loading it using resStore, but that just can't be the best way to use this lib.

1

1 Answers

2
votes

Assume that your translation.json has loaded and assigned to a variable named resStore:

var resStore = {
    en: { 
        translation: {
            test: "some string"
        }
    },
    no: { 
        translation: {
            test: "litt tekst"
        }
    }
};

Next, you can override default ajax loading functionality with your customLoad function. An example might look like this:

var options = {
    lng: 'en',
    load: 'current',
    lowerCaseLng: true,
    fallbackLng: false,
    resGetPath: 'i18n/__lng__/__ns__.json',
    customLoad: function(lng, ns, options, loadComplete) {
        var data = resStore[lng][ns];
        loadComplete(null, data); // or loadComplete('some error'); if failed
    },
    ns: {
        namespaces: ['translation'],
        defaultNs: 'translation'
    }
};
i18n.init(options, function(t) {
    t('test'); // will get "some string"
});

new update on Mar 20, 2015

You can simply pass your resource store with the resStore option:

i18n.init({ resStore: resources });