Practitioner 2 min read
Typed translations: making a missing string a build error
Two languages from one static build, where TypeScript refuses to compile when a translation is missing instead of quietly falling back to English.
Published
The usual way to add a second language is a dictionary of string keys and a t()
function.
t("home.hero.title");
It works, and it fails quietly. Rename a key and you get undefined on a page.
Forget a translation and a visitor reads English on a Spanish URL. Nothing
breaks loudly enough for you to notice before a reader does.
Make the compiler do the checking
The alternative costs nothing extra and removes the whole class of problem. Make the English copy an ordinary object, derive a type from it, and require every other language to satisfy that type.
// copy/en.ts: the source of truth
export const en = {
nav: { work: "Work", writing: "Writing", about: "About" },
cta: {
lead: "Free consultation",
action: "Discuss a project",
},
};
export type Copy = typeof en;
// copy/es.ts: checked against it
import type { Copy } from "./en";
export const es: Copy = {
nav: { work: "Trabajo", writing: "Notas", about: "Perfil" },
cta: {
lead: "Consultas gratis",
action: "Hablemos del proyecto",
},
};
Miss a key and tsc fails and points at the exact path. Rename one in English
and every locale that has not caught up fails too. There is no string-key
indirection to get wrong, because there are no string keys, just property access
that your editor autocompletes.
<h1>{copy.home.title}</h1>
One detail matters. Do not write as const on the English object. With
as const, every string narrows to its own literal type, and the Spanish file
would have to contain the identical English text. Without it the types widen to
string and string[], so the structure is checked and the content is free.
That is the contract you want.
Routing: default language unprefixed
English stays at /; Spanish lives at /es/. Existing URLs keep working,
which matters more than symmetry.
i18n: {
defaultLocale: "en",
locales: ["en", "es"],
routing: { prefixDefaultLocale: false },
}
Three small pure functions handle the rest. They deserve care, because every link on the site passes through them.
export function localizePath(path: string, locale: Locale): string {
const clean = stripLocale(path);
if (locale === DEFAULT_LOCALE) return clean;
return clean === "/" ? `/${locale}` : `/${locale}${clean}`;
}
stripLocale runs first, which keeps localizePath idempotent. Pass in an
already-localized path and you get the correct result instead of /es/es/about.
Content, and admitting the gaps
Long-form content lives one directory per language.
src/content/blog/
├── en/typed-translations-static-build.mdx
└── es/typed-translations-static-build.mdx
An entry id becomes en/typed-translations-static-build. The loader is
unchanged, and the locale is simply the first path segment.
Translations of long articles lag behind, and that is normal. What deserves design is the fallback. Falling back to English is correct; doing it silently is not.
const translated = byLocale.get(slug);
const entry = translated ?? fallback.get(slug);
posts.push({ entry, slug, translated: Boolean(translated) });
The translated flag travels with the entry, and the page renders a short
notice when it is false. The reader gets told which language they are actually
reading, instead of having to work it out.
What this does not solve
Type checking guarantees a string exists. It cannot tell you the translation is good, that a date reads naturally, or that a sentence still fits its button in German. Those need a human.
What it buys is a different failure mode, “the build refuses” instead of “a visitor finds it”. That is the trade I want on every project. Push errors as early as they will go, and accept that the ones left are the interesting kind.