refactor: locale-provider

This commit is contained in:
tangjinzhou 2023-01-28 08:53:47 +08:00
parent 8110d8cbbd
commit dfe462ec6f
67 changed files with 95 additions and 324 deletions

View File

@ -1,85 +1,5 @@
import type { VNodeTypes, PropType, ComputedRef, Ref } from 'vue';
import { unref, inject, defineComponent, computed } from 'vue';
import defaultLocaleData from './default';
import type { Locale } from '.';
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
import LocaleReceiver from '../locale/LocaleReceiver';
export interface LocaleReceiverProps {
componentName?: string;
defaultLocale?: Locale | Function;
children: (locale: Locale, localeCode?: string, fullLocale?: Locale) => VNodeTypes;
}
export * from '../locale/LocaleReceiver';
interface LocaleInterface {
[key: string]: any;
}
export interface LocaleReceiverContext {
antLocale?: LocaleInterface;
}
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'LocaleReceiver',
props: {
componentName: String as PropType<LocaleComponentName>,
defaultLocale: {
type: [Object, Function],
},
children: {
type: Function as PropType<
(locale: any, localeCode?: string, fullLocale?: object) => VNodeTypes
>,
},
},
setup(props, { slots }) {
const localeData = inject<LocaleReceiverContext>('localeData', {});
const locale = computed(() => {
const { componentName = 'global', defaultLocale } = props;
const locale =
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const { antLocale } = localeData;
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? locale() : locale),
...(localeFromContext || {}),
};
});
const localeCode = computed(() => {
const { antLocale } = localeData;
const localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode;
});
return () => {
const children = props.children || slots.default;
const { antLocale } = localeData;
return children?.(locale.value, localeCode.value, antLocale);
};
},
});
export function useLocaleReceiver<T extends LocaleComponentName>(
componentName: T,
defaultLocale?: Locale[T] | Function | ComputedRef<Locale[T] | Function>,
propsLocale?: Ref<Locale[T]>,
): [ComputedRef<Locale[T]>] {
const localeData = inject<LocaleReceiverContext>('localeData', {} as LocaleReceiverContext);
const componentLocale = computed<Locale[T]>(() => {
const { antLocale } = localeData;
const locale =
unref(defaultLocale) || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? (locale as Function)() : locale),
...(localeFromContext || {}),
...(unref(propsLocale) || {}),
};
});
return [componentLocale];
}
export default LocaleReceiver;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ar_EG';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/bg_BG';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/bn_BD';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ca_ES';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/cs_CZ';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/da_DK';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/de_DE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/en_US';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/el_GR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/en_GB';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/en_US';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/es_ES';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/et_EE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/fa_IR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/fi_FI';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/fr_BE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/fr_FR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ga_IE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/he_IL';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/hi_IN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/hr_HR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/hu_HU';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/hy_AM';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/id_ID';
export default locale;

View File

@ -0,0 +1,5 @@
import locale from '../locale';
export * from '../locale';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/is_IS';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/it_IT';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ja_JP';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ka_GE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/km_KH';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/kn_IN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ko_KR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ku_IQ';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/lv_LV';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/mk_MK';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ml_IN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/mn_MN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ms_MY';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/nb_NO';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ne_NP';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ne_NP';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/nl_BE';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/nl_NL';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/pl_PL';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/pt_BR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/pt_PT';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ro_RO';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ru_RU';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/sk_SK';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/sl_SI';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/sr_RS';
export default locale;

View File

@ -1,2 +0,0 @@
// placeholder
@import '../../style/themes/index';

View File

@ -1 +0,0 @@
import './index.less';

View File

@ -1,3 +0,0 @@
import locale from '../locale/sv_SE';
export default locale;

View File

