Localization fixes

This commit is contained in:
Jan Polák 2023-09-17 19:48:29 +02:00
parent f36b44fc42
commit 3b9fc43161
15 changed files with 69 additions and 67 deletions

View File

@ -4,7 +4,7 @@ const chokidar = require("chokidar");
const esbuild = require("esbuild");
const { exec } = require("child_process");
const { promisify } = require("util");
const i18n = require("./site/i18n");
const i18n = require("./i18n");
function deleteDirectory(directory) {
if (fs.existsSync(directory)) {
@ -64,16 +64,18 @@ function processFile(inputFile, outputFile, filter) {
const replacedData = replaceFileContents(data, path.dirname(inputFile), locale);
if (locale == i18n.defaultLocale) {
fs.writeFileSync(outputFile, replacedData);
console.log(`${inputFile} -> ${outputFile}`);
}
let pathWithLanguageCode = outputFile.substring(0, outputFile.length - extension.length) + "." + locale + extension;
fs.writeFileSync(pathWithLanguageCode, replacedData);
console.log(`${inputFile} -> ${pathWithLanguageCode}`);
}
} else {
const data = fs.readFileSync(inputFile);
if (filter(inputFile, false, data)) return;
fs.writeFileSync(outputFile, data);
console.log(`${inputFile} -> ${outputFile}`);
}
console.log(`${inputFile} -> ${outputFile}`);
}
/**
@ -167,6 +169,7 @@ async function bundle(inputDir, outputDir, watch) {
bundleHTML(inputDir, outputDir, false, watch, (filePath, isDir, data) => {
if (isDir) return false;
if (filePath.endsWith("style.css")) return true;
if (filePath.includes("/locales/")) return true;
if (filePath.endsWith(".js") && !filePath.includes("socket.io.js")) return true;
if (data.includes(`require("`)) return true;
return false;

View File

@ -29,38 +29,6 @@ const locales = Object.keys(translations);
*/
const defaultLocale = "de";
/**
* The currently selected locale.
* @type {string}
*/
var currentLocale = defaultLocale;
/**
* Set the globally used locale.
* Expects a 2 character language code string, one from locales.
* @param {string} locale
*/
function setLocale(locale) {
if (locales.includes(locale)) {
console.log("Locale changed to " + locale);
currentLocale = locale;
return true;
}
console.error("Attempted to setLocale to unsupported language: ", locale);
return false;
}
/**
* Translates the key using the current global locale.
*
* @param {!string} key to translate
* @param {!Object.<string, string>} [args] arguments to substitute into the translated key
* @returns {string} translated string
*/
function translate(key, args) {
return translateWithLocale(currentLocale, key, args);
}
/**
* @param {!string} locale name of the language to use for translation, MUST be one of the supported languages
* @param {!string} key to translate
@ -88,9 +56,6 @@ function translateWithLocale(locale, key, args) {
return translation;
}
exports.setLocale = setLocale;
exports.defaultLocale = defaultLocale;
exports.locales = locales;
exports.translate = translate;
exports.__ = translate;
exports.translateWithLocale = translateWithLocale;

View File

@ -78,8 +78,9 @@
"ItemsList_Name": "Název",
"ItemsList_Preis": "Cena",
"Cart_Teilen": "Sdílet",
"Cart_Teilen": "Detail",
"Cart_Speichern": "Uložit",
"Cart_Warenkorb {{name}}": "Nákupní košík {{name}}",
"Cart_Warenkorb '{{name}}' existiert bereits. Bitte einen anderen Namen für den zu speichernden Warenkorb eingeben": "Nákupní košík '{{name}}' již existuje. Zadejte prosím jiný název",
"Cart_Warenkorb '{{name}}' existiert nicht.": "Nákupní košík '{{name}}' neexistuje.",
"Cart_Artikel": "Položka",

View File

@ -80,6 +80,7 @@
"Cart_Teilen": "Teilen",
"Cart_Speichern": "Speichern",
"Cart_Warenkorb {{name}}": "Warenkorb {{name}}",
"Cart_Warenkorb '{{name}}' existiert bereits. Bitte einen anderen Namen für den zu speichernden Warenkorb eingeben": "Warenkorb '{{name}}' existiert bereits. Bitte einen anderen Namen für den zu speichernden Warenkorb eingeben",
"Cart_Warenkorb '{{name}}' existiert nicht.": "Warenkorb '{{name}}' existiert nicht.",
"Cart_Artikel": "Artikel",

View File

@ -78,8 +78,9 @@
"ItemsList_Name": "Name",
"ItemsList_Preis": "Price",
"Cart_Teilen": "Share",
"Cart_Teilen": "Detail",
"Cart_Speichern": "Save",
"Cart_Warenkorb {{name}}": "Shopping cart {{name}}",
"Cart_Warenkorb '{{name}}' existiert bereits. Bitte einen anderen Namen für den zu speichernden Warenkorb eingeben": "Shopping cart '{{name}}' already exists. Please enter a different name for the shopping cart to be saved",
"Cart_Warenkorb '{{name}}' existiert nicht.": "Shopping cart '{{name}}' does not exist.",
"Cart_Artikel": "Item",

View File

@ -7,7 +7,7 @@ const csv = require("./site/js/misc");
const chokidar = require("chokidar");
const express = require("express");
const compression = require("compression");
const i18n = require("./site/i18n");
const i18n = require("./i18n");
function copyItemsToSite(dataDir) {
const items = analysis.readJSON(`${dataDir}/latest-canonical.json.${analysis.FILE_COMPRESSOR}`);

46
site/browser_i18n.js Normal file
View File

@ -0,0 +1,46 @@
const i18n = require("../i18n");
/**
* The currently selected locale.
* @type {?string}
*/
var currentLocale = i18n.defaultLocale;
/**
* Set the globally used locale.
* Expects a 2 character language code string, one from locales.
* @param {string} locale
*/
function setLocale(locale) {
if (i18n.locales.includes(locale)) {
console.log("Locale changed to " + locale);
currentLocale = locale;
return true;
}
console.error("Attempted to setLocale to unsupported language: ", locale);
return false;
}
/**
* Translates the key using the current global locale.
*
* @param {!string} key to translate
* @param {!Object.<string, string>} [args] arguments to substitute into the translated key
* @returns {string} translated string
*/
function translate(key, args) {
return i18n.translateWithLocale(currentLocale, key, args);
}
// Find the most preferred supported language
for (const langCode of 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.includes(lang)) {
setLocale(lang);
break;
}
}
exports.__ = translate;

