core icon indicating copy to clipboard operation
core copied to clipboard

Support for fallback language

Open jstoup111 opened this issue 7 years ago • 9 comments

I wasn't able to find this is the docs or on the internet. Is there a way to have the translation fallback to the standard language if it can't find a dialect?

I.E

I have a en.json file. When I retrieve the locale, I'd like to set it and have it fall back to the language if it can't find the dialect.

en-US & en-GB & en-UK should fall back to en.json if the file doesn't exist. fr-CA should fallback to fr.json and then whatever I specified as default if it doesn't find fr.json

Is this how the library works, and if not how can I get it to function this way?

jstoup111 avatar Jan 14 '17 04:01 jstoup111

Hey - here is how we do it:

In our root component's ngOnInit we have the following logic:

        const langs = [
            'en-GB',
            'de'
        ];

        let isSupported = langs.find(supportedLanguage => supportedLanguage === navigator.language);

        if (isSupported) {
            this.translate.use(navigator.language);
        }
        else {
            this.translate.use('en-GB');
        }

SamFarrington avatar Jan 17 '17 11:01 SamFarrington

I'd expect the same behavior to be provided. It seems it isn't available.

@SamFarrington This won't fall back to the specific language. Think about en-US and de-AT.

andys8 avatar Mar 16 '18 14:03 andys8

The fallback language support was excellent in angular-translate. There should be a stack or chain of fallback languages https://angular-translate.github.io/docs/#/guide/08_fallback-languages

malikhasan avatar Apr 03 '19 14:04 malikhasan

Are there any plans to get this feature in near future?

deyankush avatar Sep 15 '20 11:09 deyankush

Any update on this?

LendaVadym avatar Feb 23 '22 09:02 LendaVadym

I don't know if you offer a way to vote for enhancements, but this would be my #1 request as well :)

sebastientromp avatar Mar 01 '22 16:03 sebastientromp

@jstoup111 Did you ever find a solution for this? I am facing the same issue.

NuxXDev avatar Aug 18 '22 12:08 NuxXDev

@jstoup111 Did you ever find a solution for this? I am facing the same issue.

I did not and have been off the project that I needed this in for quite awhile now. It might be best to keep a reference to each language you support and then just split the string if it doesn't find it in the set.

const langs = new Set() //contains all your supported langs.

selectedLang = "en-US" // assume this isn't found

while(!langs.has(selectedLang)) {
  if(selectedLang.includes("-") {
    selectedLang = selectedLang.split("-")[0];
  else {
    selectedLang = defaultLang
  }
}

this.translate.use(selectedLang)

I haven't used this package in a long time so the code above likely needs some love, but the concept should work great.

jstoup111 avatar Aug 18 '22 12:08 jstoup111

Thanks, that is almost exactly what I ended up doing.

NuxXDev avatar Aug 18 '22 12:08 NuxXDev