Skip to content

Build your personal knowledge base with Trilium Notes

test(commons): cover getEnglishName, which moved in untested

Elian Doran8a04fc74ca55
diff --git a/packages/commons/src/lib/i18n.spec.ts b/packages/commons/src/lib/i18n.spec.ts
index 1ab2bb2d0e..c7b8473f04 100644
--- a/packages/commons/src/lib/i18n.spec.ts
+++ b/packages/commons/src/lib/i18n.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { getTesseractCode, isDisplayableLocale, LOCALES } from "./i18n.js";
+import { getEnglishName, getTesseractCode, isDisplayableLocale, LOCALES } from "./i18n.js";
describe("getTesseractCode", () => {
it("returns the Tesseract code for a mapped locale", () => {
@@ -58,3 +58,40 @@ describe("isDisplayableLocale", () => {
}
});
});
+
+describe("getEnglishName", () => {
+ it("names a locale in English, underscores and all", () => {
+ expect(getEnglishName("de")).toBe("German");
+ // Our ids separate the subtags with an underscore where BCP-47 wants a hyphen.
+ expect(getEnglishName("pt_br")).toBe("Brazilian Portuguese");
+ });
+
+ // Through the script subtags rather than the regions, which is the distinction these two
+ // entries actually differ by: "Chinese (China)" would name the wrong thing.
+ it("resolves the Chinese pair by script", () => {
+ expect(getEnglishName("cn")).toBe("Simplified Chinese");
+ expect(getEnglishName("tw")).toBe("Traditional Chinese");
+ });
+
+ it("offers nothing for the English entries, which are named in English already", () => {
+ expect(getEnglishName("en")).toBeNull();
+ expect(getEnglishName("en_gb")).toBeNull();
+ // Not a well-formed tag at all — the prefix check is what keeps `Intl` from throwing on it.
+ expect(getEnglishName("en_rtl")).toBeNull();
+ });
+
+ it("offers nothing for a tag Intl cannot read", () => {
+ // A malformed tag raises a RangeError rather than returning an empty result.
+ expect(getEnglishName("nonexistent-locale")).toBeNull();
+ });
+
+ it("offers nothing when the formatter has no name to give", () => {
+ // The default formatter falls back to echoing the code; one built to say nothing instead
+ // is what leaves `of()` empty for a well-formed tag it does not know.
+ const silent = new Intl.DisplayNames([ "en" ], { type: "language", fallback: "none" });
+
+ expect(getEnglishName("zz", silent)).toBeNull();
+ // The same formatter still names what it does know, so this is not just a broken one.
+ expect(getEnglishName("ro", silent)).toBe("Romanian");
+ });
+});