forked from samuelmeuli/mini-diary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.ts
97 lines (86 loc) · 2.87 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
import { app } from "electron";
import translationsDe from "./translations/de";
import translationsEl from "./translations/el";
import translationsEn from "./translations/en";
import translationsEs from "./translations/es";
import translationsFr from "./translations/fr";
import translationsIs from "./translations/is";
import translationsPt from "./translations/pt";
import translationsTr from "./translations/tr";
import translationsZh from "./translations/zh";
import { Translations } from "./translations/types";
const ALL_TRANSLATIONS: Record<string, Translations> = {
de: translationsDe,
el: translationsEl,
en: translationsEn,
es: translationsEs,
fr: translationsFr,
is: translationsIs,
pt: translationsPt,
tr: translationsTr,
zh: translationsZh,
};
const DEFAULT_LANG = "en";
const systemLang = app.getLocale();
let lang: string; // Language used by app (e.g. 'en-US'), used for dates/calendar
let langNoRegion: string; // Language used by app without region string (e.g. 'en'), used for translations
let translations: Translations; // String translations for langNoRegion
/**
* Return language to use for the app
*/
export function getUsedLang(): string {
return lang;
}
/**
* Determine language to use for the app (use system language if translations are available,
* otherwise fall back to default language
*/
function setUsedLang(): void {
const systemLangNoRegion = systemLang.split("-")[0];
const defaultTranslations = ALL_TRANSLATIONS[DEFAULT_LANG];
if (systemLangNoRegion in ALL_TRANSLATIONS) {
// Use system language if translations are available
lang = systemLang;
langNoRegion = systemLangNoRegion;
translations = {
...defaultTranslations,
...ALL_TRANSLATIONS[langNoRegion],
};
} else {
// Otherwise, fall back to default language
lang = DEFAULT_LANG;
langNoRegion = DEFAULT_LANG;
translations = defaultTranslations;
}
}
/**
* Return translation for string with the provided translation key. Perform string substitutions if
* required
*/
export function translate(i18nKey: string, substitutions?: Record<string, string>): string {
if (!(i18nKey in translations)) {
console.error(`Missing translation of i18nKey "${i18nKey}"`);
return i18nKey;
}
// Return translation if no `substitutions` object is provided
let translation = translations[i18nKey];
if (!substitutions) {
return translation;
}
// Perform string substitutions if `substitutions` object is provided
// Example:
// Translation definition: { test: 'Hello {var}' }
// Function call: translate('test', { var: 'World' })
// Result: 'Hello World'
Object.entries(substitutions).forEach(([toReplace, replacement]) => {
translation = translation.replace(new RegExp(`{${toReplace}}`, "g"), replacement);
});
return translation;
}
/**
* Return all translations for the detected language
*/
export function getTranslations(): Translations {
return translations;
}
setUsedLang();