aurelia / i18n

A plugin that provides i18n support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Load translation key from specific language without changing i18n language?

mikeesouth opened this issue · comments

I use aurelia-i18n with two languages: Swedish (sv) and English (en). Everything works as expected. But I would like to fetch a translation for a specific language without changing the language for the entire i18n instance.
Say that the user has Swedish language selected, i18n is initialized and set to Swedish. In 99% of all cases i18n.tr('my-key') gives the correct translation (Swedish).
In some special cases I would like to force the translation to english without changing the i18n language - how can I achieve this?

I thought I could use the i18next.getFixedT() function but it doesn't seem to work, i.e. this does not work:
let myEnglishString = this.i18n.i18next.getFixedT('en', 'translation')('my-key');
I've also tried with getFixedT('en', null)('my-key'); and other variants.
I get the Swedish string even if I specify 'en' in the getFixedT() call.

I'm setting up my i18n in main.ts like this for a Swedish user (lng = sv):

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

    return instance.setup({
      backend: {
        loadPath: './locales/{{lng}}/{{ns}}.json'
      },
      attributes: aliases,
      lng : 'sv',
      fallbackLng : 'en',
      debug : true
    });
  });

I've just tried the very same with German (DE) and English (EN) and it does work for me with getFixedT. Could you maybe create an example and upload it so I can debug the issue?

Since this cant be reproduced i'll close it for now. We can reopen once there is more info