2024-04-12 11:41:00 +08:00
import i18n from '@/locales/config' ;
2024-04-07 17:41:29 +08:00
import { App , ConfigProvider , ConfigProviderProps } from 'antd' ;
import enUS from 'antd/locale/en_US' ;
import zhCN from 'antd/locale/zh_CN' ;
2024-04-12 11:41:00 +08:00
import zh_HK from 'antd/locale/zh_HK' ;
2024-04-07 17:41:29 +08:00
import React , { ReactNode , useEffect , useState } from 'react' ;
import storage from './utils/authorizationUtil' ;
2024-04-12 11:41:00 +08:00
const AntLanguageMap = {
en : enUS ,
zh : zhCN ,
'zh-TRADITIONAL' : zh_HK ,
} ;
2024-04-07 17:41:29 +08:00
type Locale = ConfigProviderProps [ 'locale' ] ;
const RootProvider = ( { children } : React . PropsWithChildren ) = > {
2024-04-12 11:41:00 +08:00
const getLocale = ( lng : string ) = >
AntLanguageMap [ lng as keyof typeof AntLanguageMap ] ? ? enUS ;
2024-04-07 17:41:29 +08:00
const [ locale , setLocal ] = useState < Locale > ( getLocale ( storage . getLanguage ( ) ) ) ;
2024-04-12 11:41:00 +08:00
i18n . on ( 'languageChanged' , function ( lng : string ) {
2024-04-07 17:41:29 +08:00
storage . setLanguage ( lng ) ;
setLocal ( getLocale ( lng ) ) ;
} ) ;
useEffect ( ( ) = > {
2024-04-08 10:41:03 +08:00
// Because the language is saved in the backend, a token is required to obtain the api. However, the login page cannot obtain the language through the getUserInfo api, so the language needs to be saved in localstorage.
const lng = storage . getLanguage ( ) ;
if ( lng ) {
i18n . changeLanguage ( lng ) ;
}
} , [ ] ) ;
2024-01-29 15:02:27 +08:00
2024-04-02 15:44:09 +08:00
return (
< ConfigProvider
theme = { {
2024-03-20 18:20:42 +08:00
token : {
fontFamily : 'Inter' ,
} ,
2024-04-02 15:44:09 +08:00
} }
2024-04-07 17:41:29 +08:00
locale = { locale }
2024-04-02 15:44:09 +08:00
>
2024-04-07 17:41:29 +08:00
< App > { children } < / App >
2024-04-02 15:44:09 +08:00
< / ConfigProvider >
2024-03-20 18:20:42 +08:00
) ;
2024-04-07 17:41:29 +08:00
} ;
export function rootContainer ( container : ReactNode ) {
return < RootProvider > { container } < / RootProvider > ;
2024-01-29 15:02:27 +08:00
}