How can i restart app when i change language in react native

2.6k Views Asked by At

I tried to create a picker that is used to change language. But some texts remain untranslated even if user selects different language. i tried

import * as RNLocalize from 'react-native-localize';
import I18n from 'i18n-js';
import memoize from 'lodash.memoize'; 

import en from './en';
import am from './am';
import or from './or';
import tg from './tg';


const locales = RNLocalize.getLocales();
if (Array.isArray(locales)) {
  I18n.locale = locales[0].languageTag;
}

I18n.translations = {
  default: en,
  'en-US': en,
  en,
  am,
  or,
  tg,

};

I18n.fallbacks = true;
export default I18n;

the Code I use to switch language is

onChangeLanguage(languageSelected){
  this.setState({
    languageSelected
  })
  I18n.locale = languageSelected 
}

and the react native picker is:

<Picker
  mode="dropdown"
  iosHeader={''} 
  style={{ width: width,height:80,}}
  selectedValue={this.props.language}
  onValueChange={this.props.onChangeLanguage.bind(this)}
>
  {listLanguage.map((languageItem, i) => {
      return <Picker.Item key={i} value={languageItem.key} label= {languageItem.label} />
  })}
</Picker>

Does any one show me how to Restart app when language is changed(selected)? Thanks

2

There are 2 best solutions below

0
On

There is no need to restart your app after language change. In React, the app re-renders if it is dependant on state or props changes. You can check my answer here: React Native, how to update view after language change

0
On

You need to restart only if you change the flow's content. If the language you change to is RTL, you need to restart.

You could use react-native-restart.

You can check their npm page from here.