Flexible internalization of your React Native project with "i18next" library. The i18next library is used for the message format.
If your application on Expo:
npx expo install expo-localization
Use npm:
npm i react-native-international
Use yarn:
yarn add react-native-international
The first step is to add language packs.
For first make directory translations
and separate files for each language.
export default <LanguagePack>{
locale: 'en',
meta: { // Shown in method getLanguages
label: 'English',
},
translations: {
hello: 'Hello!',
hello_with_name: 'Hello {{name}}!',
},
}
export default <LanguagePack>{
locale: 'ky',
meta: { // Shown in method getLanguages
label: 'Кыргызча',
},
translations: {
hello: 'Салам!',
hello_with_name: 'Салам {{name}}!',
}
}
Make initiator, for example, translations/_i18t.ts
.
import { initialization } from 'react-native-international'
import { getLocales } from "expo-localization";
import enLang from './en'
import kyLang from './ky'
const localeFromPhone = async () => {
// You can take saved language from storage here
return getLocales()?.[0]?.languageCode ?? "en";
}
void initialization({
defaultFallback: 'en',
languages: [
enLang,
kyLang
],
localeFromPhone,
debug: true // i18next debug (optional)
})
Open index.js
, App.tsx
, or app/_layout.tsx
and add languages initialization import.
import "./localization/_i18n"
If you want to set language immediately, you can use localeFromPhone
handler
Use a webhook useIntl
in a component that uses strings.
import React from 'react'
import { View, Text } from 'react-native'
import { useIntl } from 'react-native-international'
export default ({ navigation }) => {
const {
t, // Instance i18next
} = useIntl()
return (
<View>
<Text>{t('hello')}</Text>
<Text>{t('hello_with_name', {name: 'Smith'})}</Text>
</View>
)
}
There are several helper methods for working with languages.
const {
t, // Instance format-message
locale, // Current locale string
getLanguages, // Method to get locales with "meta" property from language pack and "selected" flag.
changeLocale, // Method to change locale
} = useIntl()
Return current locale.
console.log(locale)
// Return "en"
Get languages method return array.
const {
getLanguages, // Method to change locale
} = useIntl()
const languages = getLanguages()
console.log(languages)
// [{
// locale: 'en',
// selected: true,
// meta: {
// label: 'English' // Meta from language pack
// }
// }, {
// locale: 'ky',
// selected: false,
// meta: {
// label: 'Кыргызча' // Meta from language pack
// }
// }]
Will change the language. If suddenly the locale was not found, use the fallback locale or the last selected one.
const {
changeLocale, // Method to change locale
} = useIntl()
changeLocale('ky')