core
core copied to clipboard
Support for fallback language
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?
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');
}
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
.
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
Are there any plans to get this feature in near future?
Any update on this?
I don't know if you offer a way to vote for enhancements, but this would be my #1 request as well :)
@jstoup111 Did you ever find a solution for this? I am facing the same issue.
@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.
Thanks, that is almost exactly what I ended up doing.