UNPKG

@intlayer/core

Version:

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

233 lines (231 loc) 9.44 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_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_getPlural = require('../getPlural.cjs'); const require_interpreter_getTranslation = require('../getTranslation.cjs'); let _intlayer_types_nodeType = require("@intlayer/types/nodeType"); _intlayer_types_nodeType = require_runtime.__toESM(_intlayer_types_nodeType); //#region src/interpreter/getContent/plugins.ts /** --------------------------------------------- * FALLBACK PLUGIN * * Used to fallback a tree-shaken plugin * --------------------------------------------- */ const fallbackPlugin = { id: "fallback-plugin", canHandle: () => false, transform: (node) => node }; /** Translation plugin. Replaces node with a locale string if nodeType = Translation. */ const translationPlugin = (locale, fallback) => process.env["INTLAYER_NODE_TYPE_TRANSLATION"] === "false" ? fallbackPlugin : { id: "translation-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.TRANSLATION, transform: (node, props, deepTransformNode) => { const original = node[_intlayer_types_nodeType.TRANSLATION] ?? {}; const result = {}; for (const key in original) { const childProps = { ...props, children: original[key], keyPath: [...props.keyPath, { type: _intlayer_types_nodeType.TRANSLATION, key }] }; result[key] = deepTransformNode(original[key], childProps); } return require_interpreter_getTranslation.getTranslation(result, locale, fallback); } }; /** Enumeration plugin. Replaces node with a function that takes quantity => string. */ const enumerationPlugin = process.env["INTLAYER_NODE_TYPE_ENUMERATION"] === "false" ? fallbackPlugin : { id: "enumeration-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.ENUMERATION, transform: (node, props, deepTransformNode) => { const original = node[_intlayer_types_nodeType.ENUMERATION]; const result = {}; for (const key in original) { const child = original[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: _intlayer_types_nodeType.ENUMERATION, key }] }); } return (arg) => { const subResult = require_interpreter_getEnumeration.getEnumeration(result, typeof arg === "number" ? arg : arg.count); if (typeof subResult === "function" && typeof arg === "object") return subResult(arg); return subResult; }; } }; /** * Plural plugin. Replaces node with a function that takes a count (or * `{ count, ...values }`) => string, picking the matching CLDR plural form * for the active locale and interpolating `{{count}}` (and other values). */ const pluralPlugin = (locale) => process.env["INTLAYER_NODE_TYPE_PLURAL"] === "false" ? fallbackPlugin : { id: "plural-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.PLURAL, transform: (node, props, deepTransformNode) => { const original = node[_intlayer_types_nodeType.PLURAL]; const result = {}; /** String plugin for plural. Replaces string node with a component that renders the insertion. */ const pluralStringPlugin = { id: "plural-string-plugin", canHandle: (node) => typeof node === "string", transform: (node, subProps, deepTransformNode) => { const transformedResult = deepTransformNode(node, { ...subProps, children: node, plugins: [...(props.plugins ?? []).filter((plugin) => plugin.id !== "intlayer-node-plugin")] }); return (values) => { const children = require_interpreter_getInsertion.getInsertion(transformedResult, values); return deepTransformNode(children, { ...subProps, plugins: props.plugins, children }); }; } }; for (const key in original) { const child = original[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: _intlayer_types_nodeType.PLURAL, key }], plugins: [pluralStringPlugin, ...props.plugins ?? []] }); } const effectiveLocale = String(locale ?? props.locale ?? "en"); return (arg) => { const count = typeof arg === "number" ? arg : arg.count; const values = typeof arg === "number" ? { count: arg } : arg; const subResult = require_interpreter_getPlural.getPlural(result, count, effectiveLocale); if (typeof subResult === "function") return subResult(values); return subResult; }; } }; /** Condition plugin. Replaces node with a function that takes boolean => string. */ const conditionPlugin = process.env["INTLAYER_NODE_TYPE_CONDITION"] === "false" ? fallbackPlugin : { id: "condition-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.CONDITION, transform: (node, props, deepTransformNode) => { const original = node[_intlayer_types_nodeType.CONDITION]; const result = {}; for (const key in original) { const child = original[key]; result[key] = deepTransformNode(child, { ...props, children: child, keyPath: [...props.keyPath, { type: _intlayer_types_nodeType.CONDITION, key }] }); } return (arg) => { const subResult = require_interpreter_getCondition.getCondition(result, typeof arg === "boolean" ? arg : arg.value); if (typeof subResult === "function" && typeof arg === "object") return subResult(arg); return subResult; }; } }; /** Insertion plugin. Replaces node with a function that takes quantity => string. */ const insertionPlugin = process.env["INTLAYER_NODE_TYPE_INSERTION"] === "false" ? fallbackPlugin : { 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) => typeof node === "string", transform: (node, subProps, deepTransformNode) => { const transformedResult = deepTransformNode(node, { ...subProps, children: node, plugins: [...(props.plugins ?? []).filter((plugin) => plugin.id !== "intlayer-node-plugin")] }); return (values) => { const children = require_interpreter_getInsertion.getInsertion(transformedResult, values); return deepTransformNode(children, { ...subProps, plugins: props.plugins, children }); }; } }; return deepTransformNode(children, { ...props, children, keyPath: newKeyPath, plugins: [insertionStringPlugin, ...props.plugins ?? []] }); } }; /** Gender plugin. Replaces node with a function that takes gender => string. */ const genderPlugin = process.env["INTLAYER_NODE_TYPE_GENDER"] === "false" ? fallbackPlugin : { id: "gender-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.GENDER, transform: (node, props, deepTransformNode) => { const original = node[_intlayer_types_nodeType.GENDER]; const result = {}; for (const key in original) { const child = original[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); } }; /** Nested plugin. Replaces node with the result of `getNesting`. */ const nestedPlugin = (locale) => process.env["INTLAYER_NODE_TYPE_NESTED"] === "false" ? fallbackPlugin : { id: "nested-plugin", canHandle: (node) => typeof node === "object" && (node?.nodeType === _intlayer_types_nodeType.NESTED || node?.nodeType === "n"), transform: (node, props) => require_interpreter_getNesting.getNesting(node[_intlayer_types_nodeType.NESTED].dictionaryKey, node[_intlayer_types_nodeType.NESTED].path, { ...props, locale: locale ?? props.locale }) }; /** File plugin. Replaces node with the result of `getNesting`. */ const filePlugin = process.env["INTLAYER_NODE_TYPE_FILE"] === "false" ? fallbackPlugin : { 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.fallbackPlugin = fallbackPlugin; exports.filePlugin = filePlugin; exports.genderPlugin = genderPlugin; exports.insertionPlugin = insertionPlugin; exports.nestedPlugin = nestedPlugin; exports.pluralPlugin = pluralPlugin; exports.translationPlugin = translationPlugin; //# sourceMappingURL=plugins.cjs.map