LocaleReceiver.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { unref, inject, defineComponent, computed } from 'vue';
  3. import defaultLocaleData from './default';
  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: function setup(props, _ref) {
  19. var slots = _ref.slots;
  20. var localeData = inject('localeData', {});
  21. var locale = computed(function () {
  22. var _props$componentName = props.componentName,
  23. componentName = _props$componentName === void 0 ? 'global' : _props$componentName,
  24. defaultLocale = props.defaultLocale;
  25. var locale = defaultLocale || defaultLocaleData[componentName || 'global'];
  26. var antLocale = localeData.antLocale;
  27. var localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
  28. return _objectSpread(_objectSpread({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
  29. });
  30. var localeCode = computed(function () {
  31. var antLocale = localeData.antLocale;
  32. var localeCode = antLocale && antLocale.locale;
  33. // Had use LocaleProvide but didn't set locale
  34. if (antLocale && antLocale.exist && !localeCode) {
  35. return defaultLocaleData.locale;
  36. }
  37. return localeCode;
  38. });
  39. return function () {
  40. var children = props.children || slots.default;
  41. var antLocale = localeData.antLocale;
  42. return children === null || children === void 0 ? void 0 : children(locale.value, localeCode.value, antLocale);
  43. };
  44. }
  45. });
  46. export function useLocaleReceiver(componentName, defaultLocale, propsLocale) {
  47. var localeData = inject('localeData', {});
  48. var componentLocale = computed(function () {
  49. var antLocale = localeData.antLocale;
  50. var locale = unref(defaultLocale) || defaultLocaleData[componentName || 'global'];
  51. var localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
  52. return _objectSpread(_objectSpread(_objectSpread({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {}), unref(propsLocale) || {});
  53. });
  54. return [componentLocale];
  55. }