-
Notifications
You must be signed in to change notification settings - Fork 88
/
i18n.ts
147 lines (127 loc) · 4.19 KB
/
i18n.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import i18next from 'i18next';
import * as Localization from 'expo-localization';
import {initReactI18next} from 'react-i18next';
import en from './locales/en.json';
import fil from './locales/fil.json';
import ar from './locales/ara.json';
import hi from './locales/hin.json';
import kn from './locales/kan.json';
import ta from './locales/tam.json';
import {iso6393To1} from 'iso-639-3';
import Keychain from 'react-native-keychain';
import {getItem} from './machines/store';
import {LocalizedField} from './machines/VerifiableCredential/VCMetaMachine/vc';
const resources = {en, fil, ar, hi, kn, ta};
const locale = Localization.locale;
const languageCodeMap = {} as {[key: string]: string};
export const SUPPORTED_LANGUAGES = {
en: 'English',
fil: 'Filipino',
ar: 'عربى',
hi: 'हिंदी',
kn: 'ಕನ್ನಡ',
ta: 'தமிழ்',
};
i18next
.use(initReactI18next)
.init({
compatibilityJSON: 'v3',
resources,
lng: getLanguageCode(locale),
fallbackLng: getLanguageCode,
supportedLngs: Object.keys(SUPPORTED_LANGUAGES),
})
.then(async () => {
const existingCredentials = await Keychain.getGenericPassword();
const language = await getItem(
'language',
null,
existingCredentials.password,
);
if (language !== i18next.language) {
i18next.changeLanguage(language);
populateLanguageCodeMap();
}
if (!Object.keys(SUPPORTED_LANGUAGES).includes(i18next.language)) {
i18next.changeLanguage('en');
populateLanguageCodeMap();
}
});
export default i18next;
export function getLanguageCode(code: string) {
const [language] = code.split('-');
return language;
}
export function getValueForCurrentLanguage(
localizedData: LocalizedField[] | Object,
defaultLanguage = '@none',
) {
const currentLanguage = i18next.language;
const currentLanguageCode = languageCodeMap[currentLanguage];
if (Array.isArray(localizedData)) {
const valueForCurrentLanguage = localizedData.filter(
obj => obj.language === currentLanguageCode,
);
return valueForCurrentLanguage[0]?.value
? valueForCurrentLanguage[0].value
: localizedData[0]?.value;
} else {
return localizedData?.value;
}
}
export function getClientNameForCurrentLanguage(
localizedData: Object,
defaultLanguage = '@none',
) {
const currentLanguage = i18next.language;
const currentLanguageCode = languageCodeMap[currentLanguage];
const localizedDataObject = localizedData as {[key: string]: string};
return localizedDataObject.hasOwnProperty(currentLanguageCode)
? localizedDataObject[currentLanguageCode]
: localizedDataObject[defaultLanguage];
}
// This method gets the value from iso-639-3 package, which contains key value pairs of three letter language codes[key] and two letter langugae code[value]. These values are according to iso standards.
// The response received from the server is three letter language code and the value in the inji code base is two letter language code. Hence the conversion is done.
function getThreeLetterLanguageCode(twoLetterLanguageCode: string) {
return iso6393To1
? Object.keys(iso6393To1).find(
key => iso6393To1[key] === twoLetterLanguageCode,
)
: null;
}
function populateLanguageCodeMap() {
const supportedLanguages = Object.keys(SUPPORTED_LANGUAGES);
supportedLanguages.forEach(languageCode => {
let threeLetterLanguageCode = languageCode;
if (isTwoLetterLanguageCode(languageCode)) {
threeLetterLanguageCode = getThreeLetterLanguageCode(languageCode);
}
languageCodeMap[languageCode] = threeLetterLanguageCode;
});
}
export function getLocalizedField(
rawField: string | LocalizedField[] | Object,
) {
if (typeof rawField === 'string') {
return rawField;
}
if (Array.isArray(rawField)) {
try {
if (rawField.length == 1) return rawField[0]?.value;
return getValueForCurrentLanguage(rawField);
} catch (e) {
return '';
}
}
try {
if (Object.keys(rawField).length === 1) {
return Object.values(rawField)[0];
}
return getValueForCurrentLanguage(rawField);
} catch (e) {
return '';
}
}
function isTwoLetterLanguageCode(languageCode: string) {
return languageCode.length == 2;
}