import React from "react" ; import type { IntlShape } from "react-intl" ; import { createIntl , IntlProvider } from "react-intl" ; import antdEnUS from "antd/lib/locale/en_US" ; import antdZhCN from "antd/lib/locale/zh_CN" ; import enLn from "./components/ln-en" ; import zhLn from "./components/ln-zh-cn" ; let gIntl ; let defaultLanguage = "zh-cn" ; const currentLocalName = localStorage . getItem ( "why__locale" ) || defaultLanguage ; if ( ! localStorage . getItem ( "why__locale" ) ) { localStorage . setItem ( "why__locale" , defaultLanguage ) ; } /**
 * 本地默认的语言配置
 */ export const localeInfo = { en : { messages : enLn , locale : "en" , antd : antdEnUS , momentLocale : "" , } , "zh-cn" : { messages : zhLn , locale : "zh-cn" , antd : antdZhCN , momentLocale : "zh-cn" , } , } ; const currentLocaleInfo = localeInfo [ defaultLanguage ] ; param lang function setDefaultLanguage ( lang ) { defaultLanguage = lang ; } param props returns export const LocaleProvider = ( props ) => { return < IntlProvider locale = { getLocale ( ) } > { props . children } < /IntlProvider>; } ; param locale param changeIntl returns const getIntl = ( locale ? , changeIntl ? ) => { if ( gIntl && ! changeIntl && ! locale ) { return gIntl ; } if ( locale && localeInfo [ locale ] ) { return createIntl ( localeInfo [ locale ] ) ; } if ( localeInfo [ defaultLanguage ] ) return createIntl ( localeInfo [ defaultLanguage ] ) ; if ( localeInfo [ "zh-cn" ] ) return createIntl ( localeInfo [ "zh-cn" ] ) ; if ( ! locale || ! ! localeInfo [ locale ] ) { throw new Error ( "The current popular language does not exist, please check the locales folder!" ) ; } return createIntl ( { locale : "zh-cn" , messages : { } , } ) ; } ; param locale const setIntl = ( locale ) => { gIntl = getIntl ( locale , true ) ; } ; returns export function getLocale ( ) { return currentLocalName ; } param lang returns export const setLocale = ( lang ) => { if ( typeof window . localStorage !== "undefined" ) { window . localStorage . setItem ( "why__locale" , lang || "" ) ; } window . location . reload ( ) ; } ; interface MessageDescriptor { id ? : string ; description ? : string | any ; defaultMessage ? : string ; } param descriptor param values export const formatMessage = ( descriptor , values ? ) => { if ( ! gIntl ) { setIntl ( getLocale ( ) ) ; } return gIntl . formatMessage ( descriptor , values ) ; } ; returns export const getLocaleInfo = ( ) => { return currentLocaleInfo ; } ; export { createIntl } ;