ReactNativeLocalization icon indicating copy to clipboard operation
ReactNativeLocalization copied to clipboard

setLaguage return undefined and doesn't switch language

Open fouad1994 opened this issue 5 years ago • 4 comments

i have these libraries: "react": "16.8.6", "react-native": "0.60.4", "react-native-localization": "^2.1.5",

this is the code:

this.languages = new LocalizedStrings({ "en-US":{ how:"How do you want your egg today?", boiledEgg:"Boiled egg", softBoiledEgg:"Soft-boiled egg", choice:"How to choose the egg" }, en:{ how:"How do you want your egg today?", boiledEgg:"Boiled egg", softBoiledEgg:"Soft-boiled egg", choice:"How to choose the egg" }, ar: { how:"Come vuoi il tuo uovo oggi?", boiledEgg:"Uovo sodo", softBoiledEgg:"Uovo alla coque", choice:"Come scegliere l'uovo" } });

this.languages.how //return "How do you want your egg today?"

but

languages.setLanguage('en-US') //return undefined languages.setLanguage('en') //return undefined languages.setLanguage('ar') //return undefined

fouad1994 avatar Aug 05 '19 18:08 fouad1994

That’s correct. SetLanguage has no return value. You should then use the string key to return the translation...

stefalda avatar Aug 05 '19 19:08 stefalda

I think you need to re-render state:

handleSetLanguage = key => {
    strings.setLanguage(key);
    this.setState({}); <--
};

HeroSony avatar Sep 11 '19 10:09 HeroSony

HeroSony please explain! why use this statement ' this.setState({}); <-- ' ?

minhtet7674 avatar Oct 09 '19 03:10 minhtet7674

@minhtet7674 because this.setState will reload the whole component.

HeroSony avatar Oct 10 '19 05:10 HeroSony