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}`));
});
+21
View File
@@ -0,0 +1,21 @@
{
"settings.title.session": "Session settings",
"settings.title.general": "General settings",
"settings.scope.session": "Session",
"settings.scope.general": "General",
"settings.name": "Name",
"settings.notes": "Notes (chat purpose)",
"settings.sp": "System Prompt",
"settings.temp": "Temperature",
"settings.topp": "Top-p",
"settings.topk": "Top-k",
"settings.minp": "Min-p",
"settings.maxtokens": "Max tokens",
"settings.save": "Save",
"settings.cancel": "Cancel",
"settings.hint.session": "Changes apply only to this session.",
"settings.hint.general": "General defaults: apply only to future sessions.",
"settings.name.locked": "The name is locked after the first interaction.",
"settings.name.unlocked": "You can rename until you send the first message.",
"settings.error": "Error: {msg}"
}
+21
View File
@@ -0,0 +1,21 @@
{
"settings.title.session": "Impostazioni sessione",
"settings.title.general": "Impostazioni generali",
"settings.scope.session": "Sessione",
"settings.scope.general": "Generali",
"settings.name": "Nome",
"settings.notes": "Note (scopo della chat)",
"settings.sp": "System Prompt",
"settings.temp": "Temperature",
"settings.topp": "Top-p",
"settings.topk": "Top-k",
"settings.minp": "Min-p",
"settings.maxtokens": "Max token",
"settings.save": "Salva",
"settings.cancel": "Annulla",
"settings.hint.session": "Modifiche valide solo per questa sessione.",
"settings.hint.general": "Default generali: si applicano solo alle sessioni future.",
"settings.name.locked": "Il nome è bloccato dopo la prima interazione.",
"settings.name.unlocked": "Puoi rinominare finché non invii il primo messaggio.",
"settings.error": "Errore: {msg}"
}
+12 -12
View File
@@ -73,30 +73,30 @@
<div class="modal-card settings-card">
<h2 id="settings-title">Impostazioni</h2>
<div class="scope-toggle">
<button id="set-scope-session" class="btn ghost active">Sessione</button>
<button id="set-scope-general" class="btn ghost">Generali</button>
<button id="set-scope-session" class="btn ghost active" data-i18n="settings.scope.session">Sessione</button>
<button id="set-scope-general" class="btn ghost" data-i18n="settings.scope.general">Generali</button>
</div>
<p id="set-hint" class="muted"></p>
<label class="field" id="set-name-field"><span>Nome</span>
<label class="field" id="set-name-field"><span data-i18n="settings.name">Nome</span>
<input id="set-name" type="text" autocomplete="off" />
</label>
<p id="set-name-hint" class="muted small"></p>
<label class="field"><span>Note (scopo della chat)</span>
<label class="field"><span data-i18n="settings.notes">Note (scopo della chat)</span>
<textarea id="set-notes" rows="2"></textarea>
</label>
<label class="field"><span>System Prompt</span>
<label class="field"><span data-i18n="settings.sp">System Prompt</span>
<textarea id="set-sp" rows="4"></textarea>
</label>
<div class="field-grid">
<label class="field"><span>Temperature</span><input id="set-temp" type="number" step="0.1" min="0" /></label>
<label class="field"><span>Top-p</span><input id="set-topp" type="number" step="0.05" min="0" max="1" /></label>
<label class="field"><span>Top-k</span><input id="set-topk" type="number" step="1" min="0" /></label>
<label class="field"><span>Min-p</span><input id="set-minp" type="number" step="0.05" min="0" max="1" /></label>
<label class="field"><span>Max token</span><input id="set-maxtokens" type="number" step="1" min="1" /></label>
<label class="field"><span data-i18n="settings.temp">Temperature</span><input id="set-temp" type="number" step="0.1" min="0" /></label>
<label class="field"><span data-i18n="settings.topp">Top-p</span><input id="set-topp" type="number" step="0.05" min="0" max="1" /></label>
<label class="field"><span data-i18n="settings.topk">Top-k</span><input id="set-topk" type="number" step="1" min="0" /></label>
<label class="field"><span data-i18n="settings.minp">Min-p</span><input id="set-minp" type="number" step="0.05" min="0" max="1" /></label>
<label class="field"><span data-i18n="settings.maxtokens">Max token</span><input id="set-maxtokens" type="number" step="1" min="1" /></label>
</div>
<div class="dialog-actions">
<button id="set-cancel" class="btn">Annulla</button>
<button id="set-save" class="btn primary">Salva</button>
<button id="set-cancel" class="btn" data-i18n="settings.cancel">Annulla</button>
<button id="set-save" class="btn primary" data-i18n="settings.save">Salva</button>
</div>
</div>
</div>