String internationalization in react native app
internationalization is called i18n which is basically the first and last letters in the word internationalization and the 18 characters between them.
To include the library i18n for internationalization, run this:
npm i react-native-i18n --save
Link the module to andorid and ios
react-native link react-native-i18n
After installing the npm package, add the following line to your Podfile for ios
pod 'RNI18n', :path => '../node_modules/react-native-i18n'
and run in the ios directory
pod install
Recompile and run the project with
react-native run-android
Create a file for English strings, app/i18n/locales/en.js
export default { greeting: 'Hi!' };
Create a file for French strings, app/i18n/locales/fr.js
export default { greeting: 'Bonjour!' };
Create a file for default strings, app/i18n/i18n.js
import I18n from 'react-native-i18n'; import en from './locales/en'; import fr from './locales/fr'; I18n.fallbacks = true; I18n.translations = { en, fr }; export default I18n;Example of using it in componentimport I18n from 'app/i18n/i18n'; class Demo extends React.Component { render () { return ({I18n.t('greeting')} ) } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts