-
Notifications
You must be signed in to change notification settings - Fork 5
/
i18n.js
62 lines (57 loc) · 1.87 KB
/
i18n.js
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
// Internationalization support
// This file is used both serverside and clientside.
/**
* Supported languages and their translations.
* Each file contains an object with key-value pairs.
*
* Argument substitution is supported through named arguments: translate("Hello {{name}}", {name: "World"})
*
* Plurals and arrays are currently not supported.
*
* If the key is missing in the current localization, it falls back to default localization
* and then to treating the key itself as the localization.
*
* @type {Object.<string, Object.<string, string>>}
*/
const translations = {
// sorted alphabetically
cs: require("./locales/cs.json"),
da: require("./locales/da.json"),
de: require("./locales/de.json"),
en: require("./locales/en.json"),
};
/**
* @type {string[]}
*/
const locales = Object.keys(translations);
/**
* @type {string}
*/
const defaultLocale = "da";
/**
* @param {!string} locale name of the language to use for translation, MUST be one of the supported languages
* @param {!string} key to translate
* @param {!Object.<string, string>} [args] arguments to substitute into the translated key
* @returns {string} translated string
*/
function translateWithLocale(locale, key, args) {
let translation = translations[locale][key];
if (translation === undefined) {
console.error("Untranslated key in ", locale, ": ", key);
if (locale != defaultLocale) {
translation = translations[defaultLocale][key] || key;
} else {
translation = key;
}
}
if (typeof args === "object") {
// Do argument substitution
for (const arg in args) {
translation = translation.replaceAll("{{" + arg + "}}", args[arg]);
}
}
return translation;
}
exports.defaultLocale = defaultLocale;
exports.locales = locales;
exports.translateWithLocale = translateWithLocale;