Skip to content

Instantly share code, notes, and snippets.

@isacjunior
Last active January 30, 2019 15:43
Show Gist options
  • Save isacjunior/46cda18d2441ea3da16df433d9bd91a8 to your computer and use it in GitHub Desktop.
Save isacjunior/46cda18d2441ea3da16df433d9bd91a8 to your computer and use it in GitHub Desktop.
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