-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathi18n.js
More file actions
31 lines (26 loc) · 972 Bytes
/
i18n.js
File metadata and controls
31 lines (26 loc) · 972 Bytes
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
import { getCurrentUserLanguage } from "../utils/methods";
import T from "i18n-react";
import en from './en.json';
import zh from './zh.json';
import es from './es.json';
const resources = {
'en': en,
'zh': zh,
'es': es,
}
export const initI18n = (dictionary) => {
let language = getCurrentUserLanguage();
// language would be something like es-ES or es_ES
// However we store our files with format es.json or en.json
// therefore retrieve only the first 2 digits
if (language.length > 2) {
language = language.split("-")[0];
language = language.split("_")[0];
}
const isDictionaryValid = dictionary && typeof dictionary === 'object' && !Array.isArray(dictionary);
try {
T.setTexts(isDictionaryValid ? { ...resources[language], ...dictionary } : resources[language]);
} catch (e) {
T.setTexts(isDictionaryValid ? { ...resources['en'], ...dictionary } : resources['en']);
}
};