-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with findBestAvailableLanguage #57
Comments
@vikrantnegi Thanks for the repo, it helps a lot! Also, please note that the goal of Given the following configs and English, French, and Arabic available translations:👉 It will returns French, because Spanish translation isn't available, but your user actually prefer French over English. 👉 It will returns English because it's the first available translation in the list. 👉 It will returns If you only want to check the first language in the list, a solution could be to use: function checkFirstLanguageOrFallback(
languageTags: string[],
): { languageTag: string, isRTL: boolean } | void {
const { languageTag, languageCode, scriptCode, isRTL } = getLocales()[0];
// languageTag format: en-US, zh-Hans-TW, etc.
// (language code + script code if it exists + country code)
if (languageTags.includes(languageTag)) {
return { languageTag, isRTL };
}
// partialCode format: en, zh-Hans, etc.
// (language code + script code if it exists)
const partialCode = languageCode + (scriptCode ? `-${scriptCode}` : "");
if (languageTags.includes(partialCode)) {
return { languageTag: partialCode, isRTL };
}
// languageCode format: en, zh, etc.
if (languageTags.includes(languageCode)) {
return { languageTag: languageCode, isRTL };
}
} But I will not recommand it, it's not how iOS and Android pick native app translations. |
@zoontek Hey thanks for the detailed explanation. Now I understood how I'm using the iOS simulator and when I changed the preference order I can get the English to be shown. I think this should be mentioned somewhere in the docs so others don't get confused. |
@vikrantnegi That's a good idea. I updated the README to add a warning about it: https://github.com/react-native-community/react-native-localize/tree/1.1.4#findbestavailablelanguage |
If I pass "en-US" to |
I agree with @likern, this make more sens to fallback to a closer language than the app default one :/ |
@HugoGresse I personally don't think it makes sense.
What you want to do here is switching to const translations = {
"en": () => require("../locales/en-US.json"), // en-US is the default "en" file
"en-FR": () => require("../locales/en-FR.json"), // let's suppose we have a "en-FR" variant
};
const fallback = { languageTag: "fr", isRTL: false };
const { languageTag, isRTL } =
RNLocalize.findBestAvailableLanguage(Object.keys(translations)) ?? fallback;
The issue here is the function input, not the output. |
Thanks for your comment @zoontek
I will assume that's because none of the "fr-XX" are available, the Is it a PEBKAC issue or? EDIT yeah, probably a PEBKAC, I think the thing is that the fallback is dynamic, no the i18n-js fallback lang right? |
@HugoGresse If I understand it correctly, you (sort of) have: const translations = {
"fr-CA": () => require("../locales/fr-CA.json"),
"en": () => require("../locales/en.json"),
};
const fallback = { languageTag: "en", isRTL: false }; // here is your fallback in case findBestAvailableLanguage returns undefined
const { languageTag, isRTL } =
RNLocalize.findBestAvailableLanguage(Object.keys(translations)) ?? fallback;
By making EDIT: |
If the translation key is |
Bug
findBestAvailableLanguage
is not falling to fallback language i.e. English when I change the language to Spanish.Steps To Reproduce
Just like the example app, I'm using three languages English, French, and Arabic.
Environment info
Describe what you expected to happen:
Reproducible sample code
Find the repo here
The text was updated successfully, but these errors were encountered: