"use client"; // Interface language in client components (PEP-98): the root layout passes the locale, // `useT()` gives a translator over the bundled dictionaries. import { createContext, useContext, useMemo, useTransition, type ReactNode } from "react"; import { useRouter } from "next/navigation"; import { LOCALE_LABELS, LOCALES, registerDictionary, translator, type Dict, type Locale, type Translator } from "@peppa/shared/i18n"; import { setLanguage } from "@/app/locale-actions"; import { cn } from "@/lib/utils"; const LocaleContext = createContext("ru"); /** `dict` — the dictionary of `locale` (not for Russian): the browser bundle carries none (PEP-217). */ export function I18nProvider({ locale, dict, children }: { locale: Locale; dict?: Dict; children: ReactNode }) { // Before the children render, on the server and on hydration alike; idempotent. if (dict) registerDictionary(locale, dict); return {children}; } export function useLocale(): Locale { return useContext(LocaleContext); } export function useT(): Translator { const locale = useLocale(); return useMemo(() => translator(locale), [locale]); } /** RU / EN / KZ: saves the cookie (and the user's choice when logged in), then re-renders. */ export function LanguageSwitcher({ className }: { className?: string }) { const current = useLocale(); const t = useT(); const router = useRouter(); const [pending, start] = useTransition(); return (
{LOCALES.map((l) => ( ))}
); }