UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

142 lines (140 loc) 4.93 kB
import { getCondition } from "../getCondition.mjs"; import { getEnumeration } from "../getEnumeration.mjs"; import { getGender } from "../getGender.mjs"; import { getInsertion } from "../getInsertion.mjs"; import { getNesting } from "../getNesting.mjs"; import { getTranslation } from "../getTranslation.mjs"; import { NodeType } from "@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 === NodeType.Translation, transform: (node, props, deepTransformNode) => { const result = structuredClone(node[NodeType.Translation]); for (const key in result) { const childProps = { ...props, children: result[key], keyPath: [...props.keyPath, { type: NodeType.Translation, key }] }; result[key] = deepTransformNode(result[key], childProps); } return 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 === NodeType.Enumeration, transform: (node, props, deepTransformNode) => { const result = structuredClone(node[NodeType.Enumeration]); for (const key in result) { const child = result[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: NodeType.Enumeration, key }] }); } return (quantity) => 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 === NodeType.Condition, transform: (node, props, deepTransformNode) => { const result = structuredClone(node[NodeType.Condition]); for (const key in result) { const child = result[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: NodeType.Condition, key }] }); } return (value) => 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 === NodeType.Gender, transform: (node, props, deepTransformNode) => { const result = structuredClone(node[NodeType.Gender]); for (const key in result) { const child = result[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: NodeType.Gender, key }] }); } return (value) => getGender(result, value); } }; const insertionPlugin = { id: "insertion-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Insertion, transform: (node, props, deepTransformNode) => { const newKeyPath = [...props.keyPath, { type: NodeType.Insertion }]; const children = node[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 = 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 === NodeType.Nested, transform: (node, props) => 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 === NodeType.File, transform: (node, props, deepTransform) => deepTransform(node.content, { ...props, children: node.content }) }; //#endregion export { conditionPlugin, enumerationPlugin, filePlugin, genderPlugin, insertionPlugin, nestedPlugin, translationPlugin }; //# sourceMappingURL=plugins.mjs.map