aurelia / i18n

A plugin that provides i18n support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrectly forcing the selection of a language

wmansfield opened this issue · comments

if (options && !options.lng) {

The plugin should not force a lng option. When using a detector, this would be dynamically resolved, by forcing this option, it prevents the detector from doing its job. If anything, the fallback language should be forced.

Yeah I see i've introduced a regression when fixing the RT feature. Will have a look at how to so it differently

commented

As said in #234 :
Maybe a better solution should be to enforce a fallbackLng and use it for relative-time when lng is missing!

It is a blocking issue for browser language detection, which I guess everyone who wants to internationalize its app wants too...

commented

A workaround is:

.plugin('aurelia-i18n', instance => {
  let aliases = ['t', 'i18n'];
  TCustomAttribute.configureAliases(aliases);

  instance.i18next
    .use(Backend)
    .use(LngDetector);

  return instance.setup({
    backend: {
      loadPath: './locales/{{lng}}/{{ns}}.json'
    },
    lng: 'en',
    detection: {
      order: ['navigator'], // Enforce navigator language detection
      lookupCookie: 'i18next',
      lookupLocalStorage: 'i18nextLng',
      caches: ['localStorage', 'cookie']
    },
    attributes: aliases,
    fallbackLng: 'en',
    debug: environment.debug
  })
    .then(() => {
      // Enforce language detection
      instance.i18next.changeLanguage();
    });
})

Calling changeLanguage() without lng argument trigger language detection mechanism. Side effect is that setting lng in setup() options cache the selected language in localStorage and cookie so I have to enforce navigator language detection in order to get it, if not language detection find obviously what you set at first in lng... With this specific workaround you cannot allow users to set language differently than the navigator one, at least at every app startup.

Alternatively, and I think it is a better workaround, you can change detection options to:

detection: {
  order: ['localStorage', 'cookie', 'navigator'],
  lookupCookie: 'i18next',
  lookupLocalStorage: 'i18nextLng',
  caches: [] // Enforce no detection caching
}

In that case detection reads cached language but do not writes in cache so you have to set it by yourself in localStorage or cookie when user wants to change its default language.

HTH

Alright sorry for taking me so long to fix this but I think I got it with the recent PR #259. Just waiting for it to properly build and after merge we'll hopefully get a quick release out.

The new Release has been pushed, may I ask you guys to check out whether now everything runs as expected?

commented

Thanks @zewa666! All seems ok for me! 😃

Thanks for the quick response. Im gonna close this for now and we can reopen the issues If any other troubles occur.