@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
149 lines (147 loc) • 5.79 kB
JavaScript
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
const require_interpreter_getCondition = require('../getCondition.cjs');
const require_interpreter_getEnumeration = require('../getEnumeration.cjs');
const require_interpreter_getGender = require('../getGender.cjs');
const require_interpreter_getInsertion = require('../getInsertion.cjs');
const require_interpreter_getNesting = require('../getNesting.cjs');
const require_interpreter_getTranslation = require('../getTranslation.cjs');
let __intlayer_types = require("@intlayer/types");
//#region src/interpreter/getContent/plugins.ts
/** Translation plugin. Replaces node with a locale string if nodeType = Translation. */
const translationPlugin = (locale, fallback, onContentNotFound) => ({
id: "translation-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Translation,
transform: (node, props, deepTransformNode) => {
const result = structuredClone(node[__intlayer_types.NodeType.Translation]);
for (const key in result) {
const childProps = {
...props,
children: result[key],
keyPath: [...props.keyPath, {
type: __intlayer_types.NodeType.Translation,
key
}]
};
result[key] = deepTransformNode(result[key], childProps);
}
return require_interpreter_getTranslation.getTranslation(result, locale, fallback);
}
});
/** Enumeration plugin. Replaces node with a function that takes quantity => string. */
const enumerationPlugin = {
id: "enumeration-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Enumeration,
transform: (node, props, deepTransformNode) => {
const result = structuredClone(node[__intlayer_types.NodeType.Enumeration]);
for (const key in result) {
const child = result[key];
result[key] = deepTransformNode(child, {
...props,
children: child,
keyPath: [...props.keyPath, {
type: __intlayer_types.NodeType.Enumeration,
key
}]
});
}
return (quantity) => require_interpreter_getEnumeration.getEnumeration(result, quantity);
}
};
/** Condition plugin. Replaces node with a function that takes boolean => string. */
const conditionPlugin = {
id: "condition-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Condition,
transform: (node, props, deepTransformNode) => {
const result = structuredClone(node[__intlayer_types.NodeType.Condition]);
for (const key in result) {
const child = result[key];
result[key] = deepTransformNode(child, {
...props,
children: child,
keyPath: [...props.keyPath, {
type: __intlayer_types.NodeType.Condition,
key
}]
});
}
return (value) => require_interpreter_getCondition.getCondition(result, value);
}
};
/** Gender plugin. Replaces node with a function that takes gender => string. */
const genderPlugin = {
id: "gender-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Gender,
transform: (node, props, deepTransformNode) => {
const result = structuredClone(node[__intlayer_types.NodeType.Gender]);
for (const key in result) {
const child = result[key];
result[key] = deepTransformNode(child, {
...props,
children: child,
keyPath: [...props.keyPath, {
type: __intlayer_types.NodeType.Gender,
key
}]
});
}
return (value) => require_interpreter_getGender.getGender(result, value);
}
};
const insertionPlugin = {
id: "insertion-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Insertion,
transform: (node, props, deepTransformNode) => {
const newKeyPath = [...props.keyPath, { type: __intlayer_types.NodeType.Insertion }];
const children = node[__intlayer_types.NodeType.Insertion];
/** Insertion string plugin. Replaces string node with a component that render the insertion. */
const insertionStringPlugin = {
id: "insertion-string-plugin",
canHandle: (node$1) => typeof node$1 === "string",
transform: (node$1, subProps, deepTransformNode$1) => {
const transformedResult = deepTransformNode$1(node$1, {
...subProps,
children: node$1,
plugins: [...(props.plugins ?? []).filter((plugin) => plugin.id !== "intlayer-node-plugin")]
});
return (values) => {
const children$1 = require_interpreter_getInsertion.getInsertion(transformedResult, values);
return deepTransformNode$1(children$1, {
...subProps,
plugins: props.plugins,
children: children$1
});
};
}
};
return deepTransformNode(children, {
...props,
children,
keyPath: newKeyPath,
plugins: [insertionStringPlugin, ...props.plugins ?? []]
});
}
};
/** Nested plugin. Replaces node with the result of `getNesting`. */
const nestedPlugin = {
id: "nested-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.Nested,
transform: (node, props) => require_interpreter_getNesting.getNesting(node.nested.dictionaryKey, node.nested.path, props)
};
/** File plugin. Replaces node with the result of `getNesting`. */
const filePlugin = {
id: "file-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === __intlayer_types.NodeType.File,
transform: (node, props, deepTransform) => deepTransform(node.content, {
...props,
children: node.content
})
};
//#endregion
exports.conditionPlugin = conditionPlugin;
exports.enumerationPlugin = enumerationPlugin;
exports.filePlugin = filePlugin;
exports.genderPlugin = genderPlugin;
exports.insertionPlugin = insertionPlugin;
exports.nestedPlugin = nestedPlugin;
exports.translationPlugin = translationPlugin;
//# sourceMappingURL=plugins.cjs.map