Files
CatoandClaude c2c42d541c langkit: one way to be multilingual across the fleet (#17)
* langkit: one way to be multilingual across the fleet

A survey of the bitbaum repos (docs/survey-2026-11-29.md) found 22 apps with
more than one language: 14 on next-intl, 8 with their own tables, and around
them 8 private Accept-Language parsers, 4 cookie names and 12 home-made
completeness checks. The decision (docs/decision.md) keeps ICU MessageFormat
on next-intl's engine and puts the repeated parts in one package:

- defineLanguages: the list, names, direction, fallbacks, regional variants
- pickLocale: the reader's choice, the browser, the content, the default
- createMessages: typed ICU messages on use-intl/core, usable in server
  components without a provider; de-CH is German respelled, not a file
- langkit check: missing/extra/empty keys, ICU syntax, arguments, plural
  forms, ß in Swiss German; for each app's verify
- toIcu: drafts ICU messages from hand-written copy tables
- LanguageSwitcher: plain links, fits sitekit's masthead actions

Proof without touching the app: BiasLens's rewrite-copy.ts (copied at
c3497b7 as a fixture), all 107 entries in en, de, de-CH and fr, says the same
through langkit. Two deliberate differences are asserted as tests.

Not on npm yet: a new package needs the owner's one npm sign-in (README).

Claude-Session: https://claude.ai/code/session_019MDtmsTRGc34xnG6koVuAY

* langkit: private until its first publish

A merged version bump publishes from GitLab, and npm cannot trust a package
that does not exist yet. Marked private so the merge starts no publish that
must fail; publishing 0.1.0 is the owner's one npm sign-in plus removing this
line.

Claude-Session: https://claude.ai/code/session_019MDtmsTRGc34xnG6koVuAY

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-29 17:11:12 +01:00

63 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test";
import assert from "node:assert/strict";
import { createMessages, defineLanguages } from "@bitbaum/langkit";
import { escapeIcu, toIcu } from "@bitbaum/langkit/migrate";
test("a function that only places its arguments becomes a message with placeholders", () => {
const table = {
title: "Rewrite studio",
by: {
ask: (name) => `Write AI ${name}’s answer`,
wrote: (author, voice) => `Asked for by ${author}. Written by AI in the voice of ${voice}.`,
},
facts: (kept, total) => `${kept} of ${total} facts kept.`,
lone: (name) => `Open ${name}`,
};
assert.deepEqual(toIcu(table), {
messages: {
title: "Rewrite studio",
by: {
ask: "Write AI {name}’s answer",
wrote: "Asked for by {author}. Written by AI in the voice of {voice}.",
},
facts: "{kept} of {total} facts kept.",
lone: "Open {name}",
},
manual: [],
});
});
test("a function that decides something is left for a person, by key", () => {
const table = {
evidence: (count) => `Cites ${count} finding${count === 1 ? "" : "s"}.`,
list: (names) => names.join(", "),
nested: { both: (a, b) => (a || b ? "x" : "y") },
count: 3,
};
const draft = toIcu(table);
assert.deepEqual(draft.messages, { nested: {} });
assert.deepEqual(draft.manual, ["evidence", "list", "nested.both", "count"]);
});
test("text that ICU would read as syntax is quoted, and reads back unchanged", () => {
const table = {
braces: "Use {name} literally, and <b> too.",
apostrophe: "It's 5 o'clock; l'été",
mixed: (name) => `{${name}} isn't <ok>`,
};
const { messages } = toIcu(table);
const languages = defineLanguages({ locales: ["en"], defaultLocale: "en" });
const t = createMessages({
languages,
tables: { en: messages },
onError: (error) => {
throw error;
},
}).translator("en");
assert.equal(t("braces"), table.braces);
assert.equal(t("apostrophe"), table.apostrophe);
assert.equal(t("mixed", { name: "x" }), table.mixed("x"));
assert.equal(escapeIcu("plain"), "plain");
});