Skip to content

Build your personal knowledge base with Trilium Notes

fix(i18n): rename the settings_other namespace breaking Weblate

Elian Doran16f00ae5859b
diff --git a/apps/client/src/services/i18n.spec.ts b/apps/client/src/services/i18n.spec.ts
index 4a6be1db37..2d498fe90e 100644
--- a/apps/client/src/services/i18n.spec.ts
+++ b/apps/client/src/services/i18n.spec.ts
@@ -1,4 +1,4 @@
-import { dayjs, findDuplicateJsonKeys, LOCALES } from "@triliumnext/commons";
+import { dayjs, findDuplicateJsonKeys, findPluralKeyConflicts, LOCALES } from "@triliumnext/commons";
import { readFileSync } from "fs";
import { join } from "path";
import { describe, expect, it, vi } from "vitest";
@@ -41,6 +41,16 @@ describe("i18n", () => {
`Duplicate keys in locale '${locale.id}' at "${translationPath}":\n${
duplicates.map((d) => ` - "${d.key}" (line ${d.line})`).join("\n")}`
).toEqual([]);
+
+ // Weblate reads these files as i18next JSON v4, where a `_one`/`_other`/… suffix
+ // marks a plural form. A section named that way is combined with the key before it
+ // into a multistring and breaks the import of the whole file.
+ const pluralConflicts = findPluralKeyConflicts(JSON.parse(translationFile));
+ expect(
+ pluralConflicts,
+ `Keys in locale '${locale.id}' at "${translationPath}" that Weblate would read as plural forms but that are not translated strings:\n${
+ pluralConflicts.map((c) => ` - "${c.path}" (plural form '_${c.suffix}')`).join("\n")}`
+ ).toEqual([]);
}
});
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 53514ad81b..77cd3fc4ae 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3124,9 +3124,7 @@
},
"settings": {
"related_settings": "Related settings",
- "related_actions": "Related actions"
- },
- "settings_other": {
+ "related_actions": "Related actions",
"related_space_usage": "Analyze the space usage within the database",
"related_space_usage_description": "See which notes, revisions and attachments take up the most space, and reclaim it."
},
diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsx
index e44bed4c8c..01749df633 100644
--- a/apps/client/src/widgets/type_widgets/options/other.tsx
+++ b/apps/client/src/widgets/type_widgets/options/other.tsx
@@ -40,8 +40,8 @@ function RelatedActions() {
title={t("settings.related_actions")}
items={[
{
- title: t("settings_other.related_space_usage"),
- description: t("settings_other.related_space_usage_description"),
+ title: t("settings.related_space_usage"),
+ description: t("settings.related_space_usage_description"),
targetPage: "_optionsContentManager",
onClick: (e) => {
// The Content Manager opens on Active Content unless asked otherwise, and
diff --git a/apps/server/src/services/i18n.spec.ts b/apps/server/src/services/i18n.spec.ts
index d0ff3fc4b2..c0108a1dbd 100644
--- a/apps/server/src/services/i18n.spec.ts
+++ b/apps/server/src/services/i18n.spec.ts
@@ -1,4 +1,4 @@
-import { findDuplicateJsonKeys, LOCALES } from "@triliumnext/commons";
+import { findDuplicateJsonKeys, findPluralKeyConflicts, LOCALES } from "@triliumnext/commons";
import { dayjs } from "@triliumnext/commons";
import { hidden_subtree, options } from "@triliumnext/core";
import { readFileSync } from "fs";
@@ -36,6 +36,16 @@ describe("i18n", () => {
`Duplicate keys in locale '${locale.id}' at "${translationPath}":\n` +
duplicates.map((d) => ` - "${d.key}" (line ${d.line})`).join("\n")
).toEqual([]);
+
+ // Weblate reads these files as i18next JSON v4, where a `_one`/`_other`/… suffix
+ // marks a plural form. A section named that way is combined with the key before it
+ // into a multistring and breaks the import of the whole file.
+ const pluralConflicts = findPluralKeyConflicts(JSON.parse(translationFile));
+ expect(
+ pluralConflicts,
+ `Keys in locale '${locale.id}' at "${translationPath}" that Weblate would read as plural forms but that are not translated strings:\n` +
+ pluralConflicts.map((c) => ` - "${c.path}" (plural form '_${c.suffix}')`).join("\n")
+ ).toEqual([]);
}
});
diff --git a/packages/commons/src/lib/test-utils.spec.ts b/packages/commons/src/lib/test-utils.spec.ts
index fce7c88428..b96663ecd2 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, sleepFor, trimIndentation } from "./test-utils.js";
+import { findDuplicateJsonKeys, findPluralKeyConflicts, sleepFor, trimIndentation } from "./test-utils.js";
describe("Utils", () => {
it("trims indentation", () => {
@@ -83,4 +83,54 @@ Hello
expect(findDuplicateJsonKeys(`{ "a" : 1, "a" : 2 }`)).toEqual([{ key: "a", line: 1 }]);
});
});
+
+ describe("findPluralKeyConflicts", () => {
+ it("accepts plural forms and keys that merely end in a similar word", () => {
+ expect(findPluralKeyConflicts({
+ item_one: "One item",
+ item_other: "{{count}} items",
+ settings_appearance: { ui: "User interface" },
+ mother: { tongue: "Mother tongue" },
+ brother_in_law: { title: "Brother in law" }
+ })).toEqual([]);
+ });
+
+ it("reports a namespace named after a plural suffix, with its path and suffix", () => {
+ expect(findPluralKeyConflicts({
+ settings: { related_actions: "Related actions" },
+ settings_other: { related_space_usage: "Space usage" }
+ })).toEqual([{ path: "settings_other", suffix: "other" }]);
+ });
+
+ it("reports every plural category and finds them at any depth", () => {
+ expect(findPluralKeyConflicts({
+ a: { b_zero: {}, b_one: {}, b_two: {}, b_few: {}, b_many: {}, b_other: {} }
+ })).toEqual([
+ { path: "a.b_zero", suffix: "zero" },
+ { path: "a.b_one", suffix: "one" },
+ { path: "a.b_two", suffix: "two" },
+ { path: "a.b_few", suffix: "few" },
+ { path: "a.b_many", suffix: "many" },
+ { path: "a.b_other", suffix: "other" }
+ ]);
+ });
+
+ it("accepts a list of strings but not a list containing anything else", () => {
+ expect(findPluralKeyConflicts({ item_other: [ "a", "b" ] })).toEqual([]);
+ expect(findPluralKeyConflicts({ item_other: [ "a", 2 ] })).toEqual([{ path: "item_other", suffix: "other" }]);
+ });
+
+ it("descends into a conflicting namespace to report nested conflicts too", () => {
+ expect(findPluralKeyConflicts({ outer_one: { inner_other: {} } })).toEqual([
+ { path: "outer_one", suffix: "one" },
+ { path: "outer_one.inner_other", suffix: "other" }
+ ]);
+ });
+
+ it("returns nothing for values that are not objects", () => {
+ expect(findPluralKeyConflicts("a string")).toEqual([]);
+ expect(findPluralKeyConflicts(null)).toEqual([]);
+ expect(findPluralKeyConflicts([ { a_other: {} } ])).toEqual([]);
+ });
+ });
});
diff --git a/packages/commons/src/lib/test-utils.ts b/packages/commons/src/lib/test-utils.ts
index 4c997b1fcb..312fec6976 100644
--- a/packages/commons/src/lib/test-utils.ts
+++ b/packages/commons/src/lib/test-utils.ts
@@ -70,6 +70,51 @@ export function sleepFor(duration: number) {
* Intended for validating hand-maintained JSON files (e.g. translation files)
* at test level.
*/
+/**
+ * Scans a parsed translation file for keys that Weblate would read as a plural form but
+ * that do not hold a plain string.
+ *
+ * Weblate parses our translation files as i18next JSON v4, in which a `_zero`, `_one`,
+ * `_two`, `_few`, `_many` or `_other` suffix marks a plural form of the key preceding it.
+ * All the forms found for a key are then combined into a single multistring, which fails
+ * with "multistring can only contain strings or list of strings" as soon as one of them
+ * is a nested object rather than a translated string.
+ *
+ * In practice this bites when a namespace happens to be named after a plural suffix — for
+ * example a `settings_other` section sitting next to a `settings` one, which Weblate reads
+ * as the plural of `settings` instead of as a section of its own.
+ *
+ * @param value the parsed translation file (or any subtree of it).
+ * @param path the dotted path of `value`, used to build the reported paths.
+ * @returns one entry per offending key, with its dotted path and the plural suffix that
+ * made Weblate treat it as a plural form.
+ */
+export function findPluralKeyConflicts(value: unknown, path = ""): Array<{ path: string; suffix: string }> {
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
+ return [];
+ }
+
+ const conflicts: Array<{ path: string; suffix: string }> = [];
+ for (const [ key, child ] of Object.entries(value)) {
+ const childPath = path ? `${path}.${key}` : key;
+ const suffix = PLURAL_SUFFIXES.find((candidate) => key.endsWith(`_${candidate}`));
+ if (suffix && !isTranslatableValue(child)) {
+ conflicts.push({ path: childPath, suffix });
+ }
+ conflicts.push(...findPluralKeyConflicts(child, childPath));
+ }
+ return conflicts;
+}
+
+/** The CLDR plural categories i18next appends to a key, in the `key_<category>` form. */
+const PLURAL_SUFFIXES = [ "zero", "one", "two", "few", "many", "other" ];
+
+/** Whether Weblate can turn the value into a multistring, i.e. a string or a list of strings. */
+function isTranslatableValue(value: unknown) {
+ return typeof value === "string"
+ || (Array.isArray(value) && value.every((entry) => typeof entry === "string"));
+}
+
export function findDuplicateJsonKeys(text: string): Array<{ key: string; line: number }> {
const duplicates: Array<{ key: string; line: number }> = [];
const stack: Set<string>[] = [];