Add lightweight i18n foundation; route new strings through catalogs

- Backend src/i18n.rs: embedded locales/<lang>.json, t(key,params) with {placeholder} interpolation, process-wide language from config.language (default it), English-then-key fallback. Applied to the identity reminder and name-lock/empty errors.

- Frontend: web/assets/locales/<lang>.json + t()/loadI18n()/applyI18n() (data-i18n attributes); language follows the browser; async bootstrap. Applied to the settings panel.

- Convention: new user-facing and model-facing strings use the catalog; wire/JSON/identifiers never translated. Existing strings migrate in a later round. it+en catalogs; 115 tests; verified live (catalog error messages) + node --check + HTTP. README documents the convention.
This commit is contained in:
adm committed 2026-11-25 16:05:29 +01:00
1 parent 73b665d1a7
commit 498e3dbc1b
12 files changed
+248 -27

No files matched your search

+40 -8
View File
@@ -7,6 +7,34 @@ const $ = (sel) => document.querySelector(sel);
const state = { sessions: [], current: null, busy: false, agent: false };
// ---- i18n: new user-facing strings go through t(key, params) --------------
// Catalogs are served at /assets/locales/<lang>.json; language follows the
// browser. Static HTML uses data-i18n attributes; dynamic strings use t().
const I18N = { lang: "it", map: {} };
function t(key, params = {}) {
let s = I18N.map[key] ?? key;
for (const [k, v] of Object.entries(params)) s = s.replaceAll(`{${k}}`, v);
return s;
}
async function loadI18n() {
const nav = (navigator.language || "it").slice(0, 2).toLowerCase();
const lang = ["it", "en"].includes(nav) ? nav : "it";
for (const l of [lang, "it"]) {
try {
const r = await fetch(`/assets/locales/${l}.json`);
if (r.ok) { I18N.map = await r.json(); I18N.lang = l; return; }
} catch { /* try next */ }
}
}
function applyI18n() {
document.querySelectorAll("[data-i18n]").forEach((n) => {
n.textContent = t(n.getAttribute("data-i18n"));
});
document.querySelectorAll("[data-i18n-ph]").forEach((n) => {
n.placeholder = t(n.getAttribute("data-i18n-ph"));
});
}
// ---- API helpers ----------------------------------------------------------
async function getJSON(url) {
const r = await fetch(url);
@@ -382,11 +410,11 @@ function fillSettingsForm(data, scope) {
const locked = sess ? sess.name_locked : false;
$("#set-name").disabled = locked;
$("#set-name-hint").textContent = locked
? "Il nome è bloccato dopo la prima interazione."
: "Puoi rinominare finché non invii il primo messaggio.";
$("#set-hint").textContent = "Modifiche valide solo per questa sessione.";
? t("settings.name.locked")
: t("settings.name.unlocked");
$("#set-hint").textContent = t("settings.hint.session");
} else {
$("#set-hint").textContent = "Default generali: si applicano solo alle sessioni future.";
$("#set-hint").textContent = t("settings.hint.general");
}
$("#set-notes").value = data.notes || "";
$("#set-sp").value = data.system_prompt || "";
@@ -416,7 +444,7 @@ async function openSettings(scope) {
$("#set-scope-session").classList.toggle("active", scope === "session");
$("#set-scope-general").classList.toggle("active", scope === "general");
$("#settings-title").textContent =
scope === "session" ? "Impostazioni sessione" : "Impostazioni generali";
scope === "session" ? t("settings.title.session") : t("settings.title.general");
settingsBase =
scope === "session"
? { ...(currentSession()?.settings || {}) }
@@ -445,7 +473,7 @@ async function saveSettings() {
}
$("#settings-modal").hidden = true;
} catch (e) {
$("#set-hint").textContent = `Errore: ${e.message}`;
$("#set-hint").textContent = t("settings.error", { msg: e.message });
}
}
@@ -567,8 +595,12 @@ function wire() {
});
}
wire();
loadSessions().catch((e) => {
(async () => {
await loadI18n();
applyI18n();
wire();
await loadSessions();
})().catch((e) => {
$("#transcript").textContent = "";
$("#transcript").appendChild(el("div", "empty", `Impossibile caricare: ${e.message}`));
});