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
+142
View File
@@ -0,0 +1,142 @@
import { describe, expect, it } from 'vitest';
import { checkFilled, compareMessages, leaves, placeholders } from './compare';
import { fill, locale, locales, messages, nl, t, translator } from './index';
describe('t', () => {
it('gives the text of a key', () => {
expect(t('common.aria.backToTop')).toBe('Terug naar boven');
});
it('is the language of this build', () => {
expect(locale).toBe('nl');
expect(messages).toBe(nl);
});
it('fails for a key that does not exist, with the key in the message', () => {
expect(() => t('common.aria.bestaatNiet' as never)).toThrow(
'Missing text: common.aria.bestaatNiet',
);
expect(() => t('common.aria.backToTop.x' as never)).toThrow(
'Missing text',
);
expect(() => t('common' as never)).toThrow('Not a text: common');
});
});
// A tree of its own, to try parameters, plurals, lists and arrays
const tree = {
greeting: 'Hallo {name}, je hebt {count} berichten',
photos: { one: '{count} foto', other: '{count} foto’s' },
steps: ['Eerste', 'Tweede'],
faq: [{ question: 'Hoe?' }, { question: 'Waarom?' }],
mixed: ['Eerste', 3],
};
const own = translator(tree, 'nl');
describe('translator', () => {
it('fills {parameters}', () => {
expect(own.t('greeting', { name: 'Eva', count: 2 })).toBe(
'Hallo Eva, je hebt 2 berichten',
);
});
it('fails for a parameter that is not given', () => {
expect(() => own.t('greeting', { name: 'Eva' })).toThrow(
'Missing parameter {count}',
);
expect(() => own.t('greeting')).toThrow('Missing parameter {name}');
});
it('reaches texts inside lists by position', () => {
expect(own.t('faq.1.question')).toBe('Waarom?');
expect(own.t('steps.0')).toBe('Eerste');
});
it('gives a list of texts', () => {
expect(own.tList('steps')).toEqual(['Eerste', 'Tweede']);
expect(() => own.tList('mixed' as never)).toThrow(
'Not a list of texts',
);
expect(() => own.tList('greeting' as never)).toThrow(
'Not a list of texts',
);
});
it('chooses the singular or the plural form for the language', () => {
expect(own.tPlural('photos', 1)).toBe('1 foto');
expect(own.tPlural('photos', 0)).toBe('0 foto’s');
expect(own.tPlural('photos', 2)).toBe('2 foto’s');
expect(own.tPlural('photos', 21, { name: 'x' })).toBe('21 foto’s');
});
it('follows the plural rules of another language', () => {
// Polish: one, few and many forms; only "one" has its own text here
const pl = translator(tree, 'pl');
expect(pl.tPlural('photos', 1)).toBe('1 foto');
expect(pl.tPlural('photos', 5)).toBe('5 foto’s');
});
});
describe('fill', () => {
it('leaves a text without parameters as it is', () => {
expect(fill('Gewoon {{ tekst')).toBe('Gewoon {{ tekst');
});
});
describe('the texts of the site', () => {
it('are all filled, without TODO and with valid {parameters}', () => {
expect(checkFilled(nl)).toEqual([]);
expect(leaves(nl).size).toBeGreaterThan(0);
});
it.each(Object.keys(locales))(
'language %s has the same keys and parameters as Dutch',
(name) => {
expect(
compareMessages(nl, locales[name as keyof typeof locales]),
).toEqual([]);
},
);
});
describe('compareMessages and checkFilled', () => {
const base = { a: 'Hallo {name}', b: ['een', 'twee'], c: { d: 'x' } };
it('finds missing keys, extra keys and different parameters', () => {
const other = { a: 'Hello', b: ['one'], c: { d: 'x', e: 'extra' } };
expect(compareMessages(base, other)).toEqual([
{ key: 'a', problem: 'different {parameters}' },
{ key: 'b.1', problem: 'missing' },
{ key: 'c.e', problem: 'not in the base' },
]);
});
it('accepts parameters in another order', () => {
expect(
compareMessages({ a: '{x} en {y}' }, { a: '{y} and {x}' }),
).toEqual([]);
});
it('finds empty texts, TODO and broken braces', () => {
expect(
checkFilled({
a: ' ',
b: 'TODO vertalen',
c: 'kapot {naam',
d: 'ok',
}),
).toEqual([
{ key: 'a', problem: 'empty' },
{ key: 'b', problem: 'TODO' },
{ key: 'c', problem: 'invalid {parameter}' },
]);
});
it('lists the parameters of a text', () => {
expect(placeholders('{b} {a} {b}')).toEqual(['a', 'b', 'b']);
});
it('collects texts of numbers, booleans and null as nothing', () => {
expect(leaves({ a: 1, b: true, c: null, d: 'x' }).size).toBe(1);
});
});