forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (44 loc) · 1.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import i18next, { LanguageDetectorModule } from 'i18next'
import { initReactI18next } from 'react-i18next'
import { NativeModules, Platform } from 'react-native'
import { languages } from './locale/languages'
declare module 'react-i18next' {
interface CustomTypeOptions {
resources: typeof languages['en-US']
}
}
const fallbackLang = 'en-US'
const detectOSLanguage = () => {
const locale: string | undefined =
Platform.OS === 'ios'
? NativeModules?.SettingsManager?.settings?.AppleLanguages[0] // iOS 13
: NativeModules?.I18nManager?.localeIdentifier
let lang = locale?.replace('_', '-')
// the "en-" check is for weird locales like en-FR
if (lang?.startsWith('en-') && !languages.hasOwnProperty(lang)) {
lang = 'en-US'
}
if (!lang || !languages.hasOwnProperty(lang)) {
lang = fallbackLang
}
return lang
}
const RNLanguageDetector: LanguageDetectorModule = {
type: 'languageDetector',
init: () => {},
detect: detectOSLanguage,
cacheUserLanguage: () => {},
}
export const initI18N = () =>
i18next
.use(RNLanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: fallbackLang,
resources: languages,
returnEmptyString: false,
})
.then()
.catch(e => {
console.log('failed to init i18n:', e)
})