Implement full internationalization for the Next.js backoffice: - i18n infrastructure: type-safe dictionaries (fr.ts/en.ts), cookie-based locale detection, React Context for client components, server-side translation helper - Language selector in Settings page (General tab) with cookie + DB persistence - All ~35 pages and components translated via t() / useTranslation() - Default locale set to English, French available via settings Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type { Locale } from "./types";
|
|
import type { TranslationKey } from "./fr";
|
|
|
|
const dictionaries: Record<Locale, () => Promise<Record<TranslationKey, string>>> = {
|
|
fr: () => import("./fr").then((m) => m.default),
|
|
en: () => import("./en").then((m) => m.default),
|
|
};
|
|
|
|
export async function getDictionary(locale: Locale): Promise<Record<TranslationKey, string>> {
|
|
return dictionaries[locale]();
|
|
}
|
|
|
|
// Synchronous versions for client-side use
|
|
import fr from "./fr";
|
|
import en from "./en";
|
|
|
|
const dictionariesSync: Record<Locale, Record<TranslationKey, string>> = { fr, en };
|
|
|
|
export function getDictionarySync(locale: Locale): Record<TranslationKey, string> {
|
|
return dictionariesSync[locale];
|
|
}
|
|
|
|
export type TranslateFunction = (key: TranslationKey, params?: Record<string, string | number>) => string;
|
|
|
|
export function createTranslateFunction(dict: Record<TranslationKey, string>): TranslateFunction {
|
|
return (key: TranslationKey, params?: Record<string, string | number>): string => {
|
|
let value: string = dict[key] ?? key;
|
|
if (params) {
|
|
for (const [k, v] of Object.entries(params)) {
|
|
value = value.replace(new RegExp(`\\{\\{${k}\\}\\}`, "g"), String(v));
|
|
}
|
|
}
|
|
return value;
|
|
};
|
|
}
|