56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useI18nContext } from "./context";
|
|
import { getLocaleFromCookie, setLocaleInCookie } from "./cookies";
|
|
import { enUS } from "./locales/en-US";
|
|
import { zhCN } from "./locales/zh-CN";
|
|
|
|
import {
|
|
DEFAULT_LOCALE,
|
|
detectLocale,
|
|
normalizeLocale,
|
|
type Locale,
|
|
type Translations,
|
|
} from "./index";
|
|
|
|
const translations: Record<Locale, Translations> = {
|
|
"en-US": enUS,
|
|
"zh-CN": zhCN,
|
|
};
|
|
|
|
export function useI18n() {
|
|
const { locale, setLocale } = useI18nContext();
|
|
|
|
const t = translations[locale] ?? translations[DEFAULT_LOCALE];
|
|
|
|
const changeLocale = (newLocale: Locale) => {
|
|
setLocale(newLocale);
|
|
setLocaleInCookie(newLocale);
|
|
};
|
|
|
|
// Initialize locale on mount
|
|
useEffect(() => {
|
|
const saved = getLocaleFromCookie();
|
|
if (saved) {
|
|
const normalizedSaved = normalizeLocale(saved);
|
|
setLocale(normalizedSaved);
|
|
if (saved !== normalizedSaved) {
|
|
setLocaleInCookie(normalizedSaved);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const detected = detectLocale();
|
|
setLocale(detected);
|
|
setLocaleInCookie(detected);
|
|
}, [setLocale]);
|
|
|
|
return {
|
|
locale,
|
|
t,
|
|
changeLocale,
|
|
};
|
|
}
|