View File

@ -6,7 +6,7 @@ const { STORE_KEYS, stores } = require("./model/stores");
require("./views");
const { ProgressBar } = require("./views/progress-bar");
const progressBar = new ProgressBar(STORE_KEYS.length);
const { __ } = require("./i18n");
const { __ } = require("./browser_i18n");
let carts = null;

View File

@ -1,7 +1,7 @@
const { downloadJSON, today } = require("./js/misc");
const model = require("./model");
require("./views");
const { __ } = require("./i18n");
const { __ } = require("./browser_i18n");
const { STORE_KEYS } = require("./model/stores");
const { ProgressBar } = require("./views/progress-bar");

View File

@ -1,23 +1,8 @@
const i18n = require("./i18n");
// Process redirects and localization before running anything else
(() => {
if (location.href.includes("heissepreise.github.io")) {
location.href = "https://heisse-preise.io";
return;
}
// Find the most preferred supported language
for (const langCode of 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.includes(lang)) {
i18n.setLocale(lang);
break;
}
}
})();
// Process redirects before running anything else
if (location.href.includes("heissepreise.github.io")) {
location.href = "https://heisse-preise.io";
return;
}
const { getQueryParameter } = require("./js/misc");
const model = require("./model");

View File

@ -3,7 +3,7 @@ const { View } = require("./views/view");
const { Settings } = require("./model/settings");
require("./js/misc");
require("./views/custom-checkbox");
const { __ } = require("./i18n");
const { __ } = require("./browser_i18n");
class SettingsView extends View {
constructor() {

View File

@ -1,6 +1,6 @@
const { downloadJSON, dom } = require("../js/misc");
const { View } = require("./view");
const { __ } = require("../i18n");
const { __ } = require("../browser_i18n");
class CartsList extends View {
constructor() {

View File

@ -1,7 +1,7 @@
const { STORE_KEYS } = require("../model/stores");
const { settings } = require("../model");
const { today, log, deltaTime, uniqueDates, calculateItemPriceTimeSeries } = require("../js/misc");
const { __ } = require("../i18n");
const { __ } = require("../browser_i18n");
const { View } = require("./view");
require("./custom-checkbox");
const moment = require("moment");

View File

@ -3,7 +3,7 @@ const { stores, STORE_KEYS, BUDGET_BRANDS } = require("../model/stores");
const { fromCategoryCode, categories } = require("../model/categories");
const { settings } = require("../model");
const { View } = require("./view");
const { __ } = require("../i18n");
const { __ } = require("../browser_i18n");
class ItemsFilter extends View {
constructor() {

View File

@ -14,7 +14,7 @@ const { vectorizeItems, similaritySortItems } = require("../js/knn");
const { stores } = require("../model/stores");
const { View } = require("./view");
const { ItemsChart } = require("./items-chart");
const { __ } = require("../i18n");
const { __ } = require("../browser_i18n");
class ItemsList extends View {
static priceTypeId = 0;