UNPKG

webpack-angular-translate

Version:

Webpack plugin that extracts the translation-ids with the default texts.

112 lines 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var translate_html_parser_1 = require("./translate-html-parser"); var translateAttributeRegex = /^translate-attr-.*$/; function translateDirectiveTranslationExtractor(element, context) { var translateDirective; var translationId; var translateAttribute = element.attributes.find(function (attribute) { return attribute.name === "translate"; }); if (element.tagName === "translate") { translateDirective = true; } if (translateAttribute) { translateDirective = true; translationId = translateAttribute.value; } for (var _i = 0, _a = element.attributes; _i < _a.length; _i++) { var attribute = _a[_i]; handleAttributesWithTranslateExpressions(attribute, context); } if (translateDirective) { var hasTranslateAttributes = handleTranslateAttributes(element, context); var defaultTextAttribute = element.attributes.find(function (attr) { return attr.name === "translate-default"; }); if (!translationId) { if (element.texts.length === 1) { translationId = element.texts[0].text; } else if (element.texts.length > 1) { context.emitError("The element does not specify a translation id but has multiple child text elements. Specify the translation id on the element to define the translation id.", element.startPosition); return; } } // <any translate='test'></any> if (translationId) { context.registerTranslation({ translationId: translationId, defaultText: defaultTextAttribute ? defaultTextAttribute.value : undefined, position: element.startPosition, }); } else if (!hasTranslateAttributes) { // no translate-attr-* and the element has not specified a translation id, someone is using the directive incorrectly context.emitSuppressableError("the element uses the translate directive but does not specify a translation id nor has any translated attributes (translate-attr-*). Specify a translation id or remove the translate-directive.", element.startPosition); } } else { handleTexts(element, context); } } exports.default = translateDirectiveTranslationExtractor; translateDirectiveTranslationExtractor.mayContainTranslations = function (content) { return content.indexOf("translate") !== -1; }; function handleTranslateAttributes(element, context) { var keyedAttributes = element.attributes.reduce(function (obj, attribute) { obj[attribute.name] = attribute; return obj; }, {}); // translate-attr-ATTR var translateAttributes = element.attributes.filter(function (attr) { return translateAttributeRegex.test(attr.name); }); for (var _i = 0, translateAttributes_1 = translateAttributes; _i < translateAttributes_1.length; _i++) { var attribute = translateAttributes_1[_i]; var attributeName = attribute.name.substr("translate-attr-".length); var defaultTextAttribute = keyedAttributes["translate-default-attr-" + attributeName]; context.registerTranslation({ translationId: attribute.value, defaultText: defaultTextAttribute ? defaultTextAttribute.value : undefined, position: attribute.startPosition, }); } return translateAttributes.length > 0; } // <any attr='{{ x | translate }}'></any> function handleAttributesWithTranslateExpressions(attribute, context) { for (var _i = 0, _a = attribute.expressions; _i < _a.length; _i++) { var expression = _a[_i]; handleAngularExpression(expression, context, attribute.startPosition); } } function handleAngularExpression(expression, context, position) { var translationId = expression.value; // e.g { translate | var} instead of a string constant if (!(/^".*"$/.test(translationId) || /^'.*'$/.test(translationId))) { context.emitSuppressableError("A dynamic filter expression is used in the text or an attribute of the element '" + context.asHtml() + "'. Add the '" + translate_html_parser_1.SUPPRESS_ATTRIBUTE_NAME + "' attribute to suppress the error (ensure that you have registered the translation manually, consider using i18n.registerTranslation).", position); } else if (expression.previousFilters) { context.emitSuppressableError("Another filter is used before the translate filter in the element " + context.asHtml() + ". Add the '" + translate_html_parser_1.SUPPRESS_ATTRIBUTE_NAME + "' to suppress the error (ensure that you have registered the translation manually, consider using i18n.registerTranslation).", position); } else { // trim the quotes translationId = translationId.substring(1, translationId.length - 1); context.registerTranslation({ translationId: translationId, position: position, }); } } function handleTexts(element, context) { for (var _i = 0, _a = element.texts; _i < _a.length; _i++) { var text = _a[_i]; for (var _b = 0, _c = text.expressions; _b < _c.length; _b++) { var expression = _c[_b]; handleAngularExpression(expression, context, text.startPosition); } } } //# sourceMappingURL=translate-directive-translation-extractor.js.map