import React, { useMemo, useContext, createContext, memo } from 'react'; import { en_US, es_ES, zh_CN, zh_HK, pt_BR, fr_FR, id_ID, vi_VN } from './lang'; import type { LocaleProviderProps, Locale } from './interface'; import { LangType } from './enum'; import defaultLocale from './lang/zh_CN'; const LocaleContext = createContext(defaultLocale); const LangContext = createContext(LangType.zh_CN); export const useLocale = () => useContext(LocaleContext); export const useLangType = () => useContext(LangContext); export const getLocale = (langType: LangType) => { switch (langType) { case LangType.en_US: return en_US; case LangType.zh_HK: return zh_HK; case LangType.zh_CN: return zh_CN; case LangType.es_ES: return es_ES; case LangType.pt_BR: return pt_BR; case LangType.fr_FR: return fr_FR; case LangType.id_ID: return id_ID; case LangType.vi_VN: return vi_VN; default: return zh_CN; } }; const LocaleProvider: React.FC = ({ children, langType = LangType.zh_CN, }) => { const state = useMemo( () => ({ ...defaultLocale, ...getLocale(langType), }), [langType] ); return ( {children} ); }; export default memo(LocaleProvider);