"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TranslationFunctionTransformer = void 0; var wxml_parser_1 = __importStar(require("../parser/wxml-parser")); var expression_parser_1 = __importDefault(require("../parser/expression-parser")); var BLOCK_DELIMITER_START = '{{'; /** * Transform miniprogram templates (WXML) to import i18n data and transform i18n invocations * i18n data will be imported via WXS mechanism while i18n invocations will be transformed * to normal WXS calls, e.g. t(key, val) will be transformed to something like $i18n.t(key, val) */ var TranslationFunctionTransformer = /** @class */ (function () { function TranslationFunctionTransformer(translationFunctionName, i18nModuleName, currentLocaleVariableName) { if (translationFunctionName === void 0) { translationFunctionName = "t" /* default */; } if (i18nModuleName === void 0) { i18nModuleName = "i18n" /* default */; } if (currentLocaleVariableName === void 0) { currentLocaleVariableName = "$_locale" /* default */; } this.translationFunctionName = translationFunctionName; this.i18nModuleName = i18nModuleName; this.currentLocaleVariableName = currentLocaleVariableName; this.source = ''; } /** * Given a piece of wxml source code, transform its i18n function calls into normal wxs function calls */ TranslationFunctionTransformer.prototype.transform = function (source, fileName) { if (fileName === void 0) { fileName = ''; } this.source = source; var parser = new wxml_parser_1.default(source, fileName); var nodes = parser.parse(); // Transform function call expressions in place this.transformNodes(nodes, fileName); return this.source; }; TranslationFunctionTransformer.prototype.transformNodes = function (nodes, fileName) { var e_1, _a; // walk through wxml nodes to pick up interpolation blocks for (var i = nodes.length - 1; i >= 0; i--) { var node = nodes[i]; if (node instanceof wxml_parser_1.Text) { if (node.content.includes(BLOCK_DELIMITER_START)) { var _b = this.transformFunctionCallExpr(node.content, fileName), content = _b.content, transformed = _b.transformed; if (transformed) { var head = this.source.substring(0, node.start); var rear = this.source.substring(node.end); this.source = head + content + rear; } } } else if (node instanceof wxml_parser_1.Element) { this.transformNodes(node.children, fileName); var attributes = this.sortAttributesByStartPos(node.attributes); try { for (var attributes_1 = (e_1 = void 0, __values(attributes)), attributes_1_1 = attributes_1.next(); !attributes_1_1.done; attributes_1_1 = attributes_1.next()) { var attrValue = attributes_1_1.value; if (attrValue && attrValue.value.includes(BLOCK_DELIMITER_START)) { var _c = this.transformFunctionCallExpr(attrValue.value, fileName), content = _c.content, transformed = _c.transformed; if (transformed) { var head = this.source.substring(0, attrValue.start); var rear = this.source.substring(attrValue.end); this.source = head + content + rear; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (attributes_1_1 && !attributes_1_1.done && (_a = attributes_1.return)) _a.call(attributes_1); } finally { if (e_1) throw e_1.error; } } } } }; TranslationFunctionTransformer.prototype.transformFunctionCallExpr = function (source, fileName) { var parser = new expression_parser_1.default(source, fileName); var expr = parser.parse(); return this.transformFunctionCallExprRecursively(source, expr.callExpressions); }; TranslationFunctionTransformer.prototype.transformFunctionCallExprRecursively = function (source, callExpressions) { var transformed = false; for (var i = callExpressions.length - 1; i >= 0; i--) { var callExpr = callExpressions[i]; if (callExpr.expression === this.translationFunctionName) { // Inject currentLocale variable var end = callExpr.end - 1; var localeVarDecl = callExpr.parameters.length > 0 ? ", " + this.currentLocaleVariableName : this.currentLocaleVariableName; source = source.substring(0, end) + localeVarDecl + source.substring(end); var child = this.transformFunctionCallExprRecursively(source, callExpr.childFunctionExpressions); transformed = transformed || child.transformed; source = child.content; var head = source.substring(0, callExpr.functionNameStart); var rear = source.substring(callExpr.functionNameEnd); source = head + this.i18nModuleName + '.' + "t" /* default */ + rear; transformed = true; } } return { transformed: transformed, content: source }; }; TranslationFunctionTransformer.prototype.sortAttributesByStartPos = function (attributes) { // descendent return Array.from(attributes.values()).filter(function (a) { return !!a; }).sort(function (a, b) { return b.start - a.start; }); }; return TranslationFunctionTransformer; }()); exports.TranslationFunctionTransformer = TranslationFunctionTransformer;