Ng2-translate - Fallback to default language when file doesn't exist

Hi,

I have set up ng2-translate which translates my entire app and I have an en-GB.json file which contains all of my translations. The problem I have now is that if the user’s device is set to another language (e.g. ‘en-US’), I get 404 errors saying that the file en-US.json doesn’t exist.

I have tried this solution https://github.com/ocombe/ng2-translate#how-to-handle-missing-translations but it seems that I would have to set up a default value for every single translation which isn’t great.

Is there a way to automatically fall back to using the en-GB.json file if en-US.json (and other language files) doesn’t exist?

Thanks for any help.

Try to set up a default languag to be used as a fallback when a translation isn’t found in the current language
translate.setDefaultLang('en');

Sorry I should of mentioned I already do that like so:

    Globalization.getPreferredLanguage().then(
            res => {
              language = res.value;
              this.translate.setDefaultLang('en-GB');
              this.translate.use(language);
           }
   );

Oh okay, try i think the problem is when you set up language.
I have this in a project :

var userLang = navigator.language.split('-')[0]; // use navigator lang if available
userLang = /(fr|en)/gi.test(userLang) ? userLang : 'en';
translate.setDefaultLang('en');
translate.use(userLang);

But i remove what is behind the char “-” in the language. So i only have files : en.json & fr.json here.
However i have a regex to test if i have a file for fr or gb and if i don’t have the user language, i setup userLang to en (defaultLang).

Thanks that’s a good solution, I’ll give it a go :slight_smile: I also asked on Stackoverflow and just stored available languages in an array.

I was trying to overthink it and use a ng2-translate method.