Skip to content

Build your personal knowledge base with Trilium Notes

fix(i18n): rename open_all_too_many, which Weblate read as a plural form

Elian Dorana7635841922a
diff --git a/apps/client/src/services/i18n.spec.ts b/apps/client/src/services/i18n.spec.ts
index 0e6a4f52c6..a155dc8f31 100644
--- a/apps/client/src/services/i18n.spec.ts
+++ b/apps/client/src/services/i18n.spec.ts
@@ -1,5 +1,5 @@
import { MESSAGE_KEY_PREFIX, MESSAGE_OVERRIDES, slugify } from "@triliumnext/ckeditor5";
-import { dayjs, findDuplicateJsonKeys, findPluralKeyConflicts, LOCALES } from "@triliumnext/commons";
+import { dayjs, findDuplicateJsonKeys, findMalformedPluralGroups, findPluralKeyConflicts, LOCALES } from "@triliumnext/commons";
import { readdirSync, readFileSync } from "fs";
import i18next from "i18next";
import { join } from "path";
@@ -64,6 +64,38 @@ describe("i18n", () => {
}
});
+ // A `_one`/`_many`/… suffix is not part of a key: it is a plural form of the key before it,
+ // to both i18next and Weblate. A key that merely reads that way — `open_all_too_many` for
+ // "too many notes to open" — silently becomes one form of a plural whose other forms nobody
+ // wrote, which Weblate counts as untranslated in the *source* language and then propagates,
+ // empty, to every locale it manages.
+ //
+ // Only the English catalogs are checked, since they are the source Weblate imports: a
+ // translation is free to leave a form empty until someone writes it, and several do.
+ it("declares the plural forms English uses, and no others", () => {
+ // Everything the setup wizard and login page need is in a catalog of its own, and the
+ // server catalog is the one every non-browser runtime reads — Weblate imports all three.
+ const catalogs = [
+ join(__dirname, "..", "translations", "en", "translation.json"),
+ join(__dirname, "..", "translations", "en", "entry.json"),
+ join(__dirname, "..", "..", "..", "..", "apps", "server", "src", "assets", "translations", "en", "server.json")
+ ];
+ const categories = new Intl.PluralRules("en").resolvedOptions().pluralCategories;
+
+ for (const catalogPath of catalogs) {
+ const malformed = findMalformedPluralGroups(
+ JSON.parse(readFileSync(catalogPath, { encoding: "utf-8" })), categories);
+
+ expect(
+ malformed,
+ `Plural groups in "${catalogPath}" that do not match English's ${categories.join("/")} forms — a key that is not a plural must not end in one of those:\n${
+ malformed.map((g) => ` - "${g.path}" declares ${g.present.map((f) => `_${f}`).join(", ")}${
+ g.missing.length ? `, missing ${g.missing.map((f) => `_${f}`).join(", ")}` : ""}${
+ g.unexpected.length ? `, unexpected ${g.unexpected.map((f) => `_${f}`).join(", ")}` : ""}`).join("\n")}`
+ ).toEqual([]);
+ }
+ });
+
// i18next HTML-escapes interpolated values by default, so a value carrying a slash, an
// apostrophe or a quote reaches the user as `blobs/9`. Almost everything we interpolate is
// rendered as text (a Preact child, a title attribute), where that escaping only produces
diff --git a/apps/client/src/translations/ar/translation.json b/apps/client/src/translations/ar/translation.json
index c18bacd362..28e22cb8bb 100644
--- a/apps/client/src/translations/ar/translation.json
+++ b/apps/client/src/translations/ar/translation.json
@@ -403,13 +403,7 @@
"open_all_confirm_many": "",
"open_all_confirm_other": "",
"expand_tooltip": "يوسّع العناصر الفرعية المباشرة لهذه المجموعة (مستوى واحد فقط). لمزيد من الخيارات، اضغط السهم الموجود على اليمين.",
- "open_all_in_tabs_tooltip": "فتح جميع النتائج في علامات تبويب جديدة",
- "open_all_too_zero": "book_properties.open_all_too",
- "open_all_too_one": "book_properties.open_all_too",
- "open_all_too_two": "book_properties.open_all_too",
- "open_all_too_few": "book_properties.open_all_too",
- "open_all_too_many": "book_properties.open_all_too",
- "open_all_too_other": "book_properties.open_all_too"
+ "open_all_in_tabs_tooltip": "فتح جميع النتائج في علامات تبويب جديدة"
},
"file_properties": {
"download": "تنزيل",
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 8642fc15e1..eb1629b249 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1289,7 +1289,7 @@
"open_all_in_tabs_tooltip": "Open all results in new tabs",
"open_all_confirm_one": "This will open {{count}} note in new tabs. Continue?",
"open_all_confirm_other": "This will open {{count}} notes in new tabs. Continue?",
- "open_all_too_many": "Too many notes to open at once ({{count}}). The maximum is {{max}}, try narrowing the results first."
+ "open_all_limit_exceeded": "Too many notes to open at once ({{count}}). The maximum is {{max}}, try narrowing the results first."
},
"edited_notes": {
"no_edited_notes_found": "No edited notes on this day yet...",
diff --git a/apps/client/src/translations/es/translation.json b/apps/client/src/translations/es/translation.json
index cc4cba8726..183e0906d3 100644
--- a/apps/client/src/translations/es/translation.json
+++ b/apps/client/src/translations/es/translation.json
@@ -1056,9 +1056,7 @@
"open_all_confirm_one": "Esto abrirá {{count}} nota en una nueva pestaña. ¿Continuar?",
"open_all_confirm_many": "Esto abrirá {{count}} notas en nuevas pestañas. ¿Continuar?",
"open_all_confirm_other": "Esto abrirá {{count}} notas en nuevas pestañas. ¿Continuar?",
- "open_all_too_one": "",
- "open_all_too_many": "Demasiadas notas para abrir a la vez ({{count}}). El máximo es {{max}}; intente acotar los resultados primero.",
- "open_all_too_other": "",
+ "open_all_limit_exceeded": "Demasiadas notas para abrir a la vez ({{count}}). El máximo es {{max}}; intente acotar los resultados primero.",
"dashboard": "Panel"
},
"edited_notes": {
diff --git a/apps/client/src/translations/pt_br/translation.json b/apps/client/src/translations/pt_br/translation.json
index 6050bc2b74..8c1df7c37a 100644
--- a/apps/client/src/translations/pt_br/translation.json
+++ b/apps/client/src/translations/pt_br/translation.json
@@ -1173,9 +1173,7 @@
"open_all_confirm_one": "Isso abrirá {{count}} nota em novas abas. Continuar?",
"open_all_confirm_many": "",
"open_all_confirm_other": "Isso abrirá {{count}} notas em novas abas. Continuar?",
- "open_all_too_one": "",
- "open_all_too_many": "Notas demais para abrir de uma vez ({{count}}). O máximo é {{max}}, tente estreitar os resultados primeiro.",
- "open_all_too_other": ""
+ "open_all_limit_exceeded": "Notas demais para abrir de uma vez ({{count}}). O máximo é {{max}}, tente estreitar os resultados primeiro."
},
"edited_notes": {
"no_edited_notes_found": "Ainda não há nenhuma nota editada neste dia…",
diff --git a/apps/client/src/translations/ru/translation.json b/apps/client/src/translations/ru/translation.json
index 07f95c39bb..86754af40d 100644
--- a/apps/client/src/translations/ru/translation.json
+++ b/apps/client/src/translations/ru/translation.json
@@ -1084,9 +1084,7 @@
"open_all_confirm_one": "Это откроет заметки ({{count}}) в новых вкладках. Продолжить?",
"open_all_confirm_few": "",
"open_all_confirm_many": "",
- "open_all_too_one": "Слишком много результатов ({{count}}). Максимум — {{max}}.",
- "open_all_too_few": "Слишком много результатов ({{count}}). Максимум — {{max}}.",
- "open_all_too_many": "Слишком много результатов ({{count}}). Максимум — {{max}}."
+ "open_all_limit_exceeded": "Слишком много результатов ({{count}}). Максимум — {{max}}."
},
"edited_notes": {
"deleted": "(удалено)",
diff --git a/apps/client/src/widgets/note_bars/CollectionProperties.tsx b/apps/client/src/widgets/note_bars/CollectionProperties.tsx
index f9d5568bc7..bfcd52b30a 100644
--- a/apps/client/src/widgets/note_bars/CollectionProperties.tsx
+++ b/apps/client/src/widgets/note_bars/CollectionProperties.tsx
@@ -81,7 +81,7 @@ function OpenAllButton({ note, isOpening, setIsOpening }: {
if (count === 0) return;
if (count > MAX_OPEN_TABS) {
- toast.showError(t("book_properties.open_all_too_many", { count, max: MAX_OPEN_TABS }));
+ toast.showError(t("book_properties.open_all_limit_exceeded", { count, max: MAX_OPEN_TABS }));
return;
}
diff --git a/packages/commons/src/lib/test-utils.spec.ts b/packages/commons/src/lib/test-utils.spec.ts
index b96663ecd2..440da50b5c 100644
--- a/packages/commons/src/lib/test-utils.spec.ts
+++ b/packages/commons/src/lib/test-utils.spec.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
-import { findDuplicateJsonKeys, findPluralKeyConflicts, sleepFor, trimIndentation } from "./test-utils.js";
+import { findDuplicateJsonKeys, findMalformedPluralGroups, findPluralKeyConflicts, sleepFor, trimIndentation } from "./test-utils.js";
describe("Utils", () => {
it("trims indentation", () => {
@@ -133,4 +133,54 @@ Hello
expect(findPluralKeyConflicts([ { a_other: {} } ])).toEqual([]);
});
});
+
+ describe("findMalformedPluralGroups", () => {
+ const ENGLISH = [ "one", "other" ];
+
+ it("accepts a complete group, and keys that merely end in a similar word", () => {
+ expect(findMalformedPluralGroups({
+ item_one: "One item",
+ item_other: "{{count}} items",
+ mother: "Mother",
+ brother_in_law: "Brother in law"
+ }, ENGLISH)).toEqual([]);
+ });
+
+ it("reports a key that is not a plural but ends in a plural suffix", () => {
+ expect(findMalformedPluralGroups({
+ open_all_too_many: "Too many notes to open at once."
+ }, ENGLISH)).toEqual([
+ { path: "open_all_too", present: [ "many" ], missing: [ "one", "other" ], unexpected: [ "many" ] }
+ ]);
+ });
+
+ it("reports a group missing one of the forms the language declines", () => {
+ expect(findMalformedPluralGroups({ item_other: "{{count}} items" }, ENGLISH)).toEqual([
+ { path: "item", present: [ "other" ], missing: [ "one" ], unexpected: [] }
+ ]);
+ });
+
+ it("gathers the forms of a group and reports them under the base key at any depth", () => {
+ expect(findMalformedPluralGroups({
+ section: { count_one: "one", count_few: "few", count_many: "many" }
+ }, ENGLISH)).toEqual([
+ { path: "section.count", present: [ "one", "few", "many" ], missing: [ "other" ], unexpected: [ "few", "many" ] }
+ ]);
+ });
+
+ it("follows the categories it is given rather than English's", () => {
+ const russian = { count_one: "one", count_few: "few", count_many: "many", count_other: "other" };
+ expect(findMalformedPluralGroups(russian, [ "one", "few", "many", "other" ])).toEqual([]);
+ });
+
+ it("leaves a form holding something other than a string to findPluralKeyConflicts", () => {
+ expect(findMalformedPluralGroups({ settings_other: { ui: "User interface" } }, ENGLISH)).toEqual([]);
+ });
+
+ it("returns nothing for values that are not objects", () => {
+ expect(findMalformedPluralGroups("a string", ENGLISH)).toEqual([]);
+ expect(findMalformedPluralGroups(null, ENGLISH)).toEqual([]);
+ expect(findMalformedPluralGroups([ { a_other: "a" } ], ENGLISH)).toEqual([]);
+ });
+ });
});
diff --git a/packages/commons/src/lib/test-utils.ts b/packages/commons/src/lib/test-utils.ts
index 312fec6976..61c830898a 100644
--- a/packages/commons/src/lib/test-utils.ts
+++ b/packages/commons/src/lib/test-utils.ts
@@ -106,6 +106,65 @@ export function findPluralKeyConflicts(value: unknown, path = ""): Array<{ path:
return conflicts;
}
+/**
+ * Scans a parsed translation file for plural groups whose forms do not match the plural
+ * categories of the language the file holds.
+ *
+ * Both i18next and Weblate read a `_zero`/`_one`/`_two`/`_few`/`_many`/`_other` suffix as a
+ * plural form of the key preceding it, and gather all the forms of a key into one unit. A key
+ * that merely happens to end that way — `open_all_too_many` for "too many notes to open" — is
+ * therefore not a key of its own: it declares the `many` form of `open_all_too`, leaving the
+ * forms the language actually uses empty. Weblate then counts the whole unit as untranslated,
+ * which in the source language means it can never reach 100%, and it propagates the phantom
+ * key with its empty forms to every locale it manages.
+ *
+ * The reverse — a plural group missing one of the forms its language declines — is worth the
+ * same report: the missing form falls back to another one and reads wrong for those counts.
+ *
+ * @param value the parsed translation file (or any subtree of it).
+ * @param categories the plural categories of the file's language, e.g. `["one", "other"]` for
+ * English — `Intl.PluralRules` is the authority on these.
+ * @param path the dotted path of `value`, used to build the reported paths.
+ * @returns one entry per offending group, with the forms it declares and how they differ from
+ * the expected ones.
+ */
+export function findMalformedPluralGroups(
+ value: unknown,
+ categories: string[],
+ path = ""
+): Array<{ path: string; present: string[]; missing: string[]; unexpected: string[] }> {
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
+ return [];
+ }
+
+ const malformed: Array<{ path: string; present: string[]; missing: string[]; unexpected: string[] }> = [];
+ // Insertion-ordered, so groups are reported in the order the file declares them.
+ const groups = new Map<string, string[]>();
+
+ for (const [ key, child ] of Object.entries(value)) {
+ const childPath = path ? `${path}.${key}` : key;
+ const suffix = PLURAL_SUFFIXES.find((candidate) => key.endsWith(`_${candidate}`));
+ // A form holding anything but a string is `findPluralKeyConflicts`'s business: it is a
+ // section that only looks like a plural, and reporting it as a broken group too would
+ // just be the same finding twice.
+ if (suffix && typeof child === "string") {
+ const base = key.slice(0, -(suffix.length + 1));
+ groups.set(base, [ ...groups.get(base) ?? [], suffix ]);
+ }
+ malformed.push(...findMalformedPluralGroups(child, categories, childPath));
+ }
+
+ for (const [ base, present ] of groups) {
+ const missing = categories.filter((category) => !present.includes(category));
+ const unexpected = present.filter((suffix) => !categories.includes(suffix));
+ if (missing.length || unexpected.length) {
+ malformed.push({ path: path ? `${path}.${base}` : base, present, missing, unexpected });
+ }
+ }
+
+ return malformed;
+}
+
/** The CLDR plural categories i18next appends to a key, in the `key_<category>` form. */
const PLURAL_SUFFIXES = [ "zero", "one", "two", "few", "many", "other" ];