Skip to content

Instantly share code, notes, and snippets.

@naishe
Last active July 20, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naishe/a6d9ea9d23214875ac176b63387ab833 to your computer and use it in GitHub Desktop.
Save naishe/a6d9ea9d23214875ac176b63387ab833 to your computer and use it in GitHub Desktop.
Initializing i18next for React Native project
import i18n, {
LanguageDetectorAsyncModule,
Services,
InitOptions,
} from 'i18next';
import {initReactI18next} from 'react-i18next';
import AsyncStorage from '@react-native-community/async-storage';
import * as RNLocalize from 'react-native-localize';
const languageDetector: LanguageDetectorAsyncModule = {
type: 'languageDetector',
// If this is set to true, your detect function receives a callback function that you should call with your language,
//useful to retrieve your language stored in AsyncStorage for example
async: true,
init: (
_services: Services,
_detectorOptions: object,
_i18nextOptions: InitOptions,
) => {
/* use services and options */
},
detect: (callback: (lng: string) => void) => {
AsyncStorage.getItem('APP_LANG', (err, lng) => {
// Error fetching stored data or no language was stored
if (err || !lng) {
if (err) {
console.log('Error fetching "APP_LANG" from async store', err);
} else {
console.log(
'No language is set, choosing the best available or English as fallback',
);
}
const bestLng = RNLocalize.findBestAvailableLanguage(['en', 'hi']);
callback(bestLng?.languageTag ?? 'en');
return;
}
callback(lng);
});
},
cacheUserLanguage: (lng: string) => {
AsyncStorage.setItem('APP_LANG', lng);
},
};
i18n
.use(languageDetector)
.use(initReactI18next) // passes i18n down to react-i18next
.init({
resources: {
en: {
translation: {
'Welcome to React': 'Welcome to React and react-i18next',
},
},
hi: {
translation: {
'Welcome to React': 'रियेक्ट तथा रियेक्ट-आई १८ एन में आपका स्वागत है',
},
},
},
react: {
useSuspense: false,
},
interpolation: {
escapeValue: false,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment