Integrates react-native-i18n based on I18n.js with redux
Inspired by redux-react-native-i18n
Install :
You need to install react-native-i18n
first... cf. instructions
$ npm install react-native-redux-i18n --save
Inside the box :
// get the user preferred locales (cf. react-native-i18n) // dispatch an action// { type: `SET_LOCALE`, payload: { locale } } // dispatch an action// { type: `SET_TRANSLATIONS`, payload: { translations } } reducer// keep your state up to date Loc// connected React Native component (Text) on I18n.locale// <Loc locKey="greetings" whatever="..." />
Configure :
// app/i18n/index.js I18nfallbacks = true I18ntranslations = translations // app/i18n/translations/en.json "greetings": "Hello {{name}}" "age": "one": "1 year old." "other": "{{count}} years old ... {{more}}" "zero": "... seriously ?" // app/I18n/translations/index.js en: // 'en-GB': require('./en-GB.json'), fr: _version: '1.0' // (you should) use `_version` if you plan to `setTranslations`(update) in-app // app/store/index.js... ... // app/index.js ...
Usage :
// (Optional) app/actions.js // If you plan to fetch and update your translations in-app... const fetchTranslations = { // async } // app/scenes/Home.js ... const Home = <View> <Text><Loc locKey="greetings" name="Matthieu" customizer= text /></Text> <Text><Loc locKey="age" count=27 more="blablabla" /></Text> <Button onPress= >en-EN</Button> <Button onPress= >fr</Button> </View> const mapStateToProps = {} const mapDispatchToProps = mapStateToProps mapDispatchToPropsHome
You can use a customizer
props to change the way text will be rendered... samples:
// uppercase<Loc ="greetings" = />
// lowercase<Loc ="greetings" = />
// and more...<Loc ="greetings" = />
Also you can use it as described in react-native-i18n
// app/index.js Component { return <Text>I18n</Text> } // Enable fallbacks if you want `en-US` and `en-GB` to fallback to `en`I18nfallbacks = true I18ntranslations = en: greeting: 'Hi!' fr: greeting: 'Bonjour!'
or use I18n
directly as described in https://github.com/fnando/i18n-js
Licence
MIT