@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
166 lines (164 loc) • 6.33 kB
JavaScript
const require_rolldown_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_insertion_insertion = require('../transpiler/insertion/insertion.cjs');
let _intlayer_types = require("@intlayer/types");
//#region src/messageFormat/vue-i18n.ts
const parseVueI18nPart = (text) => {
let index = 0;
const nodes = [];
let currentText = "";
while (index < text.length) {
const char = text[index];
if (char === "{") {
if (currentText) {
nodes.push(currentText);
currentText = "";
}
index++;
let name = "";
while (index < text.length && text[index] !== "}") {
name += text[index];
index++;
}
if (index < text.length) index++;
nodes.push({
type: "argument",
name: name.trim()
});
} else {
currentText += char;
index++;
}
}
if (currentText) nodes.push(currentText);
return nodes;
};
const parseVueI18n = (text) => {
const parts = [];
let currentPart = "";
let index = 0;
while (index < text.length) {
const char = text[index];
if (char === "\\" && index + 1 < text.length && text[index + 1] === "|") {
currentPart += "|";
index += 2;
} else if (char === "|") {
parts.push(currentPart.trim());
currentPart = "";
index++;
} else {
currentPart += char;
index++;
}
}
parts.push(currentPart.trim());
return parts.map(parseVueI18nPart);
};
const vueI18nPartToIntlayer = (nodes) => {
if (nodes.length === 0) return "";
if (nodes.length === 1 && typeof nodes[0] === "string") return nodes[0];
let str = "";
for (const node of nodes) if (typeof node === "string") str += node;
else str += `{{${node.name}}}`;
return require_transpiler_insertion_insertion.insert(str);
};
const vueI18nNodesToIntlayer = (parts) => {
if (parts.length === 1) return vueI18nPartToIntlayer(parts[0]);
const options = {};
const varName = "count";
if (parts.length === 2) {
options["1"] = vueI18nPartToIntlayer(parts[0]);
options.fallback = vueI18nPartToIntlayer(parts[1]);
} else if (parts.length === 3) {
options["0"] = vueI18nPartToIntlayer(parts[0]);
options["1"] = vueI18nPartToIntlayer(parts[1]);
options.fallback = vueI18nPartToIntlayer(parts[2]);
} else parts.forEach((part, index) => {
if (index === parts.length - 1) options.fallback = vueI18nPartToIntlayer(part);
else options[index.toString()] = vueI18nPartToIntlayer(part);
});
options.__intlayer_vue_i18n_var = varName;
return require_transpiler_enumeration_enumeration.enu(options);
};
const vueI18nToIntlayerPlugin = {
canHandle: (node) => typeof node === "string" && (node.includes("{") || node.includes("|")),
transform: (node) => {
try {
return vueI18nNodesToIntlayer(parseVueI18n(node));
} catch {
return node;
}
}
};
const intlayerToVueI18nPlugin = {
canHandle: (node) => typeof node === "string" || node && typeof node === "object" && (node.nodeType === _intlayer_types.NodeType.Insertion || node.nodeType === _intlayer_types.NodeType.Enumeration || node.nodeType === _intlayer_types.NodeType.Gender || node.nodeType === "composite") || Array.isArray(node),
transform: (node, props, next) => {
if (typeof node === "string") return node.replace(/\{\{([^}]+)\}\}/g, "{$1}");
if (node.nodeType === _intlayer_types.NodeType.Insertion) return node.insertion.replace(/\{\{([^}]+)\}\}/g, "{$1}");
if (node.nodeType === _intlayer_types.NodeType.Enumeration) {
const options = node.enumeration;
const transformedOptions = {};
for (const [key, val] of Object.entries(options)) {
if (key === "__intlayer_vue_i18n_var") continue;
const childVal = next(val, props);
transformedOptions[key] = typeof childVal === "string" ? childVal : JSON.stringify(childVal);
}
const keys = Object.keys(transformedOptions);
if (keys.includes("0")) {
const indices = keys.filter((key) => /^\d+$/.test(key)).map(Number);
const maxIndex = Math.max(...indices);
const fallback = transformedOptions.fallback || transformedOptions.other;
const resultParts = [];
if (maxIndex <= 1 && !keys.includes("2")) return `${transformedOptions["0"] || ""} | ${transformedOptions["1"] || ""} | ${fallback}`;
const limit = Math.max(1, maxIndex);
for (let i = 0; i <= limit; i++) {
const key = i.toString();
if (transformedOptions[key]) resultParts.push(transformedOptions[key]);
else resultParts.push("");
}
resultParts.push(fallback);
return resultParts.join(" | ").replace(/ \| {2}\| /g, " | | ");
}
if (keys.includes("1") && (keys.includes("fallback") || keys.includes("other"))) return `${transformedOptions["1"]} | ${transformedOptions.fallback || transformedOptions.other}`;
if (keys.length === 1 && (keys.includes("fallback") || keys.includes("other"))) return transformedOptions.fallback || transformedOptions.other;
return transformedOptions.fallback || Object.values(transformedOptions)[0];
}
if (node.nodeType === _intlayer_types.NodeType.Gender) {
const options = node.gender;
const transformedOptions = {};
for (const [key, val] of Object.entries(options)) {
let newKey = key;
if (key === "fallback") newKey = "other";
transformedOptions[newKey] = next(val, props);
}
return transformedOptions;
}
if (Array.isArray(node) || node.nodeType === "composite" && Array.isArray(node.composite)) return (Array.isArray(node) ? node : node.composite).map((item) => next(item, props)).join("");
return next(node, props);
}
};
const intlayerToVueI18nFormatter = (message) => {
return require_interpreter_getContent_deepTransform.deepTransformNode(message, {
dictionaryKey: "vue-i18n",
keyPath: [],
plugins: [{
id: "vue-i18n",
...intlayerToVueI18nPlugin
}]
});
};
const vueI18nToIntlayerFormatter = (message) => {
return require_interpreter_getContent_deepTransform.deepTransformNode(message, {
dictionaryKey: "vue-i18n",
keyPath: [],
plugins: [{
id: "vue-i18n",
...vueI18nToIntlayerPlugin
}]
});
};
//#endregion
exports.intlayerToVueI18nFormatter = intlayerToVueI18nFormatter;
exports.vueI18nToIntlayerFormatter = vueI18nToIntlayerFormatter;
//# sourceMappingURL=vue-i18n.cjs.map