Internationalization
The starter comes with a basic internationalization setup. It uses expo-localization and i18next to provide a simple way to translate your app.
Adding a new language
Mainly the demo app supports two languages: English and Arabic (RTL). You can add more languages by adding the translation files in the src/translations folder and adding the language code to the src/lib/i18n/resources.ts file.
import en from '@/translations/en.json';
export const resources = { en: { translation: en, },};
export type Language = keyof typeof resources;Using translations in your app
The i18n core module provides a set of utility functions to help you use translation in your app.
useTranslationhook fromreact-i18next: to get the translation function.
import React from 'react';import { useTranslation } from 'react-i18next';
import { Text } from '@/components/ui';
export const Foo = () => { const { t } = useTranslation(); return ( <> <Text className="text-center">{t('settings.language')}</Text> </> );};or as Text component comes with translation support, you can easily use it as follows:
import React from 'react';
import { Text } from '@/components/ui';
export const Foo = () => { return <Text className="text-center" tx="settings.language" />;};useSetLanguagehook: to change the current language. This hook returns the selected language and a function to change the language.
Additionally, the useSetLanguage hook will save the selected language in device storage using MMKV and will be used as the default language when the app is opened again As well as adding some extra config for RTL languages while updating the selected language.
import type { TOptions } from 'i18next';import type { Language, resources } from './resources';import type { RecursiveKeyOf } from './types';import { getLocales } from 'expo-localization';import i18n, { changeLanguage as i18nChangeLanguage, t } from 'i18next';import memoize from 'lodash.memoize';import { useCallback, useEffect } from 'react';import { AppState, NativeModules, Platform } from 'react-native';
import { useMMKVString } from 'react-native-mmkv';import RNRestart from 'react-native-restart';import { storage } from '../storage';
type DefaultLocale = typeof resources.en.translation;export type TxKeyPath = RecursiveKeyOf<DefaultLocale>;
export const LOCAL = 'local';
export const getLanguage = () => storage.getString(LOCAL); // 'Marc' getItem<Language | undefined>(LOCAL);
// Language tag of the first preferred OS locale, e.g. 'en-US'export function getDeviceLanguage(): string | undefined { return getLocales()[0]?.languageTag;}
// The language selected in the app wins over the OS languageexport function resolveLanguage(): string | undefined { return getLanguage() ?? getDeviceLanguage();}
// Follows the OS language when no language was selected in the appexport function syncLanguageWithOS() { if (getLanguage() !== undefined) { return; } const deviceLanguage = getDeviceLanguage(); if (deviceLanguage !== undefined && deviceLanguage !== i18n.language) { void i18nChangeLanguage(deviceLanguage); }}
// The OS language can change while the app is in the background,// so it is read again every time the app comes back to the foregroundexport function useSyncLanguageWithOS() { useEffect(() => { const subscription = AppState.addEventListener('change', (state) => { if (state === 'active') { syncLanguageWithOS(); } }); return () => subscription.remove(); }, []);}
export const translate = memoize(
(key: TxKeyPath, options?: Record<string, unknown>) => t(key, options as TOptions), (key: TxKeyPath, options?: unknown) => options === undefined ? key : `${key}${JSON.stringify(options)}`,) as (key: TxKeyPath, options?: Record<string, unknown>) => string;
export function changeLanguage(lang: Language) { void i18nChangeLanguage(lang); if (Platform.OS === 'ios' || Platform.OS === 'android') { if (__DEV__) { (NativeModules.DevSettings as { reload: () => void }).reload(); } else { RNRestart.restart(); } } else if (Platform.OS === 'web') { window.location.reload(); } else { throw new Error('Unexpected value for Platform.OS'); }}
export function useSelectedLanguage() { const [language, setLang] = useMMKVString(LOCAL);
const setLanguage = useCallback( (lang: Language) => { setLang(lang); if (lang !== undefined) { changeLanguage(lang); } }, [setLang], );
return { language, setLanguage };}Following the OS language
When no language has been selected in the app, the app uses the OS language. The useSyncLanguageWithOS hook, mounted once in src/app/_layout.tsx, reads the OS language again every time the app comes back to the foreground and updates the current language if the user changed it in the device settings. Once a language is selected in the app, it always takes precedence over the OS language.
Robust translation
Typescript support
To make the translation for your app more robust and easy to maintain, we have added TypeScript support for the translation function. This ensures that you are using the correct translation key and provide auto-completion for the translation keys. Here is an example of the error you will get if you are using the wrong translation key.
Eslint rules
We have also added ESLint rules to ensure that your translation file resources are always up to date. These rules will run as a pre-commit hook whenever you attempt to commit changes to the translation files.
We utilize eslint-plugin-i18n-json alongside a customized validation script for i18next. This script helps sort keys, ensures that all resource files are identical and prevents any missing translations.