1

I'm using i18next for the internationalization of an application.

I am initializing it like this:

i18n.init({debug: false, getAsync: false, resGetPath: _this.buildPath + _this.i18nPath + '__lng__/__ns__.json'});

What do the variables __lng__ and __ns__ stand for?

Josh KG
  • 5,050
  • 3
  • 20
  • 24

1 Answers1

0
  • __lng__ is the language to load
  • __ns__ is the namespace to load

Basically it means that if you initialise with lng: 'en-US' it will try to load:

  1. _this.buildPath + _this.i18nPath + 'en-US/translation.json'
  2. _this.buildPath + _this.i18nPath + 'en/translation.json'
  3. _this.buildPath + _this.i18nPath + 'dev/translation.json'

Because dev is the default falback language & translation is the default namespace.

You may find the docs here useful as it shows some more ways of using them depending on how your backend works.

EDIT: There are a number of ways to set the language which is loaded. The simplest way to do this is by passing the value as the lng parameter of i18n.init(). An example of this would be:

i18n.init({
    debug: false, 
    getAsync: false, 
    lng: _this.language,
    fallbackLng: _this.defaultLanguage,
    resGetPath: _this.buildPath + _this.i18nPath + '__lng__/__ns__.json'
});

This example is the same as yours but explicitly sets the language. I also set fallbackLng but this is mainly useful if you accept the language from a user in some way (e.g from browser language) so that any unsupported languages will fallback to a default. If you only allow selection from pre-approved values then this may not be required.

Community
  • 1
  • 1
joelnb
  • 1,426
  • 11
  • 14
  • Thanks very much for your answer! Generally I understand now, how it works. But the variable `lng` is never initialized, where does it get it's value from in that case? –  May 17 '16 at 06:43
  • I added some bits to the answer which hopefully make it more clear how you can set the language but I realise now that may not be what you meant. If you meant how is the language being set for you now then it comes from [this function](https://github.com/i18next/i18next/blob/1.11.2/i18next.js#L1843) in i18next which detects the correct language to use from cookies, localStorage & then `window.navigator` properties. – joelnb May 17 '16 at 18:54
  • That was the answer I was looking for :) thank you very much one more time, you really helped me a lot! –  May 20 '16 at 06:48