2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2018-01-13 17:53:25 +08:00
|
|
|
import * as moment from 'moment';
|
2018-03-29 10:42:33 +08:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
2017-07-31 01:50:46 +08:00
|
|
|
import { ModalLocale, changeConfirmLocale } from '../modal/locale';
|
2019-07-24 10:34:55 +08:00
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
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;
|
|
|
|
Pagination?: Object;
|
|
|
|
DatePicker?: Object;
|
|
|
|
TimePicker?: Object;
|
|
|
|
Calendar?: Object;
|
|
|
|
Table?: Object;
|
|
|
|
Modal?: ModalLocale;
|
|
|
|
Popconfirm?: Object;
|
|
|
|
Transfer?: Object;
|
|
|
|
Select?: Object;
|
|
|
|
Upload?: Object;
|
|
|
|
}
|
|
|
|
|
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;
|
2019-07-24 10:34:55 +08:00
|
|
|
_ANT_MARK__?: string;
|
2016-08-02 16:10:26 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 17:53:25 +08:00
|
|
|
function setMomentLocale(locale: Locale) {
|
|
|
|
if (locale && locale.locale) {
|
2018-03-29 10:42:33 +08:00
|
|
|
interopDefault(moment).locale(locale.locale);
|
2018-01-15 14:56:15 +08:00
|
|
|
} else {
|
2018-03-29 10:42:33 +08:00
|
|
|
interopDefault(moment).locale('en');
|
2018-01-13 17:53:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:10:26 +08:00
|
|
|
export default class LocaleProvider extends React.Component<LocaleProviderProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static propTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
locale: PropTypes.object,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-01-13 17:53:25 +08:00
|
|
|
static defaultProps = {
|
|
|
|
locale: {},
|
|
|
|
};
|
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static childContextTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
antLocale: PropTypes.object,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-04-15 15:09:13 +08:00
|
|
|
constructor(props: LocaleProviderProps) {
|
|
|
|
super(props);
|
|
|
|
setMomentLocale(props.locale);
|
|
|
|
changeConfirmLocale(props.locale && props.locale.Modal);
|
2019-07-24 10:34:55 +08:00
|
|
|
|
|
|
|
warning(
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-03 16:28:02 +08:00
|
|
|
getChildContext() {
|
|
|
|
return {
|
2017-01-09 16:25:58 +08:00
|
|
|
antLocale: {
|
|
|
|
...this.props.locale,
|
|
|
|
exist: true,
|
|
|
|
},
|
2016-03-03 16:28:02 +08:00
|
|
|
};
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
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) {
|
|
|
|
setMomentLocale(locale);
|
2018-01-13 17:53:25 +08:00
|
|
|
}
|
2016-03-05 17:14:45 +08:00
|
|
|
changeConfirmLocale(locale && locale.Modal);
|
|
|
|
}
|
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
|
|
|
|
2016-03-03 16:28:02 +08:00
|
|
|
render() {
|
2016-03-05 16:39:27 +08:00
|
|
|
return React.Children.only(this.props.children);
|
2016-03-03 16:28:02 +08:00
|
|
|
}
|
|
|
|
}
|