Loading...
Loading...
[Pragmatic DDD Architecture] Guide for internationalization. Use when adding or editing translations in any component, page, or layout — also when wrapping a subtree in LocaleProvider, reading the active locale in a new component, building or modifying a locale switcher, or touching any file that calls determineLocale() or useLocale(). Covers the custom library-free implementation, Translations co-location, Server vs Client locale access patterns, and setLocaleAction.
npx skill4agent add leif-sync/pragmatic-ddd i18nnext-intli18nextLocaleProviderdetermineLocalesetLocaleActionexport const LOCALES = ["en", "es"] as const;
export const DEFAULT_LOCALE = "en";
export type LOCALE = (typeof LOCALES)[number];
export type Translations<T extends object> = Record<LOCALE, T>;constconst translations: Translations<{
title: string;
description: string;
}> = {
en: { title: "Create", description: "Fill details." },
es: { title: "Crear", description: "Complete detalles." },
};translations<component>.i18n.tsCRITICAL: You MUST NOT read the locale from theURL segment (page[locale]). You MUST always useparamsin Server Components ordetermineLocale()in Client Components. The source of truth is the cookie/header injected by middleware.useLocale()
determineLocale()cacheimport { determineLocale } from "@/shared/infrastructure/i18n/utils";
export default async function MyPage() {
const locale = await determineLocale();
const t = translations[locale];
return <h1>{t.title}</h1>;
}useLocale()<LocaleProvider locale={locale}>"use client";
import { useLocale } from "@/shared/presentation/components/LocaleProvider";
export function MyClientComponent() {
const locale = useLocale();
const t = translations[locale];
return <p>{t.description}</p>;
}"use client";
import { setLocaleAction } from "@/shared/infrastructure/i18n/actions";
import { useRouter } from "next/navigation";
export function Switcher() {
const router = useRouter();
const handleSwitch = async (newLocale: string) => {
// MUST call the server action to update the cookie
await setLocaleAction(newLocale);
// MUST trigger a router.refresh() to update all server components
router.refresh();
}
// ...
}