test(@websnacksjs/i18n): fix broken tests missing translation messages (#5)
This commit is contained in:
parent
2867c2f875
commit
d6696a150b
10 changed files with 35 additions and 65 deletions
2
packages/i18n/.gitignore
vendored
2
packages/i18n/.gitignore
vendored
|
|
@ -1 +1 @@
|
||||||
!/tests/messages/*/*.json
|
!/tests/fixtures/**/*.json
|
||||||
|
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
import assert from "node:assert/strict";
|
|
||||||
import { beforeEach, describe, it } from "node:test";
|
|
||||||
import type I18n from "@websnacksjs/i18n";
|
|
||||||
import { type Fixtures, withFixture } from "../fixtures.js";
|
|
||||||
|
|
||||||
describe("i18n.supportedLocales()", () => {
|
|
||||||
let i18n: I18n<Fixtures["base"]>;
|
|
||||||
beforeEach(() => {
|
|
||||||
i18n = withFixture("base", {
|
|
||||||
supportedLocales: ["en", "fr", "fr-Arab"],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns maximized locales for all declared, supported locales", () => {
|
|
||||||
assert.deepEqual(i18n.supportedLocales(), [
|
|
||||||
"en-Latn-US",
|
|
||||||
"fr-Latn-FR",
|
|
||||||
"fr-Arab-FR",
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("i18n.loadMessages(...)", () => {
|
|
||||||
let i18n: I18n<Fixtures["base"]>;
|
|
||||||
beforeEach(() => {
|
|
||||||
i18n = withFixture("base");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("guesses region of locales w/o region tags", async () => {
|
|
||||||
const t = await i18n.loadMessages({
|
|
||||||
locale: "fr-Latn",
|
|
||||||
});
|
|
||||||
assert.equal(t.locale(), "fr-Latn-FR");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("guesses script of locales w/ region tags", async () => {
|
|
||||||
const t = await i18n.loadMessages({
|
|
||||||
locale: "fr-FR",
|
|
||||||
});
|
|
||||||
assert.equal(t.locale(), "fr-Latn-FR");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("guesses script & region of bare language locales", async () => {
|
|
||||||
const t = await i18n.loadMessages({
|
|
||||||
locale: "fr",
|
|
||||||
});
|
|
||||||
assert.equal(t.locale(), "fr-Latn-FR");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does NOT fallback to bare language locales", async () => {
|
|
||||||
await assert.rejects(
|
|
||||||
i18n.loadMessages({
|
|
||||||
locale: "en-Arab",
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
message:
|
|
||||||
'no declared locale matches requested locale of "en-Arab-US" (maximized from "en-Arab")',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -2,8 +2,8 @@ import I18n, { type I18nOptions } from "@websnacksjs/i18n";
|
||||||
|
|
||||||
export type Fixtures = {
|
export type Fixtures = {
|
||||||
base: {
|
base: {
|
||||||
common: typeof import("./fixtures/base/messages/en/common.json");
|
common: typeof import("./fixtures/base/en/common.json");
|
||||||
drama: typeof import("./fixtures/base/messages/en/drama.json");
|
drama: typeof import("./fixtures/base/en/drama.json");
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -17,7 +17,7 @@ export const withFixture = <F extends keyof Fixtures>(
|
||||||
supportedLocales: ["en", "fr", "fr-Arab"],
|
supportedLocales: ["en", "fr", "fr-Arab"],
|
||||||
namespaces: ["drama"],
|
namespaces: ["drama"],
|
||||||
messagesUrlTemplate: new URL(
|
messagesUrlTemplate: new URL(
|
||||||
"./fixtures/base/messages/:locale/:namespace.json",
|
"./fixtures/base/:locale/:namespace.json",
|
||||||
import.meta.url,
|
import.meta.url,
|
||||||
),
|
),
|
||||||
...(overrides as Partial<I18nOptions<Fixtures["base"]>>),
|
...(overrides as Partial<I18nOptions<Fixtures["base"]>>),
|
||||||
|
|
|
||||||
7
packages/i18n/tests/fixtures/base/en/common.json
vendored
Normal file
7
packages/i18n/tests/fixtures/base/en/common.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"oh hai": "Oh hai {{name}}!",
|
||||||
|
"denial": "I did not hit her. I did not.",
|
||||||
|
"flower shop": {
|
||||||
|
"doggy": "hello doggy!"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
packages/i18n/tests/fixtures/base/en/drama.json
vendored
Normal file
3
packages/i18n/tests/fixtures/base/en/drama.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"tearing me apart": "You're tearing me apart, Lisa!"
|
||||||
|
}
|
||||||
7
packages/i18n/tests/fixtures/base/fr-Arab/common.json
vendored
Normal file
7
packages/i18n/tests/fixtures/base/fr-Arab/common.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"oh hai": "أوه سالو، \u2066{{name}}\u2069!",
|
||||||
|
"denial": "جُ ن لي با فرابيه. جُ ن لي با.",
|
||||||
|
"flower shop": {
|
||||||
|
"doggy": "سالو توتو!"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
packages/i18n/tests/fixtures/base/fr-Arab/drama.json
vendored
Normal file
3
packages/i18n/tests/fixtures/base/fr-Arab/drama.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"tearing me apart": "تو م ديشير، ليزا!"
|
||||||
|
}
|
||||||
7
packages/i18n/tests/fixtures/base/fr/common.json
vendored
Normal file
7
packages/i18n/tests/fixtures/base/fr/common.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"oh hai": "Oh salut, Mark !",
|
||||||
|
"denial": "Je ne l'ai pas frappée. Je ne l'ai pas.",
|
||||||
|
"flower shop": {
|
||||||
|
"doggy": "Salut toutou !"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
packages/i18n/tests/fixtures/base/fr/drama.json
vendored
Normal file
3
packages/i18n/tests/fixtures/base/fr/drama.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"tearing me apart": "Tu me déchires, Lisa !"
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
"extends": "../tsconfig.json",
|
"extends": "../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"rootDir": "."
|
"rootDir": "."
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue