ant-design-vue/components/locale-provider/index.jsx

78 lines
1.6 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import * as moment from 'moment';
import interopDefault from '../_util/interopDefault';
import { changeConfirmLocale } from '../modal/locale';
2018-02-23 16:14:59 +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;
// }
2019-01-12 11:33:27 +08:00
function setMomentLocale(locale) {
2018-02-23 16:14:59 +08:00
if (locale && locale.locale) {
2019-01-12 11:33:27 +08:00
interopDefault(moment).locale(locale.locale);
2018-02-23 16:14:59 +08:00
} else {
2019-01-12 11:33:27 +08:00
interopDefault(moment).locale('en');
2018-02-23 16:14:59 +08:00
}
}
const LocaleProvider = {
2018-04-08 21:17:20 +08:00
name: 'ALocaleProvider',
2018-02-23 16:14:59 +08:00
props: {
locale: PropTypes.object.def({}),
},
2019-01-12 11:33:27 +08:00
data() {
2018-02-23 16:14:59 +08:00
return {
antLocale: {
...this.locale,
exist: true,
},
2019-01-12 11:33:27 +08:00
};
2018-02-23 16:14:59 +08:00
},
2019-01-12 11:33:27 +08:00
provide() {
2018-03-15 22:21:25 +08:00
return {
localeData: this.$data,
2019-01-12 11:33:27 +08:00
};
2018-03-15 22:21:25 +08:00
},
2018-02-23 16:14:59 +08:00
watch: {
2019-01-12 11:33:27 +08:00
locale(val) {
2018-03-15 22:21:25 +08:00
this.antLocale = {
...this.locale,
exist: true,
2019-01-12 11:33:27 +08:00
};
2019-05-14 22:15:24 +08:00
setMomentLocale(val);
2018-02-23 16:14:59 +08:00
},
},
2019-01-12 11:33:27 +08:00
created() {
const { locale } = this;
setMomentLocale(locale);
changeConfirmLocale(locale && locale.Modal);
2018-02-23 16:14:59 +08:00
},
2019-01-12 11:33:27 +08:00
updated() {
const { locale } = this;
changeConfirmLocale(locale && locale.Modal);
2018-02-23 16:14:59 +08:00
},
2019-01-12 11:33:27 +08:00
beforeDestroy() {
changeConfirmLocale();
2018-02-23 16:14:59 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
return this.$slots.default ? this.$slots.default[0] : null;
2018-02-23 16:14:59 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-02-23 16:14:59 +08:00
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
LocaleProvider.install = function(Vue) {
Vue.component(LocaleProvider.name, LocaleProvider);
};
2019-01-12 11:33:27 +08:00
export default LocaleProvider;