webpack-angular-translate
Version:
Webpack plugin that extracts the translation-ids with the default texts.
121 lines • 6.04 kB
JavaScript
"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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SUPPRESS_ATTRIBUTE_NAME = void 0;
var path = __importStar(require("path"));
var htmlparser2_1 = require("htmlparser2");
var translation_1 = __importDefault(require("../translation"));
var element_context_1 = __importStar(require("./element-context"));
var ng_filters_1 = require("./ng-filters");
exports.SUPPRESS_ATTRIBUTE_NAME = "suppress-dynamic-translation-error";
var angularExpressionRegex = /^{{.*}}$/;
function isAngularExpression(value) {
return angularExpressionRegex.test(value);
}
/**
* Visitor that implements the logic for extracting the translations.
* Elements with a translate directive where the content should be translated are registered in the closetag event.
* Translated attributes are registered in the opentag event
* Attributes translated with the translate filter are handled in the opentag event
* Expressions used in the body of an element are translated in the text event.
*/
var TranslateHtmlParser = /** @class */ (function () {
function TranslateHtmlParser(loader, translationExtractors) {
this.loader = loader;
this.translationExtractors = translationExtractors;
this.ontext = this.ontext.bind(this);
}
TranslateHtmlParser.prototype.parse = function (html) {
this.context = new element_context_1.DocumentContext(this.loader, html);
this.parser = new htmlparser2_1.Parser(this, { decodeEntities: true });
this.parser.parseComplete(html);
this.context = this.parser = null;
};
TranslateHtmlParser.prototype.onopentag = function (name, attributes) {
var _this = this;
var parsedAttributes = Object.keys(attributes).map(function (attributeName) { return ({
name: attributeName,
value: attributes[attributeName],
expressions: ng_filters_1.matchAngularExpressions(attributes[attributeName]),
startPosition: getStartIndex(_this.parser)
}); });
this.context = this.context.enter(name, parsedAttributes, getStartIndex(this.parser));
this.context.suppressDynamicTranslationErrors =
typeof attributes[exports.SUPPRESS_ATTRIBUTE_NAME] !== "undefined";
};
TranslateHtmlParser.prototype.ontext = function (raw) {
var text = raw.trim();
this.context.addText({
startPosition: getStartIndex(this.parser),
raw: raw,
text: text,
expressions: ng_filters_1.matchAngularExpressions(text)
});
};
TranslateHtmlParser.prototype.onclosetag = function () {
if (!(this.context instanceof element_context_1.default)) {
throw new Error("onopentag did not create an element context");
}
var element = {
tagName: this.context.tagName,
attributes: this.context.attributes,
texts: this.context.texts,
startPosition: this.context.elementStartPosition
};
var extractorContext = this.createExtractorContext();
for (var _i = 0, _a = this.translationExtractors; _i < _a.length; _i++) {
var extractor = _a[_i];
extractor(element, extractorContext);
}
this.context = this.context.leave();
};
TranslateHtmlParser.prototype.onerror = function (error) {
this.context.emitError("Failed to parse the html, error is " + error.message, getStartIndex(this.parser));
};
TranslateHtmlParser.prototype.createExtractorContext = function () {
return {
registerTranslation: this.registerTranslation.bind(this),
emitError: this.context.emitError.bind(this.context),
emitSuppressableError: this.context.emitSuppressableError.bind(this.context),
asHtml: this.context.asHtml.bind(this.context)
};
};
TranslateHtmlParser.prototype.registerTranslation = function (translation) {
if (isAngularExpression(translation.translationId) ||
isAngularExpression(translation.defaultText)) {
this.context.emitSuppressableError("The element '" + this.context.asHtml() + "' uses an angular expression as translation id ('" + translation.translationId + "') or as default text ('" + translation.defaultText + "'). This is not supported. Either use a string literal as translation id and default text or suppress this error by adding the '" + exports.SUPPRESS_ATTRIBUTE_NAME + "' attribute to this element or any of its parents.", translation.position);
return;
}
this.loader.registerTranslation(new translation_1.default(translation.translationId, translation.defaultText, {
resource: path.relative(this.loader.context, this.loader.resourcePath),
loc: this.context.loc(translation.position)
}));
};
return TranslateHtmlParser;
}());
exports.default = TranslateHtmlParser;
function getStartIndex(parser) {
return parser.startIndex;
}
//# sourceMappingURL=translate-html-parser.js.map