UNPKG

@intlayer/core

Version:

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

138 lines 4.2 kB
import { NodeType } from "../../types/index.mjs"; import { getCondition } from "../getCondition.mjs"; import { getEnumeration } from "../getEnumeration.mjs"; import { getInsertion } from "../getInsertion.mjs"; import { getNesting } from "../getNesting.mjs"; import { getTranslation } from "../getTranslation.mjs"; const translationPlugin = (locale, fallback = true) => ({ 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); } }); 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]; const childProps = { ...props, children: child, keyPath: [ ...props.keyPath, { type: NodeType.Enumeration, key } ] }; result[key] = deepTransformNode( child, childProps ); } return (quantity) => getEnumeration(result, quantity); } }; 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]; const childProps = { ...props, children: child, keyPath: [ ...props.keyPath, { type: NodeType.Condition, key } ] }; result[key] = deepTransformNode( child, childProps ); } return (value) => getCondition(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]; const insertionStringPlugin = { id: "insertion-string-plugin", canHandle: (node2) => typeof node2 === "string", transform: (node2, subProps, deepTransformNode2) => { const transformedResult = deepTransformNode2(node2, { ...subProps, children: node2, plugins: [ ...(props.plugins ?? []).filter( (plugin) => plugin.id !== "intlayer-node-plugin" ) ] }); return (values) => { const children2 = getInsertion(transformedResult, values); return deepTransformNode2(children2, { ...subProps, plugins: props.plugins, children: children2 }); }; } }; return deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [insertionStringPlugin, ...props.plugins ?? []] }); } }; 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) }; 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 }) }; export { conditionPlugin, enumerationPlugin, filePlugin, insertionPlugin, nestedPlugin, translationPlugin }; //# sourceMappingURL=plugins.mjs.map