Skip to content

Build your personal knowledge base with Trilium Notes

test(canvas): test all languages are mapped correctly

Elian Doran497bb352098e
diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx
index 968c5c9944..fb5cc1df56 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<DISPLAYABLE_LOCALE_IDS, string | null> = {
- 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<ExcalidrawImperativeAPI>(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 0000000000..71eb3d18cf
--- /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<string>();
+ 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 0000000000..43ee724cf8
--- /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<DISPLAYABLE_LOCALE_IDS, string | null> = {
+ 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"
+};