2020-06-22 23:16:47 +08:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import defaultLocaleData from './default';
|
2018-02-23 16:14:59 +08:00
|
|
|
|
|
|
|
export default {
|
2020-03-26 17:26:23 +08:00
|
|
|
name: 'LocaleReceiver',
|
2018-02-23 16:14:59 +08:00
|
|
|
props: {
|
2019-01-05 10:40:34 +08:00
|
|
|
componentName: PropTypes.string.def('global'),
|
2019-01-12 11:33:27 +08:00
|
|
|
defaultLocale: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
2018-02-23 16:14:59 +08:00
|
|
|
children: PropTypes.func,
|
|
|
|
},
|
2020-06-22 23:16:47 +08:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
localeData: inject('localeData', {}),
|
|
|
|
};
|
2018-02-23 16:14:59 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
getLocale() {
|
|
|
|
const { componentName, defaultLocale } = this;
|
|
|
|
const locale = defaultLocale || defaultLocaleData[componentName || 'global'];
|
|
|
|
const { antLocale } = this.localeData;
|
2019-01-05 10:40:34 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
2018-02-23 16:14:59 +08:00
|
|
|
return {
|
2019-01-05 10:40:34 +08:00
|
|
|
...(typeof locale === 'function' ? locale() : locale),
|
2018-02-23 16:14:59 +08:00
|
|
|
...(localeFromContext || {}),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-02-23 16:14:59 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getLocaleCode() {
|
|
|
|
const { antLocale } = this.localeData;
|
|
|
|
const localeCode = antLocale && antLocale.locale;
|
2018-02-23 16:14:59 +08:00
|
|
|
// Had use LocaleProvide but didn't set locale
|
|
|
|
if (antLocale && antLocale.exist && !localeCode) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return defaultLocaleData.locale;
|
2018-02-23 16:14:59 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return localeCode;
|
2018-02-23 16:14:59 +08:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-05-28 19:45:50 +08:00
|
|
|
const { $slots } = this;
|
|
|
|
const children = this.children || $slots.default;
|
2020-03-07 19:45:13 +08:00
|
|
|
const { antLocale } = this.localeData;
|
|
|
|
return children(this.getLocale(), this.getLocaleCode(), antLocale);
|
2018-02-23 16:14:59 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|