Skip to content

Instantly share code, notes, and snippets.

@levynir
Last active July 4, 2017 07:10
Show Gist options
  • Save levynir/784484b34e59ef8f4e9ddaaf81624945 to your computer and use it in GitHub Desktop.
Save levynir/784484b34e59ef8f4e9ddaaf81624945 to your computer and use it in GitHub Desktop.
Simple add-on to RN I18N package to help working with RTL language and properly support RTL devices even if you are not supporting RTL in your app.
import {I18nManager} from 'react-native';
import I18n from 'react-native-i18n';
import en from './locales/en';
import he from './locales/he';
/*
We are very likely not to have all the languages translated,
so fallback to default locale in case we don't
*/
I18n.fallbacks = true;
/*
Set the translations themselves.
Remember that iw=he
*/
const iw=he;
I18n.translations = {
en,
iw,
he,
};
/*
Only allow RTL if we have translations for RTL languages (ie. not fallbacks)
*/
I18nManager.allowRTL(I18n.locale in I18n.translations);
/*
Set start/end for developer use in non-RTL aware cases,
for example, when using 'react-native-navigation':
this.props.navigator.toggleDrawer({
side: I18n.start, //instead of hardcoing left/right
animated: true
});
*/
I18n.start = I18nManager.isRTL ? 'right' : 'left';
I18n.end = I18nManager.isRTL ? 'left' : 'right';
export default I18n;
//Place in ./locales/en.js
export default {
hello: 'Hello',
home: {
title: 'Home',
},
};
//Place in ./locales/he.js
export default {
hello: 'שלום',
home: {
title: 'בית',
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment