React Native and Expo
The same hooks and components in a React Native or Expo app; four things differ from the web.
react-i18next 18 needs React 18 or later, which means React Native 0.69 or later.
Install
npm install react-i18next i18next intl-pluralrules
For the device language, add react-native-localize in a React Native app or expo-localization in an Expo app.
Plurals on Hermes
i18next needs Intl.PluralRules for plurals. Hermes, the JavaScript engine of React Native and Expo, does not implement it (Hermes' list of Intl APIs). Import the polyfill before i18next:
import 'intl-pluralrules';
The device language
There is no browser to ask, so read the language from the device and pass it as lng. With react-native-localize:
import { getLocales } from 'react-native-localize';
const lng = getLocales()[0]?.languageCode;
@os-team/i18next-react-native-language-detector, maintained by the community, does the same as a ready language detector for .use(), on top of react-native-localize.
With Expo, getLocales comes from expo-localization and is called the same way.
Bundled translation files
Import the JSON files and pass them as resources. i18next then initializes synchronously, so the translations are ready on the first render and nothing suspends.
// i18n.js
import 'intl-pluralrules';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
// Expo: from 'expo-localization'
import { getLocales } from 'react-native-localize';
import en from './locales/en/translation.json';
import de from './locales/de/translation.json';
i18n
.use(initReactI18next)
.init({
resources: {
en: { translation: en },
de: { translation: de }
},
lng: getLocales()[0]?.languageCode,
fallbackLng: 'en',
supportedLngs: ['en', 'de'],
interpolation: {
// React escapes what it renders; escape t() output yourself
// before dangerouslySetInnerHTML
escapeValue: false
}
});
export default i18n;
Import ./i18n once, in your entry file, before the app renders. useTranslation then works in every component as described in Getting started.
Trans inside Text
In React Native, text must be inside a <Text> element. Trans renders no element of its own unless you give it a parent, so render it inside <Text> (or pass parent={Text}), and map its tags to Text elements:
import { Text } from 'react-native';
import { Trans } from 'react-i18next';
<Text>
<Trans
i18nKey="welcome"
values={{ name }}
components={{ bold: <Text style={{ fontWeight: 'bold' }} /> }}
/>
</Text>
// "welcome": "Hello <bold>{{name}}</bold>, welcome back."
More on tags and components: Trans component.