"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, translator, type Locale, type Translator } from "@peppa/shared/i18n"; import { setLanguage } from "@/app/locale-actions"; import { cn } from "@/lib/utils"; const LocaleContext = createContext("ru"); export function I18nProvider({ locale, children }: { locale: Locale; children: ReactNode }) { 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. * `compact` (PEP-136) is a native list for the phone top bar, where three buttons do not fit. */ export function LanguageSwitcher({ className, compact = false }: { className?: string; compact?: boolean }) { const current = useLocale(); const t = useT(); const router = useRouter(); const [pending, start] = useTransition(); const choose = (l: string) => start(async () => { await setLanguage(l); router.refresh(); }); if (compact) { return ( ); } return (
{LOCALES.map((l) => ( ))}
); }