/* eslint-disable @typescript-eslint/camelcase */ import { useEffect, useState } from "react"; import zh_CN from "./locale/zh_CN"; import en_US from "./locale/en_US"; import { useConfig } from "../_util/config-context"; import { translationMap, Locale } from "./translationMap"; // 约定中语言名不规范 const lngs = { zh: "zh_CN", en: "en_US", jp: "ja_JP", ko: "ko_KR", }; export function setLocale() { console.error("`setLocale()` 方法已废弃"); } export function useTranslation(moment?: typeof import("moment")): Locale { const { locale: configLocale } = useConfig(); const t = typeof configLocale === "string" ? translationMap[configLocale] || zh_CN : { ...zh_CN, ...en_US, ...configLocale }; const locale = lngs[t.locale] || t.locale; useState(() => { if (moment) { moment.locale(locale); } }); useEffect(() => { if (moment) { moment.locale(locale); } }, [locale]); // eslint-disable-line react-hooks/exhaustive-deps return t; } export interface WithTranslationProps { /** * @ignore */ t?: Locale; } /* eslint-enable @typescript-eslint/camelcase */