@angular/localize 13.0.0-rc.1 → 13.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,746 @@
1
+
2
+ import {createRequire as __cjsCompatRequire} from 'module';
3
+ const require = __cjsCompatRequire(import.meta.url);
4
+
5
+ import {
6
+ Diagnostics,
7
+ buildCodeFrameError,
8
+ buildLocalizeReplacement,
9
+ isBabelParseError,
10
+ isLocalize,
11
+ translate,
12
+ types,
13
+ unwrapMessagePartsFromLocalizeCall,
14
+ unwrapMessagePartsFromTemplateLiteral,
15
+ unwrapSubstitutionsFromLocalizeCall
16
+ } from "./chunk-PHXSPSJE.js";
17
+
18
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/es2015_translate_plugin.mjs
19
+ import { getFileSystem } from "@angular/compiler-cli/private/localize";
20
+ function makeEs2015TranslatePlugin(diagnostics, translations, { missingTranslation = "error", localizeName = "$localize" } = {}, fs = getFileSystem()) {
21
+ return {
22
+ visitor: {
23
+ TaggedTemplateExpression(path) {
24
+ try {
25
+ const tag = path.get("tag");
26
+ if (isLocalize(tag, localizeName)) {
27
+ const [messageParts] = unwrapMessagePartsFromTemplateLiteral(path.get("quasi").get("quasis"), fs);
28
+ const translated = translate(diagnostics, translations, messageParts, path.node.quasi.expressions, missingTranslation);
29
+ path.replaceWith(buildLocalizeReplacement(translated[0], translated[1]));
30
+ }
31
+ } catch (e) {
32
+ if (isBabelParseError(e)) {
33
+ throw buildCodeFrameError(fs, path, e);
34
+ } else {
35
+ throw e;
36
+ }
37
+ }
38
+ }
39
+ }
40
+ };
41
+ }
42
+
43
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/es5_translate_plugin.mjs
44
+ import { getFileSystem as getFileSystem2 } from "@angular/compiler-cli/private/localize";
45
+ function makeEs5TranslatePlugin(diagnostics, translations, { missingTranslation = "error", localizeName = "$localize" } = {}, fs = getFileSystem2()) {
46
+ return {
47
+ visitor: {
48
+ CallExpression(callPath) {
49
+ try {
50
+ const calleePath = callPath.get("callee");
51
+ if (isLocalize(calleePath, localizeName)) {
52
+ const [messageParts] = unwrapMessagePartsFromLocalizeCall(callPath, fs);
53
+ const [expressions] = unwrapSubstitutionsFromLocalizeCall(callPath, fs);
54
+ const translated = translate(diagnostics, translations, messageParts, expressions, missingTranslation);
55
+ callPath.replaceWith(buildLocalizeReplacement(translated[0], translated[1]));
56
+ }
57
+ } catch (e) {
58
+ if (isBabelParseError(e)) {
59
+ diagnostics.error(buildCodeFrameError(fs, callPath, e));
60
+ } else {
61
+ throw e;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ };
67
+ }
68
+
69
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/locale_plugin.mjs
70
+ function makeLocalePlugin(locale, { localizeName = "$localize" } = {}) {
71
+ return {
72
+ visitor: {
73
+ MemberExpression(expression) {
74
+ const obj = expression.get("object");
75
+ if (!isLocalize(obj, localizeName)) {
76
+ return;
77
+ }
78
+ const property = expression.get("property");
79
+ if (!property.isIdentifier({ name: "locale" })) {
80
+ return;
81
+ }
82
+ if (expression.parentPath.isAssignmentExpression() && expression.parentPath.get("left") === expression) {
83
+ return;
84
+ }
85
+ const parent = expression.parentPath;
86
+ if (parent.isLogicalExpression({ operator: "&&" }) && parent.get("right") === expression) {
87
+ const left = parent.get("left");
88
+ if (isLocalizeGuard(left, localizeName)) {
89
+ parent.replaceWith(expression);
90
+ } else if (left.isLogicalExpression({ operator: "&&" }) && isLocalizeGuard(left.get("right"), localizeName)) {
91
+ left.replaceWith(left.get("left"));
92
+ }
93
+ }
94
+ expression.replaceWith(types.stringLiteral(locale));
95
+ }
96
+ }
97
+ };
98
+ }
99
+ function isLocalizeGuard(expression, localizeName) {
100
+ if (!expression.isBinaryExpression() || !(expression.node.operator === "!==" || expression.node.operator === "!=")) {
101
+ return false;
102
+ }
103
+ const left = expression.get("left");
104
+ const right = expression.get("right");
105
+ return left.isUnaryExpression({ operator: "typeof" }) && isLocalize(left.get("argument"), localizeName) && right.isStringLiteral({ value: "undefined" }) || right.isUnaryExpression({ operator: "typeof" }) && isLocalize(right.get("argument"), localizeName) && left.isStringLiteral({ value: "undefined" });
106
+ }
107
+
108
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.mjs
109
+ import { \u0275parseTranslation } from "@angular/localize";
110
+ var ArbTranslationParser = class {
111
+ canParse(filePath, contents) {
112
+ const result = this.analyze(filePath, contents);
113
+ return result.canParse && result.hint;
114
+ }
115
+ analyze(_filePath, contents) {
116
+ const diagnostics = new Diagnostics();
117
+ if (!contents.includes('"@@locale"')) {
118
+ return { canParse: false, diagnostics };
119
+ }
120
+ try {
121
+ return { canParse: true, diagnostics, hint: this.tryParseArbFormat(contents) };
122
+ } catch {
123
+ diagnostics.warn("File is not valid JSON.");
124
+ return { canParse: false, diagnostics };
125
+ }
126
+ }
127
+ parse(_filePath, contents, arb = this.tryParseArbFormat(contents)) {
128
+ const bundle = {
129
+ locale: arb["@@locale"],
130
+ translations: {},
131
+ diagnostics: new Diagnostics()
132
+ };
133
+ for (const messageId of Object.keys(arb)) {
134
+ if (messageId.startsWith("@")) {
135
+ continue;
136
+ }
137
+ const targetMessage = arb[messageId];
138
+ bundle.translations[messageId] = \u0275parseTranslation(targetMessage);
139
+ }
140
+ return bundle;
141
+ }
142
+ tryParseArbFormat(contents) {
143
+ const json = JSON.parse(contents);
144
+ if (typeof json["@@locale"] !== "string") {
145
+ throw new Error("Missing @@locale property.");
146
+ }
147
+ return json;
148
+ }
149
+ };
150
+
151
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.mjs
152
+ import { \u0275parseTranslation as \u0275parseTranslation2 } from "@angular/localize";
153
+ import { extname } from "path";
154
+ var SimpleJsonTranslationParser = class {
155
+ canParse(filePath, contents) {
156
+ const result = this.analyze(filePath, contents);
157
+ return result.canParse && result.hint;
158
+ }
159
+ analyze(filePath, contents) {
160
+ const diagnostics = new Diagnostics();
161
+ if (extname(filePath) !== ".json" || !(contents.includes('"locale"') && contents.includes('"translations"'))) {
162
+ diagnostics.warn("File does not have .json extension.");
163
+ return { canParse: false, diagnostics };
164
+ }
165
+ try {
166
+ const json = JSON.parse(contents);
167
+ if (json.locale === void 0) {
168
+ diagnostics.warn('Required "locale" property missing.');
169
+ return { canParse: false, diagnostics };
170
+ }
171
+ if (typeof json.locale !== "string") {
172
+ diagnostics.warn('The "locale" property is not a string.');
173
+ return { canParse: false, diagnostics };
174
+ }
175
+ if (json.translations === void 0) {
176
+ diagnostics.warn('Required "translations" property missing.');
177
+ return { canParse: false, diagnostics };
178
+ }
179
+ if (typeof json.translations !== "object") {
180
+ diagnostics.warn('The "translations" is not an object.');
181
+ return { canParse: false, diagnostics };
182
+ }
183
+ return { canParse: true, diagnostics, hint: json };
184
+ } catch (e) {
185
+ diagnostics.warn("File is not valid JSON.");
186
+ return { canParse: false, diagnostics };
187
+ }
188
+ }
189
+ parse(_filePath, contents, json) {
190
+ const { locale: parsedLocale, translations } = json || JSON.parse(contents);
191
+ const parsedTranslations = {};
192
+ for (const messageId in translations) {
193
+ const targetMessage = translations[messageId];
194
+ parsedTranslations[messageId] = \u0275parseTranslation2(targetMessage);
195
+ }
196
+ return { locale: parsedLocale, translations: parsedTranslations, diagnostics: new Diagnostics() };
197
+ }
198
+ };
199
+
200
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.mjs
201
+ import { ParseErrorLevel as ParseErrorLevel3, visitAll as visitAll2 } from "@angular/compiler";
202
+
203
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/base_visitor.mjs
204
+ var BaseVisitor = class {
205
+ visitElement(_element, _context) {
206
+ }
207
+ visitAttribute(_attribute, _context) {
208
+ }
209
+ visitText(_text, _context) {
210
+ }
211
+ visitComment(_comment, _context) {
212
+ }
213
+ visitExpansion(_expansion, _context) {
214
+ }
215
+ visitExpansionCase(_expansionCase, _context) {
216
+ }
217
+ };
218
+
219
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/message_serializer.mjs
220
+ import { Element as Element2, visitAll } from "@angular/compiler";
221
+
222
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parse_error.mjs
223
+ import { ParseErrorLevel } from "@angular/compiler";
224
+ var TranslationParseError = class extends Error {
225
+ constructor(span, msg, level = ParseErrorLevel.ERROR) {
226
+ super(contextualMessage(span, msg, level));
227
+ this.span = span;
228
+ this.msg = msg;
229
+ this.level = level;
230
+ }
231
+ };
232
+ function contextualMessage(span, msg, level) {
233
+ const ctx = span.start.getContext(100, 2);
234
+ msg += `
235
+ At ${span.start}${span.details ? `, ${span.details}` : ""}:
236
+ `;
237
+ if (ctx) {
238
+ msg += `...${ctx.before}[${ParseErrorLevel[level]} ->]${ctx.after}...
239
+ `;
240
+ }
241
+ return msg;
242
+ }
243
+
244
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.mjs
245
+ import { Element, ParseError, ParseErrorLevel as ParseErrorLevel2, XmlParser } from "@angular/compiler";
246
+ function getAttrOrThrow(element, attrName) {
247
+ const attrValue = getAttribute(element, attrName);
248
+ if (attrValue === void 0) {
249
+ throw new TranslationParseError(element.sourceSpan, `Missing required "${attrName}" attribute:`);
250
+ }
251
+ return attrValue;
252
+ }
253
+ function getAttribute(element, attrName) {
254
+ const attr = element.attrs.find((a) => a.name === attrName);
255
+ return attr !== void 0 ? attr.value : void 0;
256
+ }
257
+ function parseInnerRange(element) {
258
+ const xmlParser = new XmlParser();
259
+ const xml = xmlParser.parse(element.sourceSpan.start.file.content, element.sourceSpan.start.file.url, { tokenizeExpansionForms: true, range: getInnerRange(element) });
260
+ return xml;
261
+ }
262
+ function getInnerRange(element) {
263
+ const start = element.startSourceSpan.end;
264
+ const end = element.endSourceSpan.start;
265
+ return {
266
+ startPos: start.offset,
267
+ startLine: start.line,
268
+ startCol: start.col,
269
+ endPos: end.offset
270
+ };
271
+ }
272
+ function canParseXml(filePath, contents, rootNodeName, attributes) {
273
+ const diagnostics = new Diagnostics();
274
+ const xmlParser = new XmlParser();
275
+ const xml = xmlParser.parse(contents, filePath);
276
+ if (xml.rootNodes.length === 0 || xml.errors.some((error) => error.level === ParseErrorLevel2.ERROR)) {
277
+ xml.errors.forEach((e) => addParseError(diagnostics, e));
278
+ return { canParse: false, diagnostics };
279
+ }
280
+ const rootElements = xml.rootNodes.filter(isNamedElement(rootNodeName));
281
+ const rootElement = rootElements[0];
282
+ if (rootElement === void 0) {
283
+ diagnostics.warn(`The XML file does not contain a <${rootNodeName}> root node.`);
284
+ return { canParse: false, diagnostics };
285
+ }
286
+ for (const attrKey of Object.keys(attributes)) {
287
+ const attr = rootElement.attrs.find((attr2) => attr2.name === attrKey);
288
+ if (attr === void 0 || attr.value !== attributes[attrKey]) {
289
+ addParseDiagnostic(diagnostics, rootElement.sourceSpan, `The <${rootNodeName}> node does not have the required attribute: ${attrKey}="${attributes[attrKey]}".`, ParseErrorLevel2.WARNING);
290
+ return { canParse: false, diagnostics };
291
+ }
292
+ }
293
+ if (rootElements.length > 1) {
294
+ xml.errors.push(new ParseError(xml.rootNodes[1].sourceSpan, "Unexpected root node. XLIFF 1.2 files should only have a single <xliff> root node.", ParseErrorLevel2.WARNING));
295
+ }
296
+ return { canParse: true, diagnostics, hint: { element: rootElement, errors: xml.errors } };
297
+ }
298
+ function isNamedElement(name) {
299
+ function predicate(node) {
300
+ return node instanceof Element && node.name === name;
301
+ }
302
+ return predicate;
303
+ }
304
+ function addParseDiagnostic(diagnostics, sourceSpan, message, level) {
305
+ addParseError(diagnostics, new ParseError(sourceSpan, message, level));
306
+ }
307
+ function addParseError(diagnostics, parseError) {
308
+ if (parseError.level === ParseErrorLevel2.ERROR) {
309
+ diagnostics.error(parseError.toString());
310
+ } else {
311
+ diagnostics.warn(parseError.toString());
312
+ }
313
+ }
314
+ function addErrorsToBundle(bundle, errors) {
315
+ for (const error of errors) {
316
+ addParseError(bundle.diagnostics, error);
317
+ }
318
+ }
319
+
320
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/message_serializer.mjs
321
+ var MessageSerializer = class extends BaseVisitor {
322
+ constructor(renderer, config) {
323
+ super();
324
+ this.renderer = renderer;
325
+ this.config = config;
326
+ }
327
+ serialize(nodes) {
328
+ this.renderer.startRender();
329
+ visitAll(this, nodes);
330
+ this.renderer.endRender();
331
+ return this.renderer.message;
332
+ }
333
+ visitElement(element) {
334
+ if (this.config.placeholder && element.name === this.config.placeholder.elementName) {
335
+ const name = getAttrOrThrow(element, this.config.placeholder.nameAttribute);
336
+ const body = this.config.placeholder.bodyAttribute && getAttribute(element, this.config.placeholder.bodyAttribute);
337
+ this.visitPlaceholder(name, body);
338
+ } else if (this.config.placeholderContainer && element.name === this.config.placeholderContainer.elementName) {
339
+ const start = getAttrOrThrow(element, this.config.placeholderContainer.startAttribute);
340
+ const end = getAttrOrThrow(element, this.config.placeholderContainer.endAttribute);
341
+ this.visitPlaceholderContainer(start, element.children, end);
342
+ } else if (this.config.inlineElements.indexOf(element.name) !== -1) {
343
+ visitAll(this, element.children);
344
+ } else {
345
+ throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
346
+ }
347
+ }
348
+ visitText(text) {
349
+ this.renderer.text(text.value);
350
+ }
351
+ visitExpansion(expansion) {
352
+ this.renderer.startIcu();
353
+ this.renderer.text(`${expansion.switchValue}, ${expansion.type},`);
354
+ visitAll(this, expansion.cases);
355
+ this.renderer.endIcu();
356
+ }
357
+ visitExpansionCase(expansionCase) {
358
+ this.renderer.text(` ${expansionCase.value} {`);
359
+ this.renderer.startContainer();
360
+ visitAll(this, expansionCase.expression);
361
+ this.renderer.closeContainer();
362
+ this.renderer.text(`}`);
363
+ }
364
+ visitContainedNodes(nodes) {
365
+ this.renderer.startContainer();
366
+ visitAll(this, nodes);
367
+ this.renderer.closeContainer();
368
+ }
369
+ visitPlaceholder(name, body) {
370
+ this.renderer.placeholder(name, body);
371
+ }
372
+ visitPlaceholderContainer(startName, children, closeName) {
373
+ this.renderer.startPlaceholder(startName);
374
+ this.visitContainedNodes(children);
375
+ this.renderer.closePlaceholder(closeName);
376
+ }
377
+ isPlaceholderContainer(node) {
378
+ return node instanceof Element2 && node.name === this.config.placeholderContainer.elementName;
379
+ }
380
+ };
381
+
382
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/target_message_renderer.mjs
383
+ import { \u0275makeParsedTranslation } from "@angular/localize";
384
+ var TargetMessageRenderer = class {
385
+ constructor() {
386
+ this.current = { messageParts: [], placeholderNames: [], text: "" };
387
+ this.icuDepth = 0;
388
+ }
389
+ get message() {
390
+ const { messageParts, placeholderNames } = this.current;
391
+ return \u0275makeParsedTranslation(messageParts, placeholderNames);
392
+ }
393
+ startRender() {
394
+ }
395
+ endRender() {
396
+ this.storeMessagePart();
397
+ }
398
+ text(text) {
399
+ this.current.text += text;
400
+ }
401
+ placeholder(name, body) {
402
+ this.renderPlaceholder(name);
403
+ }
404
+ startPlaceholder(name) {
405
+ this.renderPlaceholder(name);
406
+ }
407
+ closePlaceholder(name) {
408
+ this.renderPlaceholder(name);
409
+ }
410
+ startContainer() {
411
+ }
412
+ closeContainer() {
413
+ }
414
+ startIcu() {
415
+ this.icuDepth++;
416
+ this.text("{");
417
+ }
418
+ endIcu() {
419
+ this.icuDepth--;
420
+ this.text("}");
421
+ }
422
+ normalizePlaceholderName(name) {
423
+ return name.replace(/-/g, "_");
424
+ }
425
+ renderPlaceholder(name) {
426
+ name = this.normalizePlaceholderName(name);
427
+ if (this.icuDepth > 0) {
428
+ this.text(`{${name}}`);
429
+ } else {
430
+ this.storeMessagePart();
431
+ this.current.placeholderNames.push(name);
432
+ }
433
+ }
434
+ storeMessagePart() {
435
+ this.current.messageParts.push(this.current.text);
436
+ this.current.text = "";
437
+ }
438
+ };
439
+
440
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/serialize_translation_message.mjs
441
+ function serializeTranslationMessage(element, config) {
442
+ const { rootNodes, errors: parseErrors } = parseInnerRange(element);
443
+ try {
444
+ const serializer = new MessageSerializer(new TargetMessageRenderer(), config);
445
+ const translation = serializer.serialize(rootNodes);
446
+ return { translation, parseErrors, serializeErrors: [] };
447
+ } catch (e) {
448
+ return { translation: null, parseErrors, serializeErrors: [e] };
449
+ }
450
+ }
451
+
452
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.mjs
453
+ var Xliff1TranslationParser = class {
454
+ canParse(filePath, contents) {
455
+ const result = this.analyze(filePath, contents);
456
+ return result.canParse && result.hint;
457
+ }
458
+ analyze(filePath, contents) {
459
+ return canParseXml(filePath, contents, "xliff", { version: "1.2" });
460
+ }
461
+ parse(filePath, contents, hint) {
462
+ if (hint) {
463
+ return this.extractBundle(hint);
464
+ } else {
465
+ return this.extractBundleDeprecated(filePath, contents);
466
+ }
467
+ }
468
+ extractBundle({ element, errors }) {
469
+ const diagnostics = new Diagnostics();
470
+ errors.forEach((e) => addParseError(diagnostics, e));
471
+ if (element.children.length === 0) {
472
+ addParseDiagnostic(diagnostics, element.sourceSpan, "Missing expected <file> element", ParseErrorLevel3.WARNING);
473
+ return { locale: void 0, translations: {}, diagnostics };
474
+ }
475
+ const files = element.children.filter(isNamedElement("file"));
476
+ if (files.length === 0) {
477
+ addParseDiagnostic(diagnostics, element.sourceSpan, "No <file> elements found in <xliff>", ParseErrorLevel3.WARNING);
478
+ } else if (files.length > 1) {
479
+ addParseDiagnostic(diagnostics, files[1].sourceSpan, "More than one <file> element found in <xliff>", ParseErrorLevel3.WARNING);
480
+ }
481
+ const bundle = { locale: void 0, translations: {}, diagnostics };
482
+ const translationVisitor = new XliffTranslationVisitor();
483
+ const localesFound = new Set();
484
+ for (const file of files) {
485
+ const locale = getAttribute(file, "target-language");
486
+ if (locale !== void 0) {
487
+ localesFound.add(locale);
488
+ bundle.locale = locale;
489
+ }
490
+ visitAll2(translationVisitor, file.children, bundle);
491
+ }
492
+ if (localesFound.size > 1) {
493
+ addParseDiagnostic(diagnostics, element.sourceSpan, `More than one locale found in translation file: ${JSON.stringify(Array.from(localesFound))}. Using "${bundle.locale}"`, ParseErrorLevel3.WARNING);
494
+ }
495
+ return bundle;
496
+ }
497
+ extractBundleDeprecated(filePath, contents) {
498
+ const hint = this.canParse(filePath, contents);
499
+ if (!hint) {
500
+ throw new Error(`Unable to parse "${filePath}" as XLIFF 1.2 format.`);
501
+ }
502
+ const bundle = this.extractBundle(hint);
503
+ if (bundle.diagnostics.hasErrors) {
504
+ const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 1.2 format`);
505
+ throw new Error(message);
506
+ }
507
+ return bundle;
508
+ }
509
+ };
510
+ var XliffTranslationVisitor = class extends BaseVisitor {
511
+ visitElement(element, bundle) {
512
+ if (element.name === "trans-unit") {
513
+ this.visitTransUnitElement(element, bundle);
514
+ } else {
515
+ visitAll2(this, element.children, bundle);
516
+ }
517
+ }
518
+ visitTransUnitElement(element, bundle) {
519
+ const id = getAttribute(element, "id");
520
+ if (id === void 0) {
521
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <trans-unit> element.`, ParseErrorLevel3.ERROR);
522
+ return;
523
+ }
524
+ if (bundle.translations[id] !== void 0) {
525
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${id}"`, ParseErrorLevel3.ERROR);
526
+ return;
527
+ }
528
+ let targetMessage = element.children.find(isNamedElement("target"));
529
+ if (targetMessage === void 0) {
530
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing <target> element", ParseErrorLevel3.WARNING);
531
+ targetMessage = element.children.find(isNamedElement("source"));
532
+ if (targetMessage === void 0) {
533
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing required element: one of <target> or <source> is required", ParseErrorLevel3.ERROR);
534
+ return;
535
+ }
536
+ }
537
+ const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(targetMessage, {
538
+ inlineElements: ["g", "bx", "ex", "bpt", "ept", "ph", "it", "mrk"],
539
+ placeholder: { elementName: "x", nameAttribute: "id" }
540
+ });
541
+ if (translation !== null) {
542
+ bundle.translations[id] = translation;
543
+ }
544
+ addErrorsToBundle(bundle, parseErrors);
545
+ addErrorsToBundle(bundle, serializeErrors);
546
+ }
547
+ };
548
+
549
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser.mjs
550
+ import { Element as Element3, ParseErrorLevel as ParseErrorLevel4, visitAll as visitAll3 } from "@angular/compiler";
551
+ var Xliff2TranslationParser = class {
552
+ canParse(filePath, contents) {
553
+ const result = this.analyze(filePath, contents);
554
+ return result.canParse && result.hint;
555
+ }
556
+ analyze(filePath, contents) {
557
+ return canParseXml(filePath, contents, "xliff", { version: "2.0" });
558
+ }
559
+ parse(filePath, contents, hint) {
560
+ if (hint) {
561
+ return this.extractBundle(hint);
562
+ } else {
563
+ return this.extractBundleDeprecated(filePath, contents);
564
+ }
565
+ }
566
+ extractBundle({ element, errors }) {
567
+ const diagnostics = new Diagnostics();
568
+ errors.forEach((e) => addParseError(diagnostics, e));
569
+ const locale = getAttribute(element, "trgLang");
570
+ const files = element.children.filter(isFileElement);
571
+ if (files.length === 0) {
572
+ addParseDiagnostic(diagnostics, element.sourceSpan, "No <file> elements found in <xliff>", ParseErrorLevel4.WARNING);
573
+ } else if (files.length > 1) {
574
+ addParseDiagnostic(diagnostics, files[1].sourceSpan, "More than one <file> element found in <xliff>", ParseErrorLevel4.WARNING);
575
+ }
576
+ const bundle = { locale, translations: {}, diagnostics };
577
+ const translationVisitor = new Xliff2TranslationVisitor();
578
+ for (const file of files) {
579
+ visitAll3(translationVisitor, file.children, { bundle });
580
+ }
581
+ return bundle;
582
+ }
583
+ extractBundleDeprecated(filePath, contents) {
584
+ const hint = this.canParse(filePath, contents);
585
+ if (!hint) {
586
+ throw new Error(`Unable to parse "${filePath}" as XLIFF 2.0 format.`);
587
+ }
588
+ const bundle = this.extractBundle(hint);
589
+ if (bundle.diagnostics.hasErrors) {
590
+ const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 2.0 format`);
591
+ throw new Error(message);
592
+ }
593
+ return bundle;
594
+ }
595
+ };
596
+ var Xliff2TranslationVisitor = class extends BaseVisitor {
597
+ visitElement(element, { bundle, unit }) {
598
+ if (element.name === "unit") {
599
+ this.visitUnitElement(element, bundle);
600
+ } else if (element.name === "segment") {
601
+ this.visitSegmentElement(element, bundle, unit);
602
+ } else {
603
+ visitAll3(this, element.children, { bundle, unit });
604
+ }
605
+ }
606
+ visitUnitElement(element, bundle) {
607
+ const externalId = getAttribute(element, "id");
608
+ if (externalId === void 0) {
609
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <trans-unit> element.`, ParseErrorLevel4.ERROR);
610
+ return;
611
+ }
612
+ if (bundle.translations[externalId] !== void 0) {
613
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${externalId}"`, ParseErrorLevel4.ERROR);
614
+ return;
615
+ }
616
+ visitAll3(this, element.children, { bundle, unit: externalId });
617
+ }
618
+ visitSegmentElement(element, bundle, unit) {
619
+ if (unit === void 0) {
620
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Invalid <segment> element: should be a child of a <unit> element.", ParseErrorLevel4.ERROR);
621
+ return;
622
+ }
623
+ let targetMessage = element.children.find(isNamedElement("target"));
624
+ if (targetMessage === void 0) {
625
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing <target> element", ParseErrorLevel4.WARNING);
626
+ targetMessage = element.children.find(isNamedElement("source"));
627
+ if (targetMessage === void 0) {
628
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing required element: one of <target> or <source> is required", ParseErrorLevel4.ERROR);
629
+ return;
630
+ }
631
+ }
632
+ const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(targetMessage, {
633
+ inlineElements: ["cp", "sc", "ec", "mrk", "sm", "em"],
634
+ placeholder: { elementName: "ph", nameAttribute: "equiv", bodyAttribute: "disp" },
635
+ placeholderContainer: { elementName: "pc", startAttribute: "equivStart", endAttribute: "equivEnd" }
636
+ });
637
+ if (translation !== null) {
638
+ bundle.translations[unit] = translation;
639
+ }
640
+ addErrorsToBundle(bundle, parseErrors);
641
+ addErrorsToBundle(bundle, serializeErrors);
642
+ }
643
+ };
644
+ function isFileElement(node) {
645
+ return node instanceof Element3 && node.name === "file";
646
+ }
647
+
648
+ // bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser.mjs
649
+ import { ParseErrorLevel as ParseErrorLevel5, visitAll as visitAll4 } from "@angular/compiler";
650
+ import { extname as extname2 } from "path";
651
+ var XtbTranslationParser = class {
652
+ canParse(filePath, contents) {
653
+ const result = this.analyze(filePath, contents);
654
+ return result.canParse && result.hint;
655
+ }
656
+ analyze(filePath, contents) {
657
+ const extension = extname2(filePath);
658
+ if (extension !== ".xtb" && extension !== ".xmb") {
659
+ const diagnostics = new Diagnostics();
660
+ diagnostics.warn("Must have xtb or xmb extension.");
661
+ return { canParse: false, diagnostics };
662
+ }
663
+ return canParseXml(filePath, contents, "translationbundle", {});
664
+ }
665
+ parse(filePath, contents, hint) {
666
+ if (hint) {
667
+ return this.extractBundle(hint);
668
+ } else {
669
+ return this.extractBundleDeprecated(filePath, contents);
670
+ }
671
+ }
672
+ extractBundle({ element, errors }) {
673
+ const langAttr = element.attrs.find((attr) => attr.name === "lang");
674
+ const bundle = {
675
+ locale: langAttr && langAttr.value,
676
+ translations: {},
677
+ diagnostics: new Diagnostics()
678
+ };
679
+ errors.forEach((e) => addParseError(bundle.diagnostics, e));
680
+ const bundleVisitor = new XtbVisitor();
681
+ visitAll4(bundleVisitor, element.children, bundle);
682
+ return bundle;
683
+ }
684
+ extractBundleDeprecated(filePath, contents) {
685
+ const hint = this.canParse(filePath, contents);
686
+ if (!hint) {
687
+ throw new Error(`Unable to parse "${filePath}" as XMB/XTB format.`);
688
+ }
689
+ const bundle = this.extractBundle(hint);
690
+ if (bundle.diagnostics.hasErrors) {
691
+ const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XMB/XTB format`);
692
+ throw new Error(message);
693
+ }
694
+ return bundle;
695
+ }
696
+ };
697
+ var XtbVisitor = class extends BaseVisitor {
698
+ visitElement(element, bundle) {
699
+ switch (element.name) {
700
+ case "translation":
701
+ const id = getAttribute(element, "id");
702
+ if (id === void 0) {
703
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <translation> element.`, ParseErrorLevel5.ERROR);
704
+ return;
705
+ }
706
+ if (bundle.translations[id] !== void 0) {
707
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${id}"`, ParseErrorLevel5.ERROR);
708
+ return;
709
+ }
710
+ const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(element, { inlineElements: [], placeholder: { elementName: "ph", nameAttribute: "name" } });
711
+ if (parseErrors.length) {
712
+ bundle.diagnostics.warn(computeParseWarning(id, parseErrors));
713
+ } else if (translation !== null) {
714
+ bundle.translations[id] = translation;
715
+ }
716
+ addErrorsToBundle(bundle, serializeErrors);
717
+ break;
718
+ default:
719
+ addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Unexpected <${element.name}> tag.`, ParseErrorLevel5.ERROR);
720
+ }
721
+ }
722
+ };
723
+ function computeParseWarning(id, errors) {
724
+ const msg = errors.map((e) => e.toString()).join("\n");
725
+ return `Could not parse message with id "${id}" - perhaps it has an unrecognised ICU format?
726
+ ` + msg;
727
+ }
728
+
729
+ export {
730
+ makeEs2015TranslatePlugin,
731
+ makeEs5TranslatePlugin,
732
+ makeLocalePlugin,
733
+ ArbTranslationParser,
734
+ SimpleJsonTranslationParser,
735
+ Xliff1TranslationParser,
736
+ Xliff2TranslationParser,
737
+ XtbTranslationParser
738
+ };
739
+ /**
740
+ * @license
741
+ * Copyright Google LLC All Rights Reserved.
742
+ *
743
+ * Use of this source code is governed by an MIT-style license that can be
744
+ * found in the LICENSE file at https://angular.io/license
745
+ */
746
+ //# sourceMappingURL=chunk-JSM2M4H6.js.map