stefalda / ReactNativeLocalization

Class to localize the ReactNative interface
MIT License
898 stars 123 forks source link

setLanguage() on fly. React Native 0.64.2 #207

Open rusakovic opened 2 years ago

rusakovic commented 2 years ago

"react-native-localization": "^2.1.7", "react-native": "0.64.2",

Language in iOS: 'ru' Trying change to: 'it'

But after pressing the button, state is not changeable. If I leave strings.setLanguage('it'); in the component, after reloading the app, language is switched to 'it'. So, is it not possible to change the language on fly? Should I save the language to database, and set the language from database during app loading?

Thank you!

My code:

const App = () => {
  let strings = new LocalizedStrings({
    'en-US': {
      how: 'How do you want your egg today?',
    },
    en: {
      how: 'How do you want your egg today?',
    },
    ru: {
      how: 'даd',
    },
    it: {
      how: 'si',
    },
  });

  const [, setLanguage] = useState(0);

  const setLanguageHandler = () => {
    console.log('language');
    strings.setLanguage('it');
    setLanguage(1);
  }
return (
<>
     <Button  onPress={setLanguageHandler}/>
    <Text>{strings.how}</Text>
</>
)
};