import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import Backend from 'i18next-http-backend'; // 引入 Backend 插件 type I18NextProviderProps = { children: React.ReactNode; }; export const initI18n = (basename: string) => { // 初始化 i18n i18n .use(Backend) // 使用 Backend 插件 .use(initReactI18next) .init({ backend: { loadPath: `${basename}/locales/{{lng}}/{{ns}}.json`, // 指定 JSON 文件的路径 }, lng: 'zh', // 默认语言 fallbackLng: 'en', // 备用语言 interpolation: { escapeValue: false, // react 已经安全地处理了转义 }, }); }; /** * 国际化组件,初始化 * @param props * @returns */ export const I18NextProvider = (props: I18NextProviderProps) => { const { children } = props; return <>{children}; };