LocaleProvider动态切换confirm组件使用场景的语言
在使用LocaleProvider的时候
// 单例单独存储Modal语言配置
let runtimeLocale: ModalLocale = {
...(defaultLocale.Modal as ModalLocale),
};
// 构造函数切换modal语言配置
constructor() {
changeConfirmLocale(props.locale && props.locale.Modal);
}
// 组件更新切换modal语言配置
componentDidUpdate(prevProps: LocaleProviderProps) {
const { locale } = this.props;
if (prevProps.locale !== locale) {
changeConfirmLocale(locale && locale.Modal);
}
}
// 组件卸载切换modal语言配置
componentWillUnmount() {
changeConfirmLocale();
}
也就是在主组件 LocalProvider管理语言切换 监听到主组件中语言切换了之后,更新modal的语言包
LocaleProvider提供动态语言包,LocalReceiver消费语言,充当Consumer
local格式定义,创建context
// Locale
export interface Locale {
locale: string;
Pagination?: PaginationLocale;
Text?: {
edit?: any;
copy?: any;
};
Form?: {
defaultValidateMessages: ValidateMessages;
};
...
}
// Context
// Partial 使全属性可选 给每个属性都加上问号可选 xxx?: XXX
const LocaleContext = createContext<(Partial<Locale> & { exist?: boolean }) | undefined>(undefined);