ReactNativeLocalization icon indicating copy to clipboard operation
ReactNativeLocalization copied to clipboard

setLanguage() only change language for current component

Open manelsanz opened this issue 6 years ago • 4 comments

Hi, I know that is not specially a BUG, but it's really annoying. After setLanguage() only change in the current component, and in the others when they update / render next time. What I do, is store the new language key on AsyncStorage and on the file App.js I set the language previously stored. And that works fine. But, I would like that when the user change the language, the whole app refresh that strings without restarting the app. I use Redux in my app.

How I can achieve that changes are applied without force to restart the App?

Thanks.

manelsanz avatar Apr 04 '19 12:04 manelsanz

A workaround I did was setting a key "language" on redux store and importing it on all components and that produces a rerender, or directly forcing update with this.forceUpdate().

manelsanz avatar Apr 08 '19 11:04 manelsanz

Hello, by the mechanism of react-native-localization. strings.key is just return a getter to get localized string. So, it not depend on any render flow, and your change will not affect still component re-rendering.

You can write a custom Text component which have a props from redux statenamedlocale. So whenever your locale` change. It will re-render your CustomText components

william-normann avatar Apr 13 '19 06:04 william-normann

You can write a custom Text component which have a props from redux statenamedlocale. So whenever your locale` change. It will re-render your CustomText components

Can you show me how to do this? My text component is getting re rendered but it is still not changing the locale.

rubek-joshi avatar Jul 23 '20 09:07 rubek-joshi

I solved it by creating a Localization Provider and using context hooks. More info here.

I think this is the more convenient way to do this.

rubek-joshi avatar Jul 23 '20 16:07 rubek-joshi