Files
homepage/src/lib/js/recipesI18n.ts
T
Alexander d540b82e85 i18n(recipes): bootstrap namespace + migrate layout, NutritionSummary
Two-locale recipes dictionary lands at src/lib/i18n/recipes/{de,en}.ts
with the same satisfies-based completeness enforcement as the other
namespaces. recipesI18n.ts is the slim shim — exports m, RecipesLang,
RecipesKey, plus langFromRecipeSlug / recipeSlugFromLang helpers for
the rezepte ↔ recipes URL slug mapping.

[recipeLang]/+layout.svelte's nav-label ternary chain collapses into
t.foo lookups. NutritionSummary.svelte is the heavy hitter — 33
inline isEnglish ternaries become a single dictionary load. Most
amino-acid names use a German-stem-plus-optional-e pattern in the old
code (`Lysin{isEnglish ? 'e' : ''}`) that's now just t.lysine in the
template; less clever, much more obviously translatable.
2026-05-01 13:22:59 +02:00

40 lines
1.3 KiB
TypeScript

/**
* Recipes route i18n.
*
* Translation tables live per-locale in `$lib/i18n/recipes/{de,en}.ts`.
* `de.ts` is the source of truth for the key set; `en.ts` uses
* `satisfies Record<keyof typeof de, string>` so missing translations
* fail the build.
*
* Recipes routes get `lang` from the layout server load (data.lang),
* derived from the [recipeLang=recipeLang] param matcher: rezepte→de,
* recipes→en. Use `langFromRecipeSlug(params.recipeLang)` if you need it
* from the slug directly.
*/
import { de } from '$lib/i18n/recipes/de';
import { en } from '$lib/i18n/recipes/en';
export const m = { de, en } as const;
export type RecipesLang = keyof typeof m;
export type RecipesKey = keyof typeof de;
/** Map a `[recipeLang]` slug to the locale code. */
export function langFromRecipeSlug(recipeLang: string | null | undefined): RecipesLang {
return recipeLang === 'recipes' ? 'en' : 'de';
}
/** Reverse: locale → URL slug. */
export function recipeSlugFromLang(lang: RecipesLang): 'recipes' | 'rezepte' {
return lang === 'en' ? 'recipes' : 'rezepte';
}
/**
* Get a translated string. Prefer `m[lang].key` directly in new code —
* this helper is kept for incremental migration.
*/
export function t(key: RecipesKey, lang: RecipesLang): string {
return m[lang][key] ?? m.en[key] ?? key;
}