SPB Git

Repository Public

i18n.test.ts 4.0 KB 88 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : apps/web/src/i18n/i18n.test.ts9 * Module           : i18n10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { describe, expect, it } from "vitest";43import { dictionaries, flatKeys, pickLocale, translate, translatePlural } from "./core";4445describe("i18n dictionaries", () => {46  const fr = flatKeys(dictionaries.fr).sort();47  const en = flatKeys(dictionaries.en).sort();4849  it("fr and en have identical key sets", () => {50    expect(en.filter((k) => !fr.includes(k)), "keys only in en").toEqual([]);51    expect(fr.filter((k) => !en.includes(k)), "keys only in fr").toEqual([]);52    expect(fr.length).toBeGreaterThan(300);53  });5455  const leaf = (locale: "fr" | "en", key: string): unknown => key.split(".").reduce<unknown>((n, p) => (n as Record<string, unknown>)[p], dictionaries[locale]);5657  it("has no empty translation", () => {58    for (const locale of ["fr", "en"] as const) {59      const empty = flatKeys(dictionaries[locale]).filter((k) => typeof leaf(locale, k) !== "string" || (leaf(locale, k) as string).trim() === "");60      // namespace stubs (`title: ""`) are not allowed to ship61      expect(empty, `empty ${locale} entries`).toEqual([]);62    }63  });6465  it("uses the same placeholders in both locales", () => {66    const vars = (s: unknown) => [...String(s).matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort().join(",");67    const mismatched = fr.filter((k) => vars(leaf("fr", k)) !== vars(leaf("en", k)));68    expect(mismatched).toEqual([]);69  });7071  it("interpolates variables and selects plural forms per locale", () => {72    expect(translate("fr", "errors.forbidden.bodyPermission", { permission: "audit.read" })).toBe("Il vous manque la permission audit.read.");73    expect(translate("en", "shell.cluster.nodes", { fresh: 26, total: 29 })).toBe("26/29 nodes fresh");74    expect(translatePlural("fr", "ui.log.matches", 0)).toBe("0 ligne correspondante"); // French: 0 is singular75    expect(translatePlural("fr", "ui.log.matches", 2)).toBe("2 lignes correspondantes");76    expect(translatePlural("en", "ui.log.matches", 0)).toBe("0 matching lines");77    expect(translatePlural("en", "ui.log.matches", 1)).toBe("1 matching line");78  });7980  it("picks the locale from the preference cookie, then Accept-Language, then French", () => {81    expect(pickLocale("en", "fr-CA")).toBe("en");82    expect(pickLocale(undefined, "en-US,en;q=0.9,fr;q=0.8")).toBe("en");83    expect(pickLocale(undefined, "de-DE,fr;q=0.5")).toBe("fr");84    expect(pickLocale("xx", "es")).toBe("fr");85    expect(pickLocale(undefined, null)).toBe("fr");86  });87});88
Files main
Menu

Theme