@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
123 lines (121 loc) • 4.5 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_types = require("@intlayer/types");
__intlayer_types = require_rolldown_runtime.__toESM(__intlayer_types);
//#region src/deepTransformPlugins/getSplittedContent.ts
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
const hasNodeType = (value) => isObject(value) && typeof value.nodeType === "string";
const mergeValues = (a, b) => {
if (a === void 0) return b;
if (b === void 0) return a;
if (Array.isArray(a) && Array.isArray(b)) return [...a, ...b];
if (isObject(a) && isObject(b) && !hasNodeType(a) && !hasNodeType(b)) {
const result = { ...a };
for (const key of Object.keys(b)) result[key] = mergeValues(result[key], b[key]);
return result;
}
return b;
};
const mergeSplitResults = (base, addition) => {
const result = { ...base };
result.common = mergeValues(base.common, addition.common);
const localeKeys = new Set([...Object.keys(base).filter((k) => k !== "common"), ...Object.keys(addition).filter((k) => k !== "common")]);
for (const key of localeKeys) result[key] = mergeValues(base[key], addition[key]);
return result;
};
const splitNode = (node) => {
if (isObject(node) && node?.nodeType === __intlayer_types.NodeType.Translation) {
const translations = node[__intlayer_types.NodeType.Translation];
const result = {};
for (const locale of Object.keys(translations)) {
const child = translations[locale];
const childSplit = splitNode(child);
const mergedForLocale = mergeValues(childSplit.common, void 0);
let recomposed;
for (const key of Object.keys(childSplit)) {
if (key === "common") continue;
recomposed = mergeValues(recomposed, childSplit[key]);
}
const finalLocaleNode = mergeValues(mergedForLocale, recomposed);
if (finalLocaleNode !== void 0) result[locale] = finalLocaleNode;
}
return result;
}
if (Array.isArray(node)) {
const commonArray = [];
const perLocaleArrays = {};
node.forEach((child) => {
const childSplit = splitNode(child);
if (childSplit.common !== void 0) commonArray.push(childSplit.common);
for (const key of Object.keys(childSplit)) {
if (key === "common") continue;
if (!perLocaleArrays[key]) perLocaleArrays[key] = [];
const value = childSplit[key];
if (value !== void 0) perLocaleArrays[key].push(value);
}
});
const result = {};
if (commonArray.length > 0) result.common = commonArray;
for (const key of Object.keys(perLocaleArrays)) result[key] = perLocaleArrays[key];
return result;
}
if (isObject(node) && !hasNodeType(node)) {
let accumulated = {};
for (const key of Object.keys(node)) {
const childSplit = splitNode(node[key]);
if (childSplit.common !== void 0) accumulated = mergeSplitResults(accumulated, { common: { [key]: childSplit.common } });
for (const locale of Object.keys(childSplit)) {
if (locale === "common") continue;
accumulated = mergeSplitResults(accumulated, { [locale]: { [key]: childSplit[locale] } });
}
}
return accumulated;
}
return { common: node };
};
const getSplittedContent = (content) => {
const split = splitNode(content);
const output = {};
if (split.common !== void 0) output.common = split.common;
for (const key of Object.keys(split)) {
if (key === "common") continue;
const value = split[key];
if (value !== void 0) output[key] = value;
}
return output;
};
/**
* Splits the `content` field of a Dictionary into "common" and per-locale buckets.
*
* Given a dictionary like:
* ```js
* {
* key: "my-key",
* content: {
* commonContent: "common content",
* multilingualContent: t({
* en: "english content",
* fr: "french content",
* de: "german content",
* }),
* },
* }
* ```
*
* It produces:
* ```js
* {
* common: { commonContent: "common content" },
* en: { multilingualContent: "english content" },
* fr: { multilingualContent: "french content" },
* de: { multilingualContent: "german content" },
* }
* ```
*
* @param dictionary - The input dictionary object with possible multilingual or common content.
* @returns An object mapping "common" and each locale to their corresponding content subtrees.
*/
const getSplittedDictionaryContent = (dictionary) => getSplittedContent(dictionary.content);
//#endregion
exports.getSplittedContent = getSplittedContent;
exports.getSplittedDictionaryContent = getSplittedDictionaryContent;
//# sourceMappingURL=getSplittedContent.cjs.map