diff --git a/README.md b/README.md index c7475e9..1ff7695 100644 --- a/README.md +++ b/README.md @@ -922,7 +922,7 @@ exists; it can be hidden, and brought back from the search box. **On a phone.** Below desktop width the top bar (logo and menu button) stays at the top of the screen so the menu is always within reach, with a menu button of at least 44 px; an open menu scrolls by itself (it no longer pushes the page down), its rows are finger-sized and the favourite stars are always visible; text is a little larger on a small screen, toasts run across the width and drawers fill it. The page asks for the whole screen (`viewport-fit=cover`) and keeps clear of the notch, the rounded corners and the home bar, which matters when it is installed to the home screen (the status bar then sits over the app). Tables turn into stacked cards, form fields are 16 px so a phone does not zoom in on them, and what is pressed does not wait for a second tap. Checked at 375 px: no page scrolls sideways. -**Swedish interface.** Press *Svenska* under *Language* in the user menu and the whole screen is drawn again in Swedish at once; the choice is kept on your account and follows you to any browser (without a choice the browser's language decides, and that guess is not stored). Swedish covers the menu, the shared parts (search, tables, dialogs, "5 min ago"), the sign-in page, and the headings, buttons, table columns, filters and messages of the pages used every day — Dashboard, Alerts, Servers, Updates, Maintenance, My work, Coming up, To do, Integrations and Settings → Display. Text with no Swedish yet is shown in English, so nothing looks broken. Not yet translated: what the server writes (alert messages, notifications, digests, reports, e-mail), the long page introductions and the less-used pages. How to add more is in docs/LANGUAGES.md. +**Swedish interface.** Press *Svenska* under *Language* in the user menu and the whole screen is drawn again in Swedish at once; the choice is kept on your account and follows you to any browser (without a choice the browser's language decides, and that guess is not stored). Swedish covers the menu, the shared parts (search, tables, dialogs, "5 min ago"), the sign-in page, and the headings, buttons, table columns, filters, status words, empty states and short messages of every page — the integration pages and the settings included. Text with no Swedish yet is shown in English, so nothing looks broken. Not yet translated: what the server writes (alert messages, notifications, digests, reports, e-mail), the longer explanatory paragraphs and sentences with numbers or names built into them. How to add more is in docs/LANGUAGES.md. **Accessibility.** There is a skip-to-content link, labelled landmarks, visible keyboard focus, and page changes are announced to screen readers; sortable table diff --git a/docs/LANGUAGES.md b/docs/LANGUAGES.md index 9fc3ba7..32aac15 100644 --- a/docs/LANGUAGES.md +++ b/docs/LANGUAGES.md @@ -35,7 +35,7 @@ tn(n, "{n} server", "{n} servers") // the one form for 1, the many form o - The Swedish is an entry in `web/src/i18n/sv.ts`: `"Save": "Spara"`. A text with no entry is shown as written, so nothing breaks while it is waiting for a translation. - **Keep the `{placeholders}` the same** in the Swedish as in the English. A test (`server/test/language.test.ts`) checks that, that no entry is empty, and that every page of the menu has a Swedish name. - Brand and product names (Proxmox, Synology, Docker …) are the same in both languages and have no entry. -- Do not call `t()` where a module is loaded (in a constant outside any component or function): it would be fixed to the language at that moment. Keep the English in the constant and call `t(label)` where it is drawn, as the menu (`layout/nav.tsx`) does. +- Do not call `t()` where a module is loaded (in a constant outside any component or function): it would be fixed to the language at that moment. Keep the English in the constant and call `t(label)` where it is drawn, as the menu (`layout/nav.tsx`) does. Where a constant holds a table of words, a getter does the same without touching where it is drawn: `{ get label() { return t("Running"); } }`. A test (`server/test/language.test.ts`) fails on any `t()` called outside a function. - Do not translate half a sentence that runs on into a link or more English: put the whole sentence in one `t()` with placeholders, or leave it all English. - A local variable called `t` hides the function; call such a variable something else (`todo`, `item`). - Changing the language re-draws the whole app (the tree is keyed on it), so no component needs to subscribe to anything. diff --git a/server/test/language.test.ts b/server/test/language.test.ts index f1d8ea1..cc08b39 100644 --- a/server/test/language.test.ts +++ b/server/test/language.test.ts @@ -88,6 +88,34 @@ describe("the Swedish text", () => { assert.deepEqual(labels.filter((l) => !(l in sv)), [], "labels with no Swedish name"); }); + test("no text is translated when a module loads: a table of words built then would stay in the language of the first paint", async () => { + const ts = (await import("typescript")).default; + const { readdirSync } = await import("node:fs"); + const root = new URL("../../web/src/", import.meta.url); + const files: URL[] = []; + const walk = (dir: URL) => { + for (const e of readdirSync(dir, { withFileTypes: true })) { + if (e.isDirectory()) { + if (e.name !== "i18n") walk(new URL(`${e.name}/`, dir)); + } else if (/\.tsx?$/.test(e.name)) files.push(new URL(e.name, dir)); + } + }; + walk(root); + const offenders: string[] = []; + for (const file of files) { + const text = readFileSync(file, "utf8"); + if (!/\btn?\(/.test(text)) continue; + const sf = ts.createSourceFile(file.pathname, text, ts.ScriptTarget.Latest, true, file.pathname.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS); + const visit = (node: import("typescript").Node, inside: boolean) => { + const fn = ts.isFunctionLike(node) || ts.isClassDeclaration(node); + if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && (node.expression.text === "t" || node.expression.text === "tn") && !inside) offenders.push(`${file.pathname.split("/web/src/")[1]}:${sf.getLineAndCharacterOfPosition(node.getStart()).line + 1}`); + ts.forEachChild(node, (c) => visit(c, inside || fn)); + }; + visit(sf, false); + } + assert.deepEqual(offenders, [], "t() called outside any function; use a getter (get label() { return t(\"…\"); }) or translate where it is shown"); + }); + test("translates the words the app's own lists show (the ranges, the density, the task states)", () => { for (const w of ["24 hours", "7 days", "30 days", "90 days", "1 year", "Compact", "Normal", "Roomy"]) assert.ok(w in sv, w); }); diff --git a/web/src/App.tsx b/web/src/App.tsx index 8ab8900..0a6a148 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -98,7 +98,7 @@ function RequireRole({ }) { if (roleRank[user.role] < roleRank[minRole]) { return ( -