import about from './nl/about.json'; import common from './nl/common.json'; import contact from './nl/contact.json'; import home from './nl/home.json'; import instagram from './nl/instagram.json'; import portfolio from './nl/portfolio.json'; import seo from './nl/seo.json'; import shoots from './nl/shoots.json'; import site from './nl/site.json'; import testimonials from './nl/testimonials.json'; // The texts of the site, per language, as JSON per area (src/i18n//*.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, site, home, about, contact, shoots, portfolio, instagram, testimonials, seo, }; 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; 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 '' ? K : `${P}.${K}`; // Dotted paths of every text (a string) in the messages; array positions are // numbers (faq.0.question) type StringPath = T extends string ? P : T extends readonly (infer U)[] ? StringPath> : T extends object ? { [K in keyof T & string]: StringPath>; }[keyof T & string] : never; // Paths of lists of texts (an array of strings) type ListPath = T extends readonly string[] ? P : T extends string ? never : T extends readonly unknown[] ? never : T extends object ? { [K in keyof T & string]: ListPath>; }[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 extends PluralForms ? P : T extends string ? never : T extends readonly unknown[] ? never : T extends object ? { [K in keyof T & string]: PluralPath>; }[keyof T & string] : never; export type MessageKey = StringPath; export type ListKey = ListPath; export type PluralKey = PluralPath; export type Params = Readonly>; 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)[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(tree: M, forLocale: string) { return { t: (key: StringPath, params?: Params) => text(tree, key, params), tList: (key: ListPath) => list(tree, key), tPlural: (key: PluralPath, count: number, params?: Params) => plural(tree, forLocale, key, count, params), }; } export const { t, tList, tPlural } = translator(messages, locale);