ant-design/components/locale-provider/index.tsx

85 lines
2.4 KiB
TypeScript
Raw Normal View History

import type { ValidateMessages } from 'rc-field-form/lib/interface';
2022-06-22 14:57:09 +08:00
import * as React from 'react';
import warning from '../_util/warning';
2022-06-22 14:57:09 +08:00
import type { PickerLocale as DatePickerLocale } from '../date-picker/generatePicker';
import type { TransferLocale as TransferLocaleForEmpty } from '../empty';
import type { ModalLocale } from '../modal/locale';
import { changeConfirmLocale } from '../modal/locale';
import type { PaginationLocale } from '../pagination/Pagination';
import type { PopconfirmLocale } from '../popconfirm/PurePanel';
2022-06-22 14:57:09 +08:00
import type { TableLocale } from '../table/interface';
import type { TransferLocale } from '../transfer';
2022-06-22 14:57:09 +08:00
import type { UploadLocale } from '../upload/interface';
import type { LocaleContextProps } from './context';
import LocaleContext from './context';
2020-03-11 14:56:01 +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;
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;
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>;
Text?: {
edit?: any;
copy?: any;
copied?: any;
expand?: any;
};
Form?: {
optional?: string;
defaultValidateMessages: ValidateMessages;
};
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 */
_ANT_MARK__?: string;
2016-08-02 16:10:26 +08:00
}
const LocaleProvider: React.FC<LocaleProviderProps> = props => {
const { locale = {} as Locale, children, _ANT_MARK__ } = props;
if (process.env.NODE_ENV !== 'production') {
warning(
_ANT_MARK__ === ANT_MARK,
'LocaleProvider',
'`LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead: http://u.ant.design/locale',
);
}
React.useEffect(() => {
changeConfirmLocale(locale && locale.Modal);
return () => {
changeConfirmLocale();
};
}, [locale]);
const getMemoizedContextValue = React.useMemo<LocaleContextProps>(
() => ({ ...locale, exist: true }),
[locale],
);
return (
<LocaleContext.Provider value={getMemoizedContextValue}>{children}</LocaleContext.Provider>
);
};
export default LocaleProvider;