From 497bb352098ecc2f092a5304733207850baae243 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 16 Nov 2025 21:03:53 +0200 Subject: [PATCH] test(canvas): test all languages are mapped correctly --- .../widgets/type_widgets/canvas/Canvas.tsx | 20 +------------ .../widgets/type_widgets/canvas/i18n.spec.ts | 29 +++++++++++++++++++ .../src/widgets/type_widgets/canvas/i18n.ts | 19 ++++++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 apps/client/src/widgets/type_widgets/canvas/i18n.spec.ts create mode 100644 apps/client/src/widgets/type_widgets/canvas/i18n.ts diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx index 968c5c994..fb5cc1df5 100644 --- a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx +++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx @@ -9,30 +9,12 @@ import "./Canvas.css"; import { NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types"; import { goToLinkExt } from "../../../services/link"; import useCanvasPersistence from "./persistence"; -import { DISPLAYABLE_LOCALE_IDS } from "@triliumnext/commons"; +import { LANGUAGE_MAPPINGS } from "./i18n"; // currently required by excalidraw, in order to allows self-hosting fonts locally. // this avoids making excalidraw load the fonts from an external CDN. window.EXCALIDRAW_ASSET_PATH = `${window.location.pathname}/node_modules/@excalidraw/excalidraw/dist/prod`; -const LANGUAGE_MAPPINGS: Record = { - ar: "ar-SA", - cn: "zh-CN", - de: "de-DE", - en: "en", - en_rtl: "en", - es: "es-ES", - fr: "fr-FR", - it: "it-IT", - ja: "ja-JP", - pt: "pt-PT", - pt_br: "pt-BR", - ro: "ro-RO", - ru: "ru-RU", - tw: "zh-TW", - uk: "uk-UA" -}; - export default function Canvas({ note, noteContext }: TypeWidgetProps) { const apiRef = useRef(null); const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly"); diff --git a/apps/client/src/widgets/type_widgets/canvas/i18n.spec.ts b/apps/client/src/widgets/type_widgets/canvas/i18n.spec.ts new file mode 100644 index 000000000..71eb3d18c --- /dev/null +++ b/apps/client/src/widgets/type_widgets/canvas/i18n.spec.ts @@ -0,0 +1,29 @@ +import { LOCALES } from "@triliumnext/commons"; +import { readdirSync } from "fs"; +import { join } from "path"; +import { describe, expect, it } from "vitest"; +import { LANGUAGE_MAPPINGS } from "./i18n.js"; + +const localeDir = join(__dirname, "../../../../../../node_modules/@excalidraw/excalidraw/dist/prod/locales"); + +describe("Canvas i18n", () => { + it("all languages are mapped correctly", () => { + // Read the node_modules dir to obtain all the supported locales. + const supportedLanguageCodes = new Set(); + for (const file of readdirSync(localeDir)) { + if (file.startsWith("percentages")) continue; + const match = file.match("^[a-z]{2,3}(?:-[A-Z]{2,3})?"); + if (!match) continue; + supportedLanguageCodes.add(match[0]); + } + + // Cross-check the locales. + for (const locale of LOCALES) { + if (locale.contentOnly || locale.devOnly) continue; + const languageCode = LANGUAGE_MAPPINGS[locale.id]; + if (!supportedLanguageCodes.has(languageCode)) { + expect.fail(`Unable to find locale for ${locale.id} -> ${languageCode}.`) + } + } + }); +}); diff --git a/apps/client/src/widgets/type_widgets/canvas/i18n.ts b/apps/client/src/widgets/type_widgets/canvas/i18n.ts new file mode 100644 index 000000000..43ee724cf --- /dev/null +++ b/apps/client/src/widgets/type_widgets/canvas/i18n.ts @@ -0,0 +1,19 @@ +import type { DISPLAYABLE_LOCALE_IDS } from "@triliumnext/commons"; + +export const LANGUAGE_MAPPINGS: Record = { + ar: "ar-SA", + cn: "zh-CN", + de: "de-DE", + en: "en", + en_rtl: "en", + es: "es-ES", + fr: "fr-FR", + it: "it-IT", + ja: "ja-JP", + pt: "pt-PT", + pt_br: "pt-BR", + ro: "ro-RO", + ru: "ru-RU", + tw: "zh-TW", + uk: "uk-UA" +};