Last active
January 30, 2019 15:43
-
-
Save isacjunior/46cda18d2441ea3da16df433d9bd91a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { Platform, Text, View, NativeModules } from 'react-native' | |
import styles from './styles' | |
import I18n from 'i18n-js' | |
function getLanguageByDevice() { | |
return Platform.OS === 'ios' | |
? NativeModules.SettingsManager.settings.AppleLocale | |
: NativeModules.I18nManager.localeIdentifier | |
} | |
const normalizeTranslate = { | |
'en_US': 'en_US', | |
'pt_BR': 'pt_BR', | |
'en': 'en_US', | |
'pt_US': 'pt_BR', | |
} | |
const en = { | |
hello: 'Hello World', | |
} | |
const pt = { | |
hello: 'Olá Mundo', | |
} | |
I18n.translations = { | |
'en_US': en, | |
'pt_BR': pt, | |
} | |
function setLanguageToI18n() { | |
const language = getLanguageByDevice() | |
const translateNormalize = normalizeTranslate[language] | |
const iHaveThisLanguage = I18n.translations.hasOwnProperty(translateNormalize) | |
iHaveThisLanguage | |
? I18n.locale = translateNormalize | |
: I18n.defaultLocale = 'en_US' | |
} | |
setLanguageToI18n() | |
const translate = key => I18n.t(key) | |
class App extends Component { | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text>i18N</Text> | |
<Text>{translate('hello')}</Text> | |
</View> | |
) | |
} | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment