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.

Dynamically change language inside app

See original GitHub issue

Can you provide an example of dynamically change the language inside the app like the one you mentioned in #1? I don’t understand when RN.languages event listener for change actually triggers.

Issue Analytics

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

github_iconTop GitHub Comments

33reactions
FadiAboMsalamcommented, Nov 26, 2018

@sWgiraffe @MrEdinLaw i had managed to dynamic language switch by using

import { I18nManager, AsyncStorage } from "react-native";
import RNRestart from "react-native-restart";

inside my main app App.js

componentDidMount() {
		AsyncStorage.getItem("lang")
			.then(language => {
				if (language === "ar") {
					I18nManager.forceRTL(true);
					if (!I18nManager.isRTL) {
						RNRestart.Restart();
					}
				} else {
					I18nManager.forceRTL(false);
					if (I18nManager.isRTL) {
						RNRestart.Restart();
					}
				}
				i18n.locale = language;
			});
	}

and where i want to change my language - in my case settings screen i have

handleLanguageChange = lang => {
		AsyncStorage.setItem("lang", lang)
			.then(data => {
				RNRestart.Restart();
			})
			.catch(err => {
				console.log("err");
			});
	}

here is my i18n.js

import RNLanguages from "react-native-languages";
import i18n from "i18n-js";

import en from "./translations/en.json";
import ar from "./translations/ar.json";

i18n.locale = RNLanguages.language;
i18n.fallbacks = true;
i18n.translations = { en, ar };

export default i18n;

of course inside Appjs i included the i18n

import i18n from "./i18n";

please let me know if this helps

7reactions
elhachimicommented, Oct 3, 2018

using React navigation and redux, i ended up passing the language to the AppNavigator like

<AppNavigator screenProps={{language}} />

so each time this prop changes on the store it makes the screens reload

Read more comments on GitHub >

github_iconTop Results From Across the Web

The All-In-One Guide for Changing App Locale Dynamically in ...
Select locale in available qualifiers then press >>. Now you can select language and region. The final screen will be like below. I...
Read more >
Android - Dynamically change language - Stack Overflow
In my Android app i want to change the default language dinamically. I have implemented this method: public void changeLanguage(String lang) ...
Read more >
How to force change App Language programmatically without ...
You can give feature inside your app and let the user select the app language instead of changing system language and then see...
Read more >
How to Change the Whole App Language in Android ...
Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have...
Read more >
How to dynamically change display name of mobile app?
In our application we support 3 languages, say, A, B and C. If the default language of the phone is A, we want...
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