Initial draft of i18n API

This commit is contained in:
Jan Polák 2023-09-17 15:08:42 +02:00
parent d2252d4507
commit e9b8e403da
6 changed files with 85 additions and 0 deletions

View File

@ -23,6 +23,7 @@ function replaceFileContents(string, fileDir) {
const pattern = /%%([^%]+)%%|\/\/\s*include\s*"([^"]+)"/g;
return string.replace(pattern, (_, filename1, filename2) => {
if (filename2 != null) console.error("include is used!!!!!");
const filename = filename1 || filename2;
const filenamePath = path.join(fileDir, filename);
try {

71
site/i18n.js Executable file
View File

@ -0,0 +1,71 @@
// 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.
*/
const translations = {
cs: require("./locales/cs.json"),
de: require("./locales/de.json"),
en: require("./locales/en.json"),
};
/**
* @type {string[]}
*/
const locales = Object.keys(locales);
const defaultLocale = "de";
/** The currently selected locale. */
var currentLocale = defaultLocale;
/**
* Set the globally used locale.
* Expects a 2 character language code string, one from locales.
*/
function setLocale(locale) {
if (Object.hasOwn(locales, locale)) {
currentLocale = locale;
return true;
}
console.error("Attempted to setLocale to unsupported language: ", locale);
return false;
}
/**
* @param {string} key to translate
* @param {Object?} args arguments to substitute into the translated key
* @returns translated string
*/
function translate(key, args) {
let translation = locales[currentLocale][key];
if (translation === undefined) {
console.error("Untranslated key in ", currentLocale, ": ", key);
if (currentLocale != defaultLocale) {
translation = locales[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.setLocale = setLocale;
exports.locales = locales;
exports.translate = translate;

View File

@ -6,11 +6,24 @@ const { STORE_KEYS } = require("./model/stores");
const { ProgressBar } = require("./views/progress-bar");
const progressBar = new ProgressBar(STORE_KEYS.length);
const i18n = require("./i18n");
(async () => {
if (location.href.includes("heissepreise.github.io")) {
location.href = "https://heisse-preise.io";
}
// Find the most preferred supported language
for (const langCode in navigator.languages) {
// We don't do regional codes, so take just the language code
let lang = langCode.length >= 2 ? langCode.substring(0, 2) : null;
if (lang == null) continue;
if (i18n.locales.indexOf(lang) != -1) {
i18n.setLocale(lang);
break;
}
}
await model.load(() => progressBar.addStep());
const itemsFilter = document.querySelector("items-filter");
const itemsList = document.querySelector("items-list");

0
site/locales/cs.json Executable file
View File

0
site/locales/de.json Executable file
View File

0
site/locales/en.json Executable file
View File