UNPKG

@intlayer/core

Version:

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

171 lines (169 loc) 7.74 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); const require_interpreter_getContent_deepTransform = require('../interpreter/getContent/deepTransform.cjs'); const require_transpiler_enumeration_enumeration = require('../transpiler/enumeration/enumeration.cjs'); const require_transpiler_gender_gender = require('../transpiler/gender/gender.cjs'); const require_transpiler_html_html = require('../transpiler/html/html.cjs'); const require_transpiler_insertion_insertion = require('../transpiler/insertion/insertion.cjs'); const require_transpiler_plural_plural = require('../transpiler/plural/plural.cjs'); let _intlayer_types_nodeType = require("@intlayer/types/nodeType"); _intlayer_types_nodeType = require_runtime.__toESM(_intlayer_types_nodeType); //#region src/messageFormat/po.ts /** * Extracts the string value from a transformed AST node or generic object. */ const extractStringValue = (val) => { if (typeof val === "string") return val; if (val && typeof val === "object" && "msgstr" in val) return val.msgstr[0] || ""; return JSON.stringify(val); }; const intlayerToPoPlugin = { canHandle: (node) => { if (typeof node === "string") return true; if (node && typeof node === "object" && (node.nodeType === _intlayer_types_nodeType.INSERTION || node.nodeType === _intlayer_types_nodeType.HTML || node.nodeType === _intlayer_types_nodeType.ENUMERATION || node.nodeType === _intlayer_types_nodeType.PLURAL || node.nodeType === _intlayer_types_nodeType.GENDER || node.nodeType === "composite")) return true; if (Array.isArray(node)) { if (node.length === 0) return false; let hasNode = false; let hasPlainObjectOrArray = false; for (const item of node) if (typeof item === "string") {} else if (item && typeof item === "object" && (item.nodeType === _intlayer_types_nodeType.INSERTION || item.nodeType === _intlayer_types_nodeType.HTML || item.nodeType === _intlayer_types_nodeType.ENUMERATION || item.nodeType === _intlayer_types_nodeType.PLURAL || item.nodeType === _intlayer_types_nodeType.GENDER || item.nodeType === "composite")) hasNode = true; else hasPlainObjectOrArray = true; if (hasPlainObjectOrArray) return false; if (!hasNode) return false; return true; } return false; }, transform: (node, props, next) => { if (typeof node === "string") { const poVal = node.replace(/\{\{([^}]+)\}\}/g, "%($1)s"); return { msgid: poVal, msgstr: [poVal] }; } if (node.nodeType === _intlayer_types_nodeType.INSERTION) return next(node[_intlayer_types_nodeType.INSERTION], props); if (node.nodeType === _intlayer_types_nodeType.HTML) { const val = node[_intlayer_types_nodeType.HTML]; return { msgid: val, msgstr: [val] }; } if (node.nodeType === _intlayer_types_nodeType.PLURAL || node.nodeType === _intlayer_types_nodeType.ENUMERATION) { const isPlural = node.nodeType === _intlayer_types_nodeType.PLURAL; const options = isPlural ? node[_intlayer_types_nodeType.PLURAL] : node[_intlayer_types_nodeType.ENUMERATION]; const rawMsgid = isPlural ? options.one || options.other || options.fallback : options.fallback || options["0"]; const msgid = extractStringValue(next(rawMsgid, props)); const msgid_plural = isPlural ? extractStringValue(next(options.other || options.fallback || rawMsgid, props)) : extractStringValue(next(options.fallback || rawMsgid, props)); const msgstr = []; if (isPlural) { if ("zero" in options) msgstr.push(extractStringValue(next(options.zero, props))); msgstr.push(extractStringValue(next(options.one || options.fallback, props))); if ("two" in options) msgstr.push(extractStringValue(next(options.two, props))); if ("few" in options) msgstr.push(extractStringValue(next(options.few, props))); if ("many" in options) msgstr.push(extractStringValue(next(options.many, props))); const otherVal = extractStringValue(next(options.other || options.fallback, props)); if (!msgstr.includes(otherVal)) msgstr.push(otherVal); } else { msgstr[0] = extractStringValue(next(options.fallback || options["0"], props)); msgstr[1] = msgstr[0]; } return { msgctxt: isPlural ? void 0 : "enumeration", msgid, msgid_plural, msgstr }; } if (node.nodeType === _intlayer_types_nodeType.GENDER) { const options = node[_intlayer_types_nodeType.GENDER]; const fallback = extractStringValue(next(options.fallback, props)); return { msgctxt: "gender", msgid: fallback, msgstr: [ extractStringValue(next(options.male || options.fallback, props)), extractStringValue(next(options.female || options.fallback, props)), fallback ] }; } if (Array.isArray(node) || node.nodeType === "composite" && Array.isArray(node.composite)) { const combined = (Array.isArray(node) ? node : node.composite).map((item) => extractStringValue(next(item, props))).join(""); return { msgid: combined, msgstr: [combined] }; } return node; } }; const poToIntlayerPlugin = { canHandle: (node) => node && typeof node === "object" && "msgid" in node && "msgstr" in node, transform: (node) => { const msgstr = node.msgstr || []; const isPlural = Boolean(node.msgid_plural) || msgstr.length > 1; const processString = (str) => { if (!str) return ""; const parsed = str.replace(/%\(([a-zA-Z0-9_-]+)\)[sdf]/g, "{{$1}}"); if (/<[a-zA-Z0-9-]+[^>]*>/.test(parsed)) return require_transpiler_html_html.html(parsed); if (parsed.includes("{{")) return require_transpiler_insertion_insertion.insert(parsed); return parsed; }; if (!isPlural) return processString(msgstr[0] || node.msgid || ""); const options = {}; if (node.msgctxt === "gender") return require_transpiler_gender_gender.gender({ male: processString(msgstr[0] || node.msgid), female: processString(msgstr[1] || msgstr[0]), fallback: processString(msgstr[2] || msgstr[msgstr.length - 1]) }); if (node.msgctxt === "enumeration") return require_transpiler_enumeration_enumeration.enu({ "0": processString(msgstr[0]), fallback: processString(msgstr[msgstr.length - 1]) }); if (msgstr.length === 2) { options.one = processString(msgstr[0]); options.other = processString(msgstr[1]); } else if (msgstr.length === 3) { options.one = processString(msgstr[0]); options.few = processString(msgstr[1]); options.other = processString(msgstr[2]); } else if (msgstr.length === 6) { options.zero = processString(msgstr[0]); options.one = processString(msgstr[1]); options.two = processString(msgstr[2]); options.few = processString(msgstr[3]); options.many = processString(msgstr[4]); options.other = processString(msgstr[5]); } else { options.one = processString(msgstr[0]); options.other = processString(msgstr[msgstr.length - 1]); for (let i = 1; i < msgstr.length - 1; i++) options[`${i + 1}`] = processString(msgstr[i]); } return require_transpiler_plural_plural.plural(options); } }; const intlayerToPortableObjectFormatter = (dictionary) => { return require_interpreter_getContent_deepTransform.deepTransformNode(dictionary, { dictionaryKey: "po", keyPath: [], plugins: [{ id: "po", ...intlayerToPoPlugin }] }); }; const portableObjectToIntlayerFormatter = (message) => { return require_interpreter_getContent_deepTransform.deepTransformNode(message, { dictionaryKey: "po", keyPath: [], plugins: [{ id: "po", ...poToIntlayerPlugin }] }); }; //#endregion exports.intlayerToPortableObjectFormatter = intlayerToPortableObjectFormatter; exports.portableObjectToIntlayerFormatter = portableObjectToIntlayerFormatter; //# sourceMappingURL=po.cjs.map