Translation files, step 0: typed text helper, checks and the baseline of the pages

Adds src/i18n (t, tList, tPlural over typed Dutch JSON, key and parameter
checks), the first text (back to top) as proof, a baseline tool for the
prerendered pages (pnpm baseline:save and baseline:check), a lint warning for
text typed straight into JSX, and docs/translations.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 17:34:18 +01:00
co-authored by Claude Sonnet 5.5
parent 107647f7a5
commit 246ebe5fd6
15 changed files with 834 additions and 2 deletions
+139
View File
@@ -0,0 +1,139 @@
import common from './nl/common.json';
// The texts of the site, per language, as JSON per area (src/i18n/<locale>/*.json).
// Only Dutch exists now. The types come from the Dutch files: a second language
// has to have exactly the same keys (see Messages and src/i18n/compare.ts), and
// a key that does not exist breaks the type check, so a missing text is found
// by the build and not by a visitor.
//
// No library: the files are imported as modules, so prerendering, hydrating
// and the host injection all see the same text without a loading step.
export const nl = { common };
export type Messages = typeof nl;
// Every language of the site; add one here (typed as Messages) to offer it
export const locales = { nl } satisfies Record<string, Messages>;
export type Locale = keyof typeof locales;
// The language of this build. A second language needs its own routes and a
// prerender per language; until then there is one.
export const locale: Locale = 'nl';
export const messages: Messages = locales[locale];
type Join<P extends string, K extends string> = P extends '' ? K : `${P}.${K}`;
// Dotted paths of every text (a string) in the messages; array positions are
// numbers (faq.0.question)
type StringPath<T, P extends string = ''> = T extends string
? P
: T extends readonly (infer U)[]
? StringPath<U, Join<P, `${number}`>>
: T extends object
? {
[K in keyof T & string]: StringPath<T[K], Join<P, K>>;
}[keyof T & string]
: never;
// Paths of lists of texts (an array of strings)
type ListPath<T, P extends string = ''> = T extends readonly string[]
? P
: T extends string
? never
: T extends readonly unknown[]
? never
: T extends object
? {
[K in keyof T & string]: ListPath<T[K], Join<P, K>>;
}[keyof T & string]
: never;
// A text with a singular and a plural form: { "one": "...", "other": "..." }
type PluralForms = { readonly one: string; readonly other: string };
type PluralPath<T, P extends string = ''> = T extends PluralForms
? P
: T extends string
? never
: T extends readonly unknown[]
? never
: T extends object
? {
[K in keyof T & string]: PluralPath<T[K], Join<P, K>>;
}[keyof T & string]
: never;
export type MessageKey = StringPath<Messages>;
export type ListKey = ListPath<Messages>;
export type PluralKey = PluralPath<Messages>;
export type Params = Readonly<Record<string, string | number>>;
function lookup(tree: unknown, key: string): unknown {
let node = tree;
for (const part of key.split('.')) {
if (typeof node !== 'object' || node === null || !(part in node)) {
throw new Error(`Missing text: ${key}`);
}
node = (node as Record<string, unknown>)[part];
}
return node;
}
// Replaces {name} by the parameter; a parameter that is not given is a bug in
// the caller, not something to show to a visitor
export function fill(text: string, params: Params = {}): string {
return text.replace(
/\{([A-Za-z][A-Za-z0-9]*)\}/g,
(_match, name: string) => {
if (!(name in params)) {
throw new Error(`Missing parameter {${name}} in: ${text}`);
}
return String(params[name]);
},
);
}
function text(tree: unknown, key: string, params?: Params): string {
const value = lookup(tree, key);
if (typeof value !== 'string') throw new Error(`Not a text: ${key}`);
return fill(value, params);
}
function list(tree: unknown, key: string): readonly string[] {
const value = lookup(tree, key);
if (
!Array.isArray(value) ||
value.some((item) => typeof item !== 'string')
) {
throw new Error(`Not a list of texts: ${key}`);
}
return value as readonly string[];
}
function plural(
tree: unknown,
forLocale: string,
key: string,
count: number,
params?: Params,
): string {
const forms = lookup(tree, key) as PluralForms;
const rule = new Intl.PluralRules(forLocale).select(count);
const form = rule === 'one' ? forms.one : forms.other;
return fill(form, { count, ...params });
}
// The functions for a language, over a tree of texts (the tests use their own)
export function translator<M extends object>(tree: M, forLocale: string) {
return {
t: (key: StringPath<M>, params?: Params) => text(tree, key, params),
tList: (key: ListPath<M>) => list(tree, key),
tPlural: (key: PluralPath<M>, count: number, params?: Params) =>
plural(tree, forLocale, key, count, params),
};
}
export const { t, tList, tPlural } = translator(messages, locale);