2020-09-12 03:34:18 +00:00
|
|
|
import { inject, defineComponent, VNodeTypes, PropType } from 'vue';
|
2020-10-13 09:46:52 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2019-01-12 03:33:27 +00:00
|
|
|
import defaultLocaleData from './default';
|
2020-09-29 14:06:01 +00:00
|
|
|
import { Locale } from '.';
|
2018-02-23 08:14:59 +00:00
|
|
|
|
2020-08-15 07:33:47 +00:00
|
|
|
export interface LocaleReceiverProps {
|
|
|
|
componentName?: string;
|
2020-09-29 14:06:01 +00:00
|
|
|
defaultLocale?: Locale | Function;
|
|
|
|
children: (locale: Locale, localeCode?: string, fullLocale?: Locale) => VNodeTypes;
|
2020-08-15 07:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface LocaleInterface {
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:46:52 +00:00
|
|
|
export interface LocaleReceiverContext {
|
|
|
|
antLocale?: LocaleInterface;
|
|
|
|
}
|
|
|
|
|
2020-08-15 16:21:58 +00:00
|
|
|
export default defineComponent({
|
2020-03-26 09:26:23 +00:00
|
|
|
name: 'LocaleReceiver',
|
2020-08-15 16:21:58 +00:00
|
|
|
props: {
|
2020-10-13 09:46:52 +00:00
|
|
|
componentName: PropTypes.string,
|
2020-08-15 16:21:58 +00:00
|
|
|
defaultLocale: {
|
|
|
|
type: [Object, Function],
|
|
|
|
},
|
|
|
|
children: {
|
|
|
|
type: Function as PropType<
|
2020-10-19 15:09:36 +00:00
|
|
|
(locale: any, localeCode?: string, fullLocale?: object) => VNodeTypes
|
2020-08-15 16:21:58 +00:00
|
|
|
>,
|
|
|
|
},
|
|
|
|
},
|
2020-06-22 15:16:47 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-10-13 09:46:52 +00:00
|
|
|
localeData: inject<LocaleReceiverContext>('localeData', {}),
|
2020-06-22 15:16:47 +00:00
|
|
|
};
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
getLocale() {
|
2020-08-15 07:33:47 +00:00
|
|
|
const { componentName = 'global', defaultLocale } = this;
|
|
|
|
const locale =
|
|
|
|
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
|
2019-01-12 03:33:27 +00:00
|
|
|
const { antLocale } = this.localeData;
|
2019-01-05 02:40:34 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
2018-02-23 08:14:59 +00:00
|
|
|
return {
|
2019-01-05 02:40:34 +00:00
|
|
|
...(typeof locale === 'function' ? locale() : locale),
|
2018-02-23 08:14:59 +00:00
|
|
|
...(localeFromContext || {}),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getLocaleCode() {
|
|
|
|
const { antLocale } = this.localeData;
|
|
|
|
const localeCode = antLocale && antLocale.locale;
|
2018-02-23 08:14:59 +00:00
|
|
|
// Had use LocaleProvide but didn't set locale
|
|
|
|
if (antLocale && antLocale.exist && !localeCode) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return defaultLocaleData.locale;
|
2018-02-23 08:14:59 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return localeCode;
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-05-28 11:45:50 +00:00
|
|
|
const { $slots } = this;
|
|
|
|
const children = this.children || $slots.default;
|
2020-03-07 11:45:13 +00:00
|
|
|
const { antLocale } = this.localeData;
|
2020-07-14 13:51:48 +00:00
|
|
|
return children?.(this.getLocale(), this.getLocaleCode(), antLocale);
|
2018-02-23 08:14:59 +00:00
|
|
|
},
|
2020-08-15 07:33:47 +00:00
|
|
|
});
|