I18next: Load `en-US` and not `en`

Created on 15 Sep 2017  路  6Comments  路  Source: i18next/i18next

.init({
    backend: opts,
    lng: `en-US`, debug: true, load: "currentOnly"

For some reason, en is also being loaded (and subsequently not found).

How can I fix this? I only want en-US to load.

Most helpful comment

reading the docs

language codes to lookup, given set language is 'en-US': 'all' --> ['en-US', 'en', 'dev'], 'currentOnly' --> 'en-US', 'languageOnly' --> 'en'

when I use the language detector, this config returns en-US even though this is clearly not in the whitelist.

let i18nOptions = {
  backend: {
    loadPath: '/locales/{{lng}}/{{ns}}.json',
  },
  fallbackLng: 'en',
  react: {
    wait: true,
  },
  ns: ['common'],
  defaultNS: 'common',
  debug: process.env.NODE_ENV === 'development',
  cache: {
    enabled: true,
  },
  load: 'languageOnly',
  whitelist: ['de', 'en'],
}
i18n.use(LanguageDetector)
  .use(Backend)
  .init(i18nOptions)

changing the load-option to currentOnly resolves the issue.

it seems to me, that either the config is wrong, or the behaviour.

Version:

i18next@^9.0.0:
  version "9.0.0"
  resolved "https://registry.yarnpkg.com/i18next/-/i18next-9.0.0.tgz#a89ab0481b5b6b3964f55b12f03de9063d8f4500"

that whole peace of code is not very straight forward, is it?
https://github.com/i18next/i18next/blob/4caedbf610fe48c347b3448d76b703a9b07dfaed/src/LanguageUtils.js#L97-L101

All 6 comments

do you have en set as fallback language? that would make sense - else it really should only load en-US

No fallback specified

could you paste your complete config? really that should work...there is a test case asserting this works as expected:

https://github.com/i18next/i18next/blob/master/test/languageUtils.spec.js#L136

really strange...

reading the docs

language codes to lookup, given set language is 'en-US': 'all' --> ['en-US', 'en', 'dev'], 'currentOnly' --> 'en-US', 'languageOnly' --> 'en'

when I use the language detector, this config returns en-US even though this is clearly not in the whitelist.

let i18nOptions = {
  backend: {
    loadPath: '/locales/{{lng}}/{{ns}}.json',
  },
  fallbackLng: 'en',
  react: {
    wait: true,
  },
  ns: ['common'],
  defaultNS: 'common',
  debug: process.env.NODE_ENV === 'development',
  cache: {
    enabled: true,
  },
  load: 'languageOnly',
  whitelist: ['de', 'en'],
}
i18n.use(LanguageDetector)
  .use(Backend)
  .init(i18nOptions)

changing the load-option to currentOnly resolves the issue.

it seems to me, that either the config is wrong, or the behaviour.

Version:

i18next@^9.0.0:
  version "9.0.0"
  resolved "https://registry.yarnpkg.com/i18next/-/i18next-9.0.0.tgz#a89ab0481b5b6b3964f55b12f03de9063d8f4500"

that whole peace of code is not very straight forward, is it?
https://github.com/i18next/i18next/blob/4caedbf610fe48c347b3448d76b703a9b07dfaed/src/LanguageUtils.js#L97-L101

Feel free to improve the code...;)...PR are very welcome.

language detector always returns what it detects - also i18next.language reflects what it detected.

i18next.languages is an array and reflects what is used!!!

closing for now...reopen if you need more info or still an issue

Was this page helpful?
0 / 5 - 0 ratings