2021-11-26 12:18:21 +08:00
|
|
|
import memoizeOne from 'memoize-one';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ValidateMessages } from 'rc-field-form/lib/interface';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2019-07-24 10:34:55 +08:00
|
|
|
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { PickerLocale as DatePickerLocale } from '../date-picker/generatePicker';
|
|
|
|
import type { TransferLocale as TransferLocaleForEmpty } from '../empty';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ModalLocale } from '../modal/locale';
|
|
|
|
import { changeConfirmLocale } from '../modal/locale';
|
|
|
|
import type { PaginationLocale } from '../pagination/Pagination';
|
2022-07-15 11:11:50 +08:00
|
|
|
import type { PopconfirmLocale } from '../popconfirm/PurePanel';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TableLocale } from '../table/interface';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { TransferLocale } from '../transfer';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { UploadLocale } from '../upload/interface';
|
2020-03-30 22:54:12 +08:00
|
|
|
import LocaleContext from './context';
|
2020-03-11 14:56:01 +08:00
|
|
|
|
2019-07-24 10:34:55 +08:00
|
|
|
export const ANT_MARK = 'internalMark';
|
2016-03-03 16:28:02 +08:00
|
|
|
|
2018-01-13 17:53:25 +08:00
|
|
|
export interface Locale {
|
|
|
|
locale: string;
|
2020-03-11 14:56:01 +08:00
|
|
|
Pagination?: PaginationLocale;
|
2020-03-12 10:49:32 +08:00
|
|
|
DatePicker?: DatePickerLocale;
|
2021-06-03 09:36:04 +08:00
|
|
|
TimePicker?: Record<string, any>;
|
|
|
|
Calendar?: Record<string, any>;
|
2020-03-11 14:56:01 +08:00
|
|
|
Table?: TableLocale;
|
2018-01-13 17:53:25 +08:00
|
|
|
Modal?: ModalLocale;
|
2020-03-11 14:56:01 +08:00
|
|
|
Popconfirm?: PopconfirmLocale;
|
2022-10-01 22:17:14 +08:00
|
|
|
Transfer?: TransferLocale;
|
2021-06-03 09:36:04 +08:00
|
|
|
Select?: Record<string, any>;
|
2020-03-11 14:56:01 +08:00
|
|
|
Upload?: UploadLocale;
|
|
|
|
Empty?: TransferLocaleForEmpty;
|
2021-06-03 09:36:04 +08:00
|
|
|
global?: Record<string, any>;
|
|
|
|
PageHeader?: { back: string };
|
|
|
|
Icon?: Record<string, any>;
|
2022-01-18 14:13:41 +08:00
|
|
|
Text?: {
|
|
|
|
edit?: any;
|
|
|
|
copy?: any;
|
|
|
|
copied?: any;
|
|
|
|
expand?: any;
|
|
|
|
};
|
2020-04-18 13:18:51 +08:00
|
|
|
Form?: {
|
2020-08-21 12:58:14 +08:00
|
|
|
optional?: string;
|
2020-04-18 13:18:51 +08:00
|
|
|
defaultValidateMessages: ValidateMessages;
|
|
|
|
};
|
2020-12-07 17:08:48 +08:00
|
|
|
Image?: {
|
|
|
|
preview: string;
|
|
|
|
};
|
2018-01-13 17:53:25 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:10:26 +08:00
|
|
|
export interface LocaleProviderProps {
|
2018-01-13 17:53:25 +08:00
|
|
|
locale: Locale;
|
2018-12-13 22:03:12 +08:00
|
|
|
children?: React.ReactNode;
|
2022-10-06 18:53:06 +08:00
|
|
|
/** @internal */
|
2019-07-24 10:34:55 +08:00
|
|
|
_ANT_MARK__?: string;
|
2016-08-02 16:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class LocaleProvider extends React.Component<LocaleProviderProps, any> {
|
2018-01-13 17:53:25 +08:00
|
|
|
static defaultProps = {
|
|
|
|
locale: {},
|
|
|
|
};
|
|
|
|
|
2018-04-15 15:09:13 +08:00
|
|
|
constructor(props: LocaleProviderProps) {
|
|
|
|
super(props);
|
|
|
|
changeConfirmLocale(props.locale && props.locale.Modal);
|
2019-07-24 10:34:55 +08:00
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2019-07-24 10:34:55 +08:00
|
|
|
props._ANT_MARK__ === ANT_MARK,
|
|
|
|
'LocaleProvider',
|
|
|
|
'`LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead: http://u.ant.design/locale',
|
|
|
|
);
|
2018-04-15 15:09:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-02 18:05:20 +08:00
|
|
|
componentDidMount() {
|
|
|
|
changeConfirmLocale(this.props.locale && this.props.locale.Modal);
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:38:27 +08:00
|
|
|
componentDidUpdate(prevProps: LocaleProviderProps) {
|
2018-01-13 17:53:25 +08:00
|
|
|
const { locale } = this.props;
|
2018-11-21 22:38:27 +08:00
|
|
|
if (prevProps.locale !== locale) {
|
2019-09-16 11:23:26 +08:00
|
|
|
changeConfirmLocale(locale && locale.Modal);
|
2018-01-13 17:53:25 +08:00
|
|
|
}
|
2016-03-05 17:14:45 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-01-13 17:53:25 +08:00
|
|
|
componentWillUnmount() {
|
2016-03-05 17:14:45 +08:00
|
|
|
changeConfirmLocale();
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2022-01-14 18:34:27 +08:00
|
|
|
getMemoizedContextValue = memoizeOne((localeValue: Locale): Locale & { exist?: boolean } => ({
|
|
|
|
...localeValue,
|
|
|
|
exist: true,
|
|
|
|
}));
|
|
|
|
|
2016-03-03 16:28:02 +08:00
|
|
|
render() {
|
2020-03-30 22:54:12 +08:00
|
|
|
const { locale, children } = this.props;
|
2022-01-14 18:34:27 +08:00
|
|
|
const contextValue = this.getMemoizedContextValue(locale);
|
2021-11-26 12:18:21 +08:00
|
|
|
return <LocaleContext.Provider value={contextValue}>{children}</LocaleContext.Provider>;
|
2016-03-03 16:28:02 +08:00
|
|
|
}
|
|
|
|
}
|