PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/@angular/compiler/src/i18n/message_extractor.js

https://gitlab.com/btluis/Angular2_Ejemplo
JavaScript | 166 lines | 98 code | 0 blank | 68 comment | 12 complexity | 7040976687959ad3aed49f443110c877 MD5 | raw file
  1. "use strict";
  2. var collection_1 = require('../facade/collection');
  3. var lang_1 = require('../facade/lang');
  4. var html_ast_1 = require('../html_ast');
  5. var interpolation_config_1 = require('../interpolation_config');
  6. var message_1 = require('./message');
  7. var shared_1 = require('./shared');
  8. /**
  9. * All messages extracted from a template.
  10. */
  11. var ExtractionResult = (function () {
  12. function ExtractionResult(messages, errors) {
  13. this.messages = messages;
  14. this.errors = errors;
  15. }
  16. return ExtractionResult;
  17. }());
  18. exports.ExtractionResult = ExtractionResult;
  19. /**
  20. * Removes duplicate messages.
  21. */
  22. function removeDuplicates(messages) {
  23. var uniq = {};
  24. messages.forEach(function (m) {
  25. if (!collection_1.StringMapWrapper.contains(uniq, message_1.id(m))) {
  26. uniq[message_1.id(m)] = m;
  27. }
  28. });
  29. return collection_1.StringMapWrapper.values(uniq);
  30. }
  31. exports.removeDuplicates = removeDuplicates;
  32. /**
  33. * Extracts all messages from a template.
  34. *
  35. * Algorithm:
  36. *
  37. * To understand the algorithm, you need to know how partitioning works.
  38. * Partitioning is required as we can use two i18n comments to group node siblings together.
  39. * That is why we cannot just use nodes.
  40. *
  41. * Partitioning transforms an array of HtmlAst into an array of Part.
  42. * A part can optionally contain a root element or a root text node. And it can also contain
  43. * children.
  44. * A part can contain i18n property, in which case it needs to be extracted.
  45. *
  46. * Example:
  47. *
  48. * The following array of nodes will be split into four parts:
  49. *
  50. * ```
  51. * <a>A</a>
  52. * <b i18n>B</b>
  53. * <!-- i18n -->
  54. * <c>C</c>
  55. * D
  56. * <!-- /i18n -->
  57. * E
  58. * ```
  59. *
  60. * Part 1 containing the a tag. It should not be translated.
  61. * Part 2 containing the b tag. It should be translated.
  62. * Part 3 containing the c tag and the D text node. It should be translated.
  63. * Part 4 containing the E text node. It should not be translated..
  64. *
  65. * It is also important to understand how we stringify nodes to create a message.
  66. *
  67. * We walk the tree and replace every element node with a placeholder. We also replace
  68. * all expressions in interpolation with placeholders. We also insert a placeholder element
  69. * to wrap a text node containing interpolation.
  70. *
  71. * Example:
  72. *
  73. * The following tree:
  74. *
  75. * ```
  76. * <a>A{{I}}</a><b>B</b>
  77. * ```
  78. *
  79. * will be stringified into:
  80. * ```
  81. * <ph name="e0"><ph name="t1">A<ph name="0"/></ph></ph><ph name="e2">B</ph>
  82. * ```
  83. *
  84. * This is what the algorithm does:
  85. *
  86. * 1. Use the provided html parser to get the html AST of the template.
  87. * 2. Partition the root nodes, and process each part separately.
  88. * 3. If a part does not have the i18n attribute, recurse to process children and attributes.
  89. * 4. If a part has the i18n attribute, stringify the nodes to create a Message.
  90. */
  91. var MessageExtractor = (function () {
  92. function MessageExtractor(_htmlParser, _parser, _implicitTags, _implicitAttrs) {
  93. this._htmlParser = _htmlParser;
  94. this._parser = _parser;
  95. this._implicitTags = _implicitTags;
  96. this._implicitAttrs = _implicitAttrs;
  97. }
  98. MessageExtractor.prototype.extract = function (template, sourceUrl, interpolationConfig) {
  99. if (interpolationConfig === void 0) { interpolationConfig = interpolation_config_1.DEFAULT_INTERPOLATION_CONFIG; }
  100. this._messages = [];
  101. this._errors = [];
  102. var res = this._htmlParser.parse(template, sourceUrl, true);
  103. if (res.errors.length == 0) {
  104. this._recurse(res.rootNodes, interpolationConfig);
  105. }
  106. return new ExtractionResult(this._messages, this._errors.concat(res.errors));
  107. };
  108. MessageExtractor.prototype._extractMessagesFromPart = function (part, interpolationConfig) {
  109. if (part.hasI18n) {
  110. this._messages.push(part.createMessage(this._parser, interpolationConfig));
  111. this._recurseToExtractMessagesFromAttributes(part.children, interpolationConfig);
  112. }
  113. else {
  114. this._recurse(part.children, interpolationConfig);
  115. }
  116. if (lang_1.isPresent(part.rootElement)) {
  117. this._extractMessagesFromAttributes(part.rootElement, interpolationConfig);
  118. }
  119. };
  120. MessageExtractor.prototype._recurse = function (nodes, interpolationConfig) {
  121. var _this = this;
  122. if (lang_1.isPresent(nodes)) {
  123. var parts = shared_1.partition(nodes, this._errors, this._implicitTags);
  124. parts.forEach(function (part) { return _this._extractMessagesFromPart(part, interpolationConfig); });
  125. }
  126. };
  127. MessageExtractor.prototype._recurseToExtractMessagesFromAttributes = function (nodes, interpolationConfig) {
  128. var _this = this;
  129. nodes.forEach(function (n) {
  130. if (n instanceof html_ast_1.HtmlElementAst) {
  131. _this._extractMessagesFromAttributes(n, interpolationConfig);
  132. _this._recurseToExtractMessagesFromAttributes(n.children, interpolationConfig);
  133. }
  134. });
  135. };
  136. MessageExtractor.prototype._extractMessagesFromAttributes = function (p, interpolationConfig) {
  137. var _this = this;
  138. var transAttrs = lang_1.isPresent(this._implicitAttrs[p.name]) ? this._implicitAttrs[p.name] : [];
  139. var explicitAttrs = [];
  140. // `i18n-` prefixed attributes should be translated
  141. p.attrs.filter(function (attr) { return attr.name.startsWith(shared_1.I18N_ATTR_PREFIX); }).forEach(function (attr) {
  142. try {
  143. explicitAttrs.push(attr.name.substring(shared_1.I18N_ATTR_PREFIX.length));
  144. _this._messages.push(shared_1.messageFromI18nAttribute(_this._parser, interpolationConfig, p, attr));
  145. }
  146. catch (e) {
  147. if (e instanceof shared_1.I18nError) {
  148. _this._errors.push(e);
  149. }
  150. else {
  151. throw e;
  152. }
  153. }
  154. });
  155. // implicit attributes should also be translated
  156. p.attrs.filter(function (attr) { return !attr.name.startsWith(shared_1.I18N_ATTR_PREFIX); })
  157. .filter(function (attr) { return explicitAttrs.indexOf(attr.name) == -1; })
  158. .filter(function (attr) { return transAttrs.indexOf(attr.name) > -1; })
  159. .forEach(function (attr) {
  160. return _this._messages.push(shared_1.messageFromAttribute(_this._parser, interpolationConfig, attr));
  161. });
  162. };
  163. return MessageExtractor;
  164. }());
  165. exports.MessageExtractor = MessageExtractor;
  166. //# sourceMappingURL=message_extractor.js.map