@ -1,62 +0,0 @@
import Pagination from '../vc-pagination/locale/ta_IN';
import DatePicker from '../date-picker/locale/ta_IN';
import TimePicker from '../time-picker/locale/ta_IN';
import Calendar from '../calendar/locale/ta_IN';
export default {
locale: 'ta',
Pagination,
DatePicker,
TimePicker,
Calendar,
// locales for all comoponents
global: {
placeholder: 'தேதியைத் தேர்ந்தெடுக்கவும்',
},
Table: {
filterTitle: 'பட்டியலை மூடு',
filterConfirm: 'சரி',
filterReset: 'மீட்டமை',
emptyText: 'தகவல் இல்லை',
selectAll: 'அனைத்தையும் தேர்வுசெய்',
selectInvert: 'தலைகீழாக மாற்று',
sortTitle: 'தலைப்பை வரிசைப்படுத்தவும்',
},
Modal: {
okText: 'சரி',
cancelText: 'ரத்து செய்யவும்',
justOkText: 'பரவாயில்லை, சரி',
},
Popconfirm: {
okText: 'சரி',
cancelText: 'ரத்து செய்யவும்',
},
Transfer: {
titles: ['', ''],
notFoundContent: 'உள்ளடக்கம் கிடைக்கவில்லை',
searchPlaceholder: 'இங்கு தேடவும்',
itemUnit: 'தகவல்',
itemsUnit: 'தகவல்கள்',
},
Upload: {
uploading: 'பதிவேற்றுகிறது...',
removeFile: 'கோப்பை அகற்று',
uploadError: 'பதிவேற்றுவதில் பிழை',
previewFile: 'கோப்பை முன்னோட்டமிடுங்கள்',
},
Empty: {
description: 'தகவல் இல்லை',
},
Icon: {
icon: 'உருவம்',
},
Text: {
edit: 'திருத்து',
copy: 'நகல் எடு',
copied: 'நகல் எடுக்கப்பட்டது',
expand: 'விரிவாக்கவும்',
},
PageHeader: {
back: 'பின் செல்லவும்',
},
};

View File

@ -1,3 +0,0 @@
import locale from '../locale/th_TH';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/tr_TR';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/uk_UA';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/ur_PK';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/vi_VN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/zh_CN';
export default locale;

View File

@ -1,3 +0,0 @@
import locale from '../locale/zh_TW';
export default locale;

View File

@ -0,0 +1,85 @@
import type { VNodeTypes, PropType, ComputedRef, Ref } from 'vue';
import { unref, inject, defineComponent, computed } from 'vue';
import defaultLocaleData from './en_US';
import type { Locale } from '.';
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
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({
compatConfig: { MODE: 3 },
name: 'LocaleReceiver',
props: {
componentName: String as PropType<LocaleComponentName>,
defaultLocale: {
type: [Object, Function],
},
children: {
type: Function as PropType<
(locale: any, localeCode?: string, fullLocale?: object) => VNodeTypes
>,
},
},
setup(props, { slots }) {
const localeData = inject<LocaleReceiverContext>('localeData', {});
const locale = computed(() => {
const { componentName = 'global', defaultLocale } = props;
const locale =
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const { antLocale } = localeData;
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? locale() : locale),
...(localeFromContext || {}),
};
});
const localeCode = computed(() => {
const { antLocale } = localeData;
const localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode;
});
return () => {
const children = props.children || slots.default;
const { antLocale } = localeData;
return children?.(locale.value, localeCode.value, antLocale);
};
},
});
export function useLocaleReceiver<T extends LocaleComponentName>(
componentName: T,
defaultLocale?: Locale[T] | Function | ComputedRef<Locale[T] | Function>,
propsLocale?: Ref<Locale[T]>,
): [ComputedRef<Locale[T]>] {
const localeData = inject<LocaleReceiverContext>('localeData', {} as LocaleReceiverContext);
const componentLocale = computed<Locale[T]>(() => {
const { antLocale } = localeData;
const locale =
unref(defaultLocale) || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? (locale as Function)() : locale),
...(localeFromContext || {}),
...(unref(propsLocale) || {}),
};
});
return [componentLocale];
}

View File

@ -9,7 +9,7 @@ import { convertLegacyProps } from '../button/buttonTypes';
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
import Button from '../button';
import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import defaultLocale from '../locale/en_US';
import { withInstall } from '../_util/type';
import useMergedState from '../_util/hooks/useMergedState';
import devWarning from '../vc-util/devWarning';

View File

@ -6,7 +6,7 @@ import classNames from '../_util/classNames';
import List from './list';
import Operation from './operation';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import defaultLocale from '../locale/en_US';
import type { VueNode } from '../_util/type';
import { withInstall } from '../_util/type';
import useConfigInject from '../config-provider/hooks/useConfigInject';