Last active
July 20, 2020 14:50
-
-
Save naishe/a6d9ea9d23214875ac176b63387ab833 to your computer and use it in GitHub Desktop.
Initializing i18next for React Native project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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