You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/locale-provider/LocaleReceiver.tsx

69 lines
1.9 KiB

import { inject, defineComponent, VNodeTypes, PropType } from 'vue';
import PropTypes from '../_util/vue-types';
import defaultLocaleData from './default';
import { Locale } from '.';
export interface LocaleReceiverProps {
componentName?: string;
defaultLocale?: Locale | Function;
children: (locale: Locale, localeCode?: string, fullLocale?: Locale) => VNodeTypes;
}
interface LocaleInterface {
[key: string]: any;
}
export interface LocaleReceiverContext {
antLocale?: LocaleInterface;
}
export default defineComponent({
name: 'LocaleReceiver',
props: {
componentName: PropTypes.string,
defaultLocale: {
type: [Object, Function],
},
children: {
type: Function as PropType<
(locale: object, localeCode?: string, fullLocale?: object) => VNodeTypes
>,
},
},
setup() {
return {
localeData: inject<LocaleReceiverContext>('localeData', {}),
};
},
methods: {
getLocale() {
const { componentName = 'global', defaultLocale } = this;
const locale =
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const { antLocale } = this.localeData;
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? locale() : locale),
...(localeFromContext || {}),
};
},
getLocaleCode() {
const { antLocale } = this.localeData;
const localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode;
},
},
render() {
const { $slots } = this;
const children = this.children || $slots.default;
const { antLocale } = this.localeData;
return children?.(this.getLocale(), this.getLocaleCode(), antLocale);
},
});