Search code examples
angularjsinternationalizationangular-translate

Angular Translate: Use different fallback languages for specific locales


I want to support translations between e.g. different versions of English, but have a common fallback.

The following is an example of how the setup could be:

locales/en_AU.json
locales/en_US.json
locales/en.json
locales/fr_BE.json
locales/fr_FR.json
locales/fr.json
...

I want to have e.g. some Australian specific strings in en_AU.json, but have the rest in en.json.

In code I would like something like:

$translateProvider.fallbackLanguage(['en_*' : 'en', 'fr_*' : 'fr'])

Or to generally attempt to fallback to whatever is before the underscore.

I would also like to have a general fallback language

Is there a way to accomplish this?


Solution

  • I ended up listening for language changes, and if the new language key contained an underscore (eg. da_DK), I changed the fallback stack to be ['da','en'] and the called refresh.

    $translate.use(languageKey);
    // Check if language has a country code and add general language as fallback along with English
    if (languageKey.indexOf('_') !== -1) {
      $translate.fallbackLanguage([languageKey.substring(0,languageKey.indexOf('_')), 'en']);
    }
    $translate.refresh();
    

    I'm using partial loaders and it didn't work for me initially. It loaded the language I changed to OK, but not the extra fallback language. The last $translate.refresh() was important