DIK-40: переключатель языка на странице «Все заведения», текст меняется сразу
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a689614d81
commit
3d5e3cf670
@ -10,6 +10,9 @@ body{font:15px/1.4 system-ui,sans-serif;margin:0;background:var(--bg);color:var(
|
||||
header{display:flex;align-items:center;gap:12px;padding:12px 16px;background:var(--accent);color:#fff}
|
||||
header h1{font-size:19px;margin:0;flex:1}
|
||||
header a{color:#fff;font-weight:600}
|
||||
.langs{display:flex;gap:4px}
|
||||
.langs button{min-width:40px;padding:6px 8px;border:1px solid #fff8;border-radius:8px;background:none;color:#fff;font:inherit;font-size:14px;font-weight:700;cursor:pointer}
|
||||
.langs button.on{background:#fff;color:var(--accent);border-color:#fff}
|
||||
main{padding:16px}
|
||||
p{color:var(--mute);margin:0 0 8px}
|
||||
.wrap{overflow-x:auto;border:1px solid var(--line);border-radius:12px;background:var(--card)}
|
||||
@ -19,7 +22,7 @@ th{position:sticky;top:0;background:var(--card);font-size:13px;color:var(--mute)
|
||||
.src{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:12px 16px;margin:0 0 16px}.src h2{font-size:16px;margin:0 0 8px}.src ul{margin:0;padding-left:20px}.src li{margin:4px 0}
|
||||
.msg{padding:12px;background:#fff3bf;color:#4a3b00;border-radius:12px}
|
||||
</style></head><body>
|
||||
<header><h1 data-i="baseTitle"></h1><a href="/" data-i="backHome"></a></header>
|
||||
<header><h1 data-i="baseTitle"></h1><div class="langs"><button data-l="ru" class="on" aria-pressed="true" title="Русский" aria-label="Русский" lang="ru">RU</button><button data-l="kk" title="Қазақша" aria-label="Қазақша" lang="kk" aria-pressed="false">KK</button><button data-l="en" title="English" aria-label="English" lang="en" aria-pressed="false">EN</button></div><a href="/" data-i="backHome"></a></header>
|
||||
<main>
|
||||
<p data-i="baseIntro"></p><p id="count"></p>
|
||||
<section id="sources" class="src"><h2 data-i="srcTitle"></h2><p data-i="srcIntro"></p>
|
||||
|
||||
@ -1,21 +1,22 @@
|
||||
const LANGS = ["ru", "kk", "en"];
|
||||
const saved = () => { try { return localStorage.getItem("lang"); } catch { return null; } };
|
||||
const lang = LANGS.includes(saved()) ? saved() : "ru";
|
||||
const t = await (await fetch(`i18n/${lang}.json`)).json();
|
||||
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k]);
|
||||
const date = (iso) => (iso ? iso.split("-").reverse().join(".") : "");
|
||||
document.documentElement.lang = lang;
|
||||
document.title = t.baseTitle;
|
||||
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
||||
let t = {}, list = null, failed = false;
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const COLS = ["colName", "colDistrict", "colAddress", "colCoords", "colLunch", "colPromo", "colChecked"];
|
||||
document.getElementById("head").replaceChildren(...COLS.map((k) => { const th = document.createElement("th"); th.textContent = t[k]; return th; }));
|
||||
try {
|
||||
const r = await fetch("/api/base");
|
||||
if (!r.ok) throw new Error();
|
||||
const list = await r.json();
|
||||
document.getElementById("count").textContent = fmt(t.baseCount, { n: list.length });
|
||||
const body = document.getElementById("rows");
|
||||
if (!list.length) { const e = document.getElementById("err"); e.textContent = t.listPreparing; e.hidden = false; }
|
||||
|
||||
function render() {
|
||||
document.title = t.baseTitle;
|
||||
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
||||
document.querySelectorAll("[data-al]").forEach((e) => e.setAttribute("aria-label", t[e.dataset.al]));
|
||||
$("head").replaceChildren(...COLS.map((k) => { const th = document.createElement("th"); th.textContent = t[k]; return th; }));
|
||||
const body = $("rows"), e = $("err");
|
||||
body.replaceChildren(); e.hidden = true; $("count").textContent = "";
|
||||
if (failed) { e.textContent = t.baseError; e.hidden = false; return; }
|
||||
if (!list) return;
|
||||
$("count").textContent = fmt(t.baseCount, { n: list.length });
|
||||
if (!list.length) { e.textContent = t.listPreparing; e.hidden = false; }
|
||||
for (const v of list) {
|
||||
const promo = v.promo ? (t.promoTexts?.[v.promo.text] || v.promo.text) + (v.promo.until ? ` (${date(v.promo.until)})` : "") : t.noPromo;
|
||||
const lunch = v.lunch ? fmt(t.baseLunch, v.lunch) : t.noLunch;
|
||||
@ -25,6 +26,23 @@ try {
|
||||
}
|
||||
body.append(tr);
|
||||
}
|
||||
} catch {
|
||||
const e = document.getElementById("err"); e.textContent = t.baseError; e.hidden = false;
|
||||
}
|
||||
|
||||
async function setLang(l) {
|
||||
try { localStorage.setItem("lang", l); } catch {}
|
||||
t = await (await fetch(`i18n/${l}.json`)).json();
|
||||
document.documentElement.lang = l;
|
||||
document.querySelectorAll("[data-l]").forEach((b) => {
|
||||
b.classList.toggle("on", b.dataset.l === l); b.setAttribute("aria-pressed", String(b.dataset.l === l));
|
||||
});
|
||||
render();
|
||||
}
|
||||
document.querySelectorAll("[data-l]").forEach((b) => (b.onclick = () => setLang(b.dataset.l)));
|
||||
|
||||
await setLang(LANGS.includes(saved()) ? saved() : "ru");
|
||||
try {
|
||||
const r = await fetch("/api/base");
|
||||
if (!r.ok) throw new Error();
|
||||
list = await r.json();
|
||||
} catch { failed = true; }
|
||||
render();
|
||||
|
||||
@ -96,3 +96,11 @@ test("Сценарий: сообщение переводится при сме
|
||||
assert.match(app, /setAttribute\("aria-pressed", String\(b\.dataset\.l === l\)\)/);
|
||||
assert.match(app, /setAttribute\("aria-expanded", String\(open\)\)/);
|
||||
});
|
||||
|
||||
test("Сценарий: на странице «Все заведения» есть переключатель языка и текст меняется сразу (DIK-40)", () => {
|
||||
const html = fs.readFileSync("public/base.html", "utf8");
|
||||
const base = fs.readFileSync("public/base.js", "utf8");
|
||||
for (const l of ["ru", "kk", "en"]) assert.match(html, new RegExp(`data-l="${l}"`));
|
||||
assert.match(base, /async function setLang/);
|
||||
assert.match(base, /localStorage\.setItem\("lang"/);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user