2020-10-13 09:46:52 +00:00
|
|
|
import { provide, App, defineComponent, VNode, PropType } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-08-31 14:25:47 +00:00
|
|
|
import moment from 'moment';
|
2019-01-12 03:33:27 +00:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
2020-08-15 07:33:47 +00:00
|
|
|
import { ModalLocale, changeConfirmLocale } from '../modal/locale';
|
2020-03-07 11:45:13 +00:00
|
|
|
import warning from '../_util/warning';
|
2020-06-22 15:16:47 +00:00
|
|
|
import { getSlot } from '../_util/props-util';
|
2020-08-15 07:33:47 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LocaleProviderProps {
|
|
|
|
locale: Locale;
|
|
|
|
children?: VNode | VNode[];
|
2020-10-13 10:04:02 +00:00
|
|
|
ANT_MARK__?: string;
|
2020-08-15 07:33:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
export const ANT_MARK = 'internalMark';
|
2020-08-15 07:33:47 +00:00
|
|
|
|
|
|
|
function setMomentLocale(locale?: Locale) {
|
2018-02-23 08:14:59 +00:00
|
|
|
if (locale && locale.locale) {
|
2019-01-12 03:33:27 +00:00
|
|
|
interopDefault(moment).locale(locale.locale);
|
2018-02-23 08:14:59 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
interopDefault(moment).locale('en');
|
2018-02-23 08:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 07:33:47 +00:00
|
|
|
const LocaleProvider = defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ALocaleProvider',
|
2018-02-23 08:14:59 +00:00
|
|
|
props: {
|
2020-08-15 16:21:58 +00:00
|
|
|
locale: {
|
2020-10-13 09:46:52 +00:00
|
|
|
type: Object as PropType<Locale>,
|
2020-08-15 16:21:58 +00:00
|
|
|
},
|
2020-10-13 10:04:02 +00:00
|
|
|
ANT_MARK__: PropTypes.string,
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2020-03-07 11:45:13 +00:00
|
|
|
warning(
|
2020-10-13 10:04:02 +00:00
|
|
|
this.ANT_MARK__ === ANT_MARK,
|
2020-03-07 11:45:13 +00:00
|
|
|
'LocaleProvider',
|
|
|
|
'`LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead',
|
|
|
|
);
|
2018-02-23 08:14:59 +00:00
|
|
|
return {
|
|
|
|
antLocale: {
|
|
|
|
...this.locale,
|
|
|
|
exist: true,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2020-09-29 14:06:01 +00:00
|
|
|
setup(props) {
|
|
|
|
warning(
|
2020-10-13 10:04:02 +00:00
|
|
|
props.ANT_MARK__ === ANT_MARK,
|
2020-09-29 14:06:01 +00:00
|
|
|
'LocaleProvider',
|
|
|
|
'`LocaleProvider` is deprecated. Please use `locale` with `ConfigProvider` instead',
|
|
|
|
);
|
|
|
|
const data = {
|
|
|
|
antLocale: {
|
|
|
|
...props.locale,
|
|
|
|
exist: true,
|
|
|
|
},
|
2020-10-13 10:04:02 +00:00
|
|
|
ANT_MARK__: ANT_MARK,
|
2020-09-29 14:06:01 +00:00
|
|
|
};
|
|
|
|
provide('localeData', data);
|
|
|
|
return data;
|
|
|
|
},
|
2018-02-23 08:14:59 +00:00
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
locale(val) {
|
2018-03-15 14:21:25 +00:00
|
|
|
this.antLocale = {
|
2020-07-28 10:37:07 +00:00
|
|
|
...val,
|
2018-03-15 14:21:25 +00:00
|
|
|
exist: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2019-05-14 14:15:24 +00:00
|
|
|
setMomentLocale(val);
|
2020-03-07 11:45:13 +00:00
|
|
|
changeConfirmLocale(val && val.Modal);
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
created() {
|
|
|
|
const { locale } = this;
|
|
|
|
setMomentLocale(locale);
|
|
|
|
changeConfirmLocale(locale && locale.Modal);
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-01-12 03:33:27 +00:00
|
|
|
changeConfirmLocale();
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-22 15:16:47 +00:00
|
|
|
return getSlot(this);
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2020-08-15 07:33:47 +00:00
|
|
|
});
|
2018-02-23 08:14:59 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2020-08-15 07:33:47 +00:00
|
|
|
LocaleProvider.install = function(app: App) {
|
2020-06-22 15:16:47 +00:00
|
|
|
app.component(LocaleProvider.name, LocaleProvider);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default LocaleProvider;
|