Make i18n translation files load on demand

This commit is contained in:
Andrey Antukh
2025-11-25 10:47:48 +01:00
committed by Belén Albeza
parent b125c7b5a3
commit d1379c55f6
15 changed files with 124 additions and 103 deletions

View File

@@ -257,7 +257,7 @@ const markedOptions = {
marked.use(markedOptions);
async function readTranslations() {
export async function compileTranslations() {
const langs = [
"ar",
"ca",
@@ -294,9 +294,10 @@ async function readTranslations() {
["uk", "ukr_UA"],
"ha",
];
const result = {};
for (let lang of langs) {
const result = {};
let filename = `${lang}.po`;
if (l.isArray(lang)) {
filename = `${lang[1]}.po`;
@@ -315,11 +316,6 @@ async function readTranslations() {
for (let key of Object.keys(trdata)) {
if (key === "") continue;
const comments = trdata[key].comments || {};
if (l.isNil(result[key])) {
result[key] = {};
}
const isMarkdown = l.includes(comments.flag, "markdown");
const msgs = trdata[key].msgstr;
@@ -329,9 +325,9 @@ async function readTranslations() {
message = marked.parseInline(message);
}
result[key][lang] = message;
result[key] = message;
} else {
result[key][lang] = msgs.map((item) => {
result[key] = msgs.map((item) => {
if (isMarkdown) {
return marked.parseInline(item);
} else {
@@ -340,22 +336,12 @@ async function readTranslations() {
});
}
}
const esm = `export default ${JSON.stringify(result, null, 0)};\n`;
const outputDir = "resources/public/js/";
const outputFile = ph.join(outputDir, "translation." + lang + ".js");
await fs.writeFile(outputFile, esm);
}
return result;
}
function filterTranslations(translations, langs = [], keyFilter) {
const filteredEntries = Object.entries(translations)
.filter(([translationKey, _]) => keyFilter(translationKey))
.map(([translationKey, value]) => {
const langEntries = Object.entries(value).filter(([lang, _]) =>
langs.includes(lang),
);
return [translationKey, Object.fromEntries(langEntries)];
});
return Object.fromEntries(filteredEntries);
}
async function generateSvgSprite(files, prefix) {
@@ -407,14 +393,6 @@ async function generateTemplates() {
const isDebug = process.env.NODE_ENV !== "production";
await fs.mkdir("./resources/public/", { recursive: true });
let translations = await readTranslations();
const storybookTranslations = JSON.stringify(
filterTranslations(translations, ["en"], (key) =>
key.startsWith("labels."),
),
);
translations = JSON.stringify(translations);
const manifest = await readShadowManifest();
let content;
@@ -440,7 +418,6 @@ async function generateTemplates() {
"resources/templates/index.mustache",
{
manifest: manifest,
translations: JSON.stringify(translations),
isDebug,
},
partials,
@@ -468,7 +445,6 @@ async function generateTemplates() {
"resources/templates/preview-head.mustache",
{
manifest: manifest,
translations: JSON.stringify(storybookTranslations),
},
partials,
);
@@ -476,14 +452,12 @@ async function generateTemplates() {
content = await renderTemplate("resources/templates/render.mustache", {
manifest: manifest,
translations: JSON.stringify(translations),
});
await fs.writeFile("./resources/public/render.html", content);
content = await renderTemplate("resources/templates/rasterizer.mustache", {
manifest: manifest,
translations: JSON.stringify(translations),
});
await fs.writeFile("./resources/public/rasterizer.html", content);

View File

@@ -4,5 +4,6 @@ await h.compileStyles();
await h.copyAssets();
await h.copyWasmPlayground();
await h.compileSvgSprites();
await h.compileTranslations();
await h.compileTemplates();
await h.compilePolyfills();

View File

@@ -1,7 +1,11 @@
import fs from "node:fs/promises";
import * as h from "./_helpers.js";
await fs.mkdir("resources/public/js", {recursive: true});
await h.compileStorybookStyles();
await h.copyAssets();
await h.compileSvgSprites();
await h.compileTranslations();
await h.compileTemplates();
await h.compilePolyfills();

View File

@@ -52,6 +52,7 @@ await fs.mkdir("./resources/public/css/", { recursive: true });
await compileSassAll();
await h.copyAssets();
await h.copyWasmPlayground();
await h.compileTranslations();
await h.compileSvgSprites();
await h.compileTemplates();
await h.compilePolyfills();
@@ -81,7 +82,7 @@ h.watch("resources/templates", null, async function (path) {
log.info("watch: translations (~)");
h.watch("translations", null, async function (path) {
log.info("changed:", path);
await h.compileTemplates();
await h.compileTranslations();
});
log.info("watch: assets (~)");