question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with findBestAvailableLanguage

See original GitHub issue

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.

  1. When I change the language to French it shows the French Text
  2. Now, when I change the language to Spanish which is not supported and should fallback to English as a fallback, it remains French.

Environment info

"i18n-js": "^3.3.0",
"lodash.memoize": "^4.1.2",
"react": "16.8.3",
"react-native": "0.59.9",
"react-native-localize": "^1.1.3"

Describe what you expected to happen:

  1. When I change the language to Spanish it should fallback to English language

Reproducible sample code

Find the repo here

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
zoontekcommented, Jul 4, 2019

@vikrantnegi Thanks for the repo, it helps a lot! I just give it a try, and it worked as planned on my device (android 9). Which OS / version do you target?

Also, please note that the goal of findBestAvailableLanguage is to returns the best available translation here.

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 undefined (so the retained value will be your fallback), because none of the user preferred languages are availables in your translations.

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.

3reactions
zoontekcommented, Jul 4, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with findBestAvailableLanguage #57 - GitHub
Bug findBestAvailableLanguage is not falling to fallback language i.e. English when I change the language to Spanish.
Read more >
How to use the react-native-localize ... - Snyk
To help you get started, we've selected a few react-native-localize.findBestAvailableLanguage examples, based on popular ways it is used in public projects.
Read more >
Missing translation problem with react-native-localize even ...
I think in homescreen during translating one has to import the translate function and use {translate('home')} instead of i18n.t() in ...
Read more >
react-native-localize - npm
findBestAvailableLanguage()​​ Returns the best language tag possible and its reading direction ( ⚠️ it respects the user preferred languages list ...
Read more >
Discussion on: Creating a Multi-Language App in React Native
Replies for: Hello, I have problem with rewrite your code using Typescript, ... findBestAvailableLanguage(Object.keys(translationGetters)) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found