LocaleReceiver.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { unref, inject, defineComponent, computed } from 'vue';
  3. import defaultLocaleData from './en_US';
  4. export default defineComponent({
  5. compatConfig: {
  6. MODE: 3
  7. },
  8. name: 'LocaleReceiver',
  9. props: {
  10. componentName: String,
  11. defaultLocale: {
  12. type: [Object, Function]
  13. },
  14. children: {
  15. type: Function
  16. }
  17. },
  18. setup(props, _ref) {
  19. let {
  20. slots
  21. } = _ref;
  22. const localeData = inject('localeData', {});
  23. const locale = computed(() => {
  24. const {
  25. componentName = 'global',
  26. defaultLocale
  27. } = props;
  28. const locale = defaultLocale || defaultLocaleData[componentName || 'global'];
  29. const {
  30. antLocale
  31. } = localeData;
  32. const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
  33. return _extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
  34. });
  35. const localeCode = computed(() => {
  36. const {
  37. antLocale
  38. } = localeData;
  39. const localeCode = antLocale && antLocale.locale;
  40. // Had use LocaleProvide but didn't set locale
  41. if (antLocale && antLocale.exist && !localeCode) {
  42. return defaultLocaleData.locale;
  43. }
  44. return localeCode;
  45. });
  46. return () => {
  47. const children = props.children || slots.default;
  48. const {
  49. antLocale
  50. } = localeData;
  51. return children === null || children === void 0 ? void 0 : children(locale.value, localeCode.value, antLocale);
  52. };
  53. }
  54. });
  55. export function useLocaleReceiver(componentName, defaultLocale, propsLocale) {
  56. const localeData = inject('localeData', {});
  57. const componentLocale = computed(() => {
  58. const {
  59. antLocale
  60. } = localeData;
  61. const locale = unref(defaultLocale) || defaultLocaleData[componentName || 'global'];
  62. const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
  63. return _extends(_extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {}), unref(propsLocale) || {});
  64. });
  65. return [componentLocale];
  66. }