tdesign-react
Version:
TDesign Component for React
114 lines (109 loc) • 4.29 kB
JavaScript
/**
* tdesign v1.13.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _defineProperty } from '../_chunks/dep-8618a2f1.js';
import React from 'react';
import { _ as _slicedToArray } from '../_chunks/dep-118954e6.js';
import { _ as _typeof } from '../_chunks/dep-c37cc2fd.js';
import { isString } from 'lodash-es';
import '../config-provider/ConfigProvider.js';
import ConfigContext from '../config-provider/ConfigContext.js';
import '../_chunks/dep-61f5e3d1.js';
import '../_chunks/dep-698f8ddd.js';
import '../_chunks/dep-09955460.js';
import 'dayjs';
function getPluralIndex(count) {
if (count === 0) return 0;
if (count === 1) return 1;
return 2;
}
function t(pattern) {
if (isString(pattern)) {
var text = pattern;
var count;
var data = {};
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (args.length > 0) {
var firstArg = args[0],
secondArg = args[1];
if (typeof firstArg === "number") {
count = firstArg;
if (secondArg && _typeof(secondArg) === "object") {
data = secondArg;
} else {
data.count = count;
}
} else if (_typeof(firstArg) === "object" && firstArg !== null) {
data = firstArg;
}
}
if (text.includes("|")) {
var pluralParts = text.split("|").map(function (part) {
return part.trim();
});
if (typeof count === "number") {
var pluralIndex = getPluralIndex(count);
if (pluralIndex < pluralParts.length) {
text = pluralParts[pluralIndex];
} else {
text = pluralParts[pluralParts.length - 1];
}
} else {
var _pluralParts = _slicedToArray(pluralParts, 1),
firstPart = _pluralParts[0];
text = firstPart;
}
}
if (data && Object.keys(data).length > 0) {
var regular = /\{\s*([\w-]+)\s*\}/g;
text = text.replace(regular, function (match, key) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
return String(data[key]);
}
return match;
});
}
return text;
}
return "";
}
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function useLocaleReceiver(componentName, defaultLocale) {
var _React$useContext = React.useContext(ConfigContext),
globalConfig = _React$useContext.globalConfig;
function transformLocale(pattern) {
var REGEXP = /\{\s*([\w-]+)\s*\}/g;
var placement = arguments.length <= 1 ? undefined : arguments[1];
if (Array.isArray(pattern)) {
return pattern.map(function (p, index) {
var translated = p.replace(REGEXP, function (_, key) {
if (placement) return String(placement[index][key]);
return "";
});
return translated;
});
}
if (typeof pattern === "function") {
return pattern(placement);
}
var data = arguments.length <= 2 ? undefined : arguments[2];
if (data) {
return t(pattern, placement, data);
}
return t(pattern, placement);
}
var componentLocale = React.useMemo(function () {
var locale = defaultLocale || {};
var connectLocaleByName = globalConfig[componentName];
var localeFromContext = componentName && globalConfig ? connectLocaleByName : {};
return _objectSpread(_objectSpread({}, typeof locale === "function" ? locale() : locale), localeFromContext || {});
}, [componentName, defaultLocale, globalConfig]);
return [componentLocale, transformLocale];
}
export { useLocaleReceiver };
//# sourceMappingURL=LocalReceiver.js.map