-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
51 lines (41 loc) · 1.31 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
51
export interface MessageTable {
[message: string]: string;
}
export interface LanguageTable {
[tag: string]: MessageTable;
}
const defaultLocale = 'en-US';
function getLangCode(tag: string) {
return tag.substring(0, 2);
}
function mapToDeviceSupportedLocale(tag: string) {
if (tag === 'en-SE') return 'sv-SE';
if (tag === 'en-NL') return 'nl-NL';
if (tag === 'en-NO') return 'nb-NO';
if (tag === 'en-IN') return 'hi-IN';
// For iOS
if (tag === 'zh-Hant') return 'zh-TW';
if (tag === 'zh-Hans') return 'zh-CN';
const lang = getLangCode(tag);
if (lang === 'en') return 'en-US';
if (lang === 'de') return 'de-DE';
if (lang === 'es') return 'es-ES';
if (lang === 'fr') return 'fr-FR';
if (lang === 'it') return 'it-IT';
if (lang === 'ru') return 'ru-RU';
if (lang === 'pt') return 'pt-BR';
return tag;
}
export default function gettextFactory(
langTable: LanguageTable,
currentLocale = defaultLocale,
fallbackLocale = defaultLocale,
) {
const lookupLocale = mapToDeviceSupportedLocale(currentLocale);
const msgTable = langTable[lookupLocale] || langTable[fallbackLocale] || {};
// Return a named function so it shows up in stack traces.
return function gettext(msgid: unknown): string {
const msgidString = String(msgid);
return msgTable[msgidString] || msgidString;
};
}