@angular/localize 13.0.0-next.9 → 13.0.0-rc.3
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.
- package/{esm2015/index.js → esm2020/index.mjs} +0 -0
- package/esm2020/init/index.mjs +12 -0
- package/{esm2015/localize.js → esm2020/localize.mjs} +0 -0
- package/{esm2015/private.js → esm2020/private.mjs} +0 -0
- package/{esm2015/src/localize/index.js → esm2020/src/localize/index.mjs} +0 -0
- package/{esm2015/src/localize/src/global.js → esm2020/src/localize/src/global.mjs} +0 -0
- package/{esm2015/src/localize/src/localize.js → esm2020/src/localize/src/localize.mjs} +2 -2
- package/{esm2015/src/translate.js → esm2020/src/translate.mjs} +0 -0
- package/{esm2015/src/utils/index.js → esm2020/src/utils/index.mjs} +0 -0
- package/{esm2015/src/utils/src/constants.js → esm2020/src/utils/src/constants.mjs} +0 -0
- package/esm2020/src/utils/src/messages.mjs +159 -0
- package/{esm2015/src/utils/src/translations.js → esm2020/src/utils/src/translations.mjs} +0 -0
- package/fesm2015/{init.js → init.mjs} +3 -3
- package/fesm2015/init.mjs.map +1 -0
- package/fesm2015/{localize.js → localize.mjs} +7 -7
- package/fesm2015/localize.mjs.map +1 -0
- package/fesm2020/init.mjs +204 -0
- package/fesm2020/init.mjs.map +1 -0
- package/fesm2020/localize.mjs +449 -0
- package/fesm2020/localize.mjs.map +1 -0
- package/init/index.d.ts +1 -1
- package/init/package.json +5 -5
- package/package.json +43 -18
- package/schematics/ng-add/index.js +1 -1
- package/src/localize/src/localize.d.ts +1 -1
- package/src/utils/src/messages.d.ts +1 -1
- package/tools/README.md +4 -0
- package/tools/bundles/index.js +1929 -0
- package/tools/bundles/index.js.map +6 -0
- package/tools/bundles/src/extract/cli.js +1278 -0
- package/tools/bundles/src/extract/cli.js.map +6 -0
- package/tools/bundles/src/migrate/cli.js +77 -0
- package/tools/bundles/src/migrate/cli.js.map +6 -0
- package/tools/bundles/src/translate/cli.js +1340 -0
- package/tools/bundles/src/translate/cli.js.map +6 -0
- package/tools/bundles_metadata.json +1 -0
- package/tools/index.d.ts +26 -0
- package/tools/src/babel_core.d.ts +38 -0
- package/{src/tools → tools}/src/diagnostics.d.ts +1 -1
- package/tools/src/extract/cli.d.ts +10 -0
- package/{src/tools → tools}/src/extract/duplicates.d.ts +2 -2
- package/{src/tools → tools}/src/extract/extraction.d.ts +2 -3
- package/{src/tools/src/extract/main.d.ts → tools/src/extract/index.d.ts} +2 -4
- package/{src/tools → tools}/src/extract/source_files/es2015_extract_plugin.d.ts +3 -3
- package/{src/tools → tools}/src/extract/source_files/es5_extract_plugin.d.ts +3 -3
- package/{src/tools → tools}/src/extract/translation_files/arb_translation_serializer.d.ts +2 -2
- package/{src/tools → tools}/src/extract/translation_files/format_options.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/icu_parsing.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/json_translation_serializer.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/legacy_message_id_migration_serializer.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/translation_serializer.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/utils.d.ts +1 -1
- package/{src/tools → tools}/src/extract/translation_files/xliff1_translation_serializer.d.ts +2 -2
- package/{src/tools → tools}/src/extract/translation_files/xliff2_translation_serializer.d.ts +2 -2
- package/{src/tools → tools}/src/extract/translation_files/xmb_translation_serializer.d.ts +2 -2
- package/{src/tools → tools}/src/extract/translation_files/xml_file.d.ts +1 -1
- package/tools/src/migrate/cli.d.ts +10 -0
- package/{src/tools/src/migrate/main.d.ts → tools/src/migrate/index.d.ts} +9 -3
- package/{src/tools → tools}/src/migrate/migrate.d.ts +1 -1
- package/{src/tools → tools}/src/source_file_utils.d.ts +3 -3
- package/{src/tools → tools}/src/translate/asset_files/asset_translation_handler.d.ts +2 -2
- package/tools/src/translate/cli.d.ts +3 -0
- package/{src/tools/src/translate/main.d.ts → tools/src/translate/index.d.ts} +1 -2
- package/{src/tools → tools}/src/translate/output_path.d.ts +2 -2
- package/{src/tools → tools}/src/translate/source_files/es2015_translate_plugin.d.ts +3 -3
- package/{src/tools → tools}/src/translate/source_files/es5_translate_plugin.d.ts +3 -3
- package/{src/tools → tools}/src/translate/source_files/locale_plugin.d.ts +2 -2
- package/{src/tools → tools}/src/translate/source_files/source_file_translation_handler.d.ts +2 -2
- package/{src/tools → tools}/src/translate/translation_files/base_visitor.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/message_serialization/message_renderer.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/message_serialization/message_serializer.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/message_serialization/target_message_renderer.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_loader.d.ts +2 -2
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/arb_translation_parser.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/serialize_translation_message.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/simple_json_translation_parser.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/translation_parse_error.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/translation_parser.d.ts +2 -2
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/translation_utils.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/xliff1_translation_parser.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/xliff2_translation_parser.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translation_files/translation_parsers/xtb_translation_parser.d.ts +1 -1
- package/{src/tools → tools}/src/translate/translator.d.ts +2 -2
- package/bundles/localize-init.umd.js +0 -218
- package/bundles/localize-init.umd.js.map +0 -1
- package/bundles/localize.umd.js +0 -782
- package/bundles/localize.umd.js.map +0 -1
- package/esm2015/init/index.js +0 -12
- package/esm2015/init/init.externs.js +0 -0
- package/esm2015/localize.externs.js +0 -0
- package/esm2015/src/localize/localize.externs.js +0 -0
- package/esm2015/src/utils/src/messages.js +0 -159
- package/esm2015/src/utils/utils.externs.js +0 -0
- package/fesm2015/init.js.map +0 -1
- package/fesm2015/localize.js.map +0 -1
- package/init.d.ts +0 -7
- package/src/tools/src/diagnostics.js +0 -68
- package/src/tools/src/extract/duplicates.js +0 -84
- package/src/tools/src/extract/extraction.js +0 -134
- package/src/tools/src/extract/main.js +0 -184
- package/src/tools/src/extract/source_files/es2015_extract_plugin.js +0 -36
- package/src/tools/src/extract/source_files/es5_extract_plugin.js +0 -49
- package/src/tools/src/extract/translation_files/arb_translation_serializer.js +0 -107
- package/src/tools/src/extract/translation_files/format_options.js +0 -52
- package/src/tools/src/extract/translation_files/icu_parsing.js +0 -224
- package/src/tools/src/extract/translation_files/json_translation_serializer.js +0 -47
- package/src/tools/src/extract/translation_files/legacy_message_id_migration_serializer.js +0 -63
- package/src/tools/src/extract/translation_files/translation_serializer.js +0 -13
- package/src/tools/src/extract/translation_files/utils.js +0 -102
- package/src/tools/src/extract/translation_files/xliff1_translation_serializer.js +0 -242
- package/src/tools/src/extract/translation_files/xliff2_translation_serializer.js +0 -231
- package/src/tools/src/extract/translation_files/xmb_translation_serializer.js +0 -139
- package/src/tools/src/extract/translation_files/xml_file.js +0 -117
- package/src/tools/src/migrate/main.js +0 -79
- package/src/tools/src/migrate/migrate.js +0 -48
- package/src/tools/src/source_file_utils.js +0 -443
- package/src/tools/src/translate/asset_files/asset_translation_handler.js +0 -65
- package/src/tools/src/translate/main.js +0 -157
- package/src/tools/src/translate/output_path.js +0 -28
- package/src/tools/src/translate/source_files/es2015_translate_plugin.js +0 -60
- package/src/tools/src/translate/source_files/es5_translate_plugin.js +0 -58
- package/src/tools/src/translate/source_files/locale_plugin.js +0 -95
- package/src/tools/src/translate/source_files/source_file_translation_handler.js +0 -127
- package/src/tools/src/translate/translation_files/base_visitor.js +0 -31
- package/src/tools/src/translate/translation_files/message_serialization/message_renderer.js +0 -20
- package/src/tools/src/translate/translation_files/message_serialization/message_serializer.js +0 -100
- package/src/tools/src/translate/translation_files/message_serialization/target_message_renderer.js +0 -84
- package/src/tools/src/translate/translation_files/translation_loader.js +0 -148
- package/src/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.js +0 -111
- package/src/tools/src/translate/translation_files/translation_parsers/serialize_translation_message.js +0 -32
- package/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.js +0 -96
- package/src/tools/src/translate/translation_files/translation_parsers/translation_parse_error.js +0 -47
- package/src/tools/src/translate/translation_files/translation_parsers/translation_parser.js +0 -13
- package/src/tools/src/translate/translation_files/translation_parsers/translation_utils.js +0 -175
- package/src/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.js +0 -175
- package/src/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser.js +0 -166
- package/src/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser.js +0 -133
- package/src/tools/src/translate/translator.js +0 -53
|
@@ -0,0 +1,1340 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import {createRequire as __cjsCompatRequire} from 'module';
|
|
4
|
+
const require = __cjsCompatRequire(import.meta.url);
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defProps = Object.defineProperties;
|
|
8
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
9
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
|
|
26
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/cli.mjs
|
|
27
|
+
import { NodeJSFileSystem, setFileSystem } from "@angular/compiler-cli/private/localize";
|
|
28
|
+
import glob from "glob";
|
|
29
|
+
import yargs from "yargs";
|
|
30
|
+
|
|
31
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/diagnostics.mjs
|
|
32
|
+
var Diagnostics = class {
|
|
33
|
+
constructor() {
|
|
34
|
+
this.messages = [];
|
|
35
|
+
}
|
|
36
|
+
get hasErrors() {
|
|
37
|
+
return this.messages.some((m) => m.type === "error");
|
|
38
|
+
}
|
|
39
|
+
add(type, message) {
|
|
40
|
+
if (type !== "ignore") {
|
|
41
|
+
this.messages.push({ type, message });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
warn(message) {
|
|
45
|
+
this.messages.push({ type: "warning", message });
|
|
46
|
+
}
|
|
47
|
+
error(message) {
|
|
48
|
+
this.messages.push({ type: "error", message });
|
|
49
|
+
}
|
|
50
|
+
merge(other) {
|
|
51
|
+
this.messages.push(...other.messages);
|
|
52
|
+
}
|
|
53
|
+
formatDiagnostics(message) {
|
|
54
|
+
const errors = this.messages.filter((d) => d.type === "error").map((d) => " - " + d.message);
|
|
55
|
+
const warnings = this.messages.filter((d) => d.type === "warning").map((d) => " - " + d.message);
|
|
56
|
+
if (errors.length) {
|
|
57
|
+
message += "\nERRORS:\n" + errors.join("\n");
|
|
58
|
+
}
|
|
59
|
+
if (warnings.length) {
|
|
60
|
+
message += "\nWARNINGS:\n" + warnings.join("\n");
|
|
61
|
+
}
|
|
62
|
+
return message;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/output_path.mjs
|
|
67
|
+
function getOutputPathFn(fs2, outputFolder) {
|
|
68
|
+
const [pre, post] = outputFolder.split("{{LOCALE}}");
|
|
69
|
+
return post === void 0 ? (_locale, relativePath) => fs2.join(pre, relativePath) : (locale, relativePath) => fs2.join(pre + locale + post, relativePath);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/index.mjs
|
|
73
|
+
import { getFileSystem as getFileSystem4, relativeFrom } from "@angular/compiler-cli/private/localize";
|
|
74
|
+
|
|
75
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/asset_files/asset_translation_handler.mjs
|
|
76
|
+
import { absoluteFrom } from "@angular/compiler-cli/private/localize";
|
|
77
|
+
var AssetTranslationHandler = class {
|
|
78
|
+
constructor(fs2) {
|
|
79
|
+
this.fs = fs2;
|
|
80
|
+
}
|
|
81
|
+
canTranslate(_relativeFilePath, _contents) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
translate(diagnostics2, _sourceRoot, relativeFilePath, contents, outputPathFn2, translations, sourceLocale2) {
|
|
85
|
+
for (const translation of translations) {
|
|
86
|
+
this.writeAssetFile(diagnostics2, outputPathFn2, translation.locale, relativeFilePath, contents);
|
|
87
|
+
}
|
|
88
|
+
if (sourceLocale2 !== void 0) {
|
|
89
|
+
this.writeAssetFile(diagnostics2, outputPathFn2, sourceLocale2, relativeFilePath, contents);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
writeAssetFile(diagnostics2, outputPathFn2, locale, relativeFilePath, contents) {
|
|
93
|
+
try {
|
|
94
|
+
const outputPath = absoluteFrom(outputPathFn2(locale, relativeFilePath));
|
|
95
|
+
this.fs.ensureDir(this.fs.dirname(outputPath));
|
|
96
|
+
this.fs.writeFile(outputPath, contents);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
diagnostics2.error(e.message);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/source_file_translation_handler.mjs
|
|
104
|
+
import { absoluteFrom as absoluteFrom2 } from "@angular/compiler-cli/private/localize";
|
|
105
|
+
|
|
106
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/babel_core.mjs
|
|
107
|
+
import * as _babelNamespace from "@babel/core";
|
|
108
|
+
import _babelDefault from "@babel/core";
|
|
109
|
+
var _a;
|
|
110
|
+
var babel = (_a = _babelDefault) != null ? _a : _babelNamespace;
|
|
111
|
+
var _typesNamespace = _babelNamespace.types;
|
|
112
|
+
if (_babelDefault !== void 0) {
|
|
113
|
+
_typesNamespace = _babelDefault.types;
|
|
114
|
+
}
|
|
115
|
+
var types = _typesNamespace;
|
|
116
|
+
var NodePath = babel.NodePath;
|
|
117
|
+
var transformSync = babel.transformSync;
|
|
118
|
+
var parseSync = babel.parseSync;
|
|
119
|
+
var transformFromAstSync = babel.transformFromAstSync;
|
|
120
|
+
|
|
121
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/es2015_translate_plugin.mjs
|
|
122
|
+
import { getFileSystem as getFileSystem2 } from "@angular/compiler-cli/private/localize";
|
|
123
|
+
|
|
124
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/source_file_utils.mjs
|
|
125
|
+
import { getFileSystem } from "@angular/compiler-cli/private/localize";
|
|
126
|
+
import { \u0275isMissingTranslationError, \u0275makeTemplateObject, \u0275translate } from "@angular/localize";
|
|
127
|
+
function isLocalize(expression, localizeName) {
|
|
128
|
+
return isNamedIdentifier(expression, localizeName) && isGlobalIdentifier(expression);
|
|
129
|
+
}
|
|
130
|
+
function isNamedIdentifier(expression, name) {
|
|
131
|
+
return expression.isIdentifier() && expression.node.name === name;
|
|
132
|
+
}
|
|
133
|
+
function isGlobalIdentifier(identifier) {
|
|
134
|
+
return !identifier.scope || !identifier.scope.hasBinding(identifier.node.name);
|
|
135
|
+
}
|
|
136
|
+
function buildLocalizeReplacement(messageParts, substitutions) {
|
|
137
|
+
let mappedString = types.stringLiteral(messageParts[0]);
|
|
138
|
+
for (let i = 1; i < messageParts.length; i++) {
|
|
139
|
+
mappedString = types.binaryExpression("+", mappedString, wrapInParensIfNecessary(substitutions[i - 1]));
|
|
140
|
+
mappedString = types.binaryExpression("+", mappedString, types.stringLiteral(messageParts[i]));
|
|
141
|
+
}
|
|
142
|
+
return mappedString;
|
|
143
|
+
}
|
|
144
|
+
function unwrapMessagePartsFromLocalizeCall(call, fs2 = getFileSystem()) {
|
|
145
|
+
let cooked = call.get("arguments")[0];
|
|
146
|
+
if (cooked === void 0) {
|
|
147
|
+
throw new BabelParseError(call.node, "`$localize` called without any arguments.");
|
|
148
|
+
}
|
|
149
|
+
if (!cooked.isExpression()) {
|
|
150
|
+
throw new BabelParseError(cooked.node, "Unexpected argument to `$localize` (expected an array).");
|
|
151
|
+
}
|
|
152
|
+
let raw = cooked;
|
|
153
|
+
if (cooked.isLogicalExpression() && cooked.node.operator === "||" && cooked.get("left").isIdentifier()) {
|
|
154
|
+
const right = cooked.get("right");
|
|
155
|
+
if (right.isAssignmentExpression()) {
|
|
156
|
+
cooked = right.get("right");
|
|
157
|
+
if (!cooked.isExpression()) {
|
|
158
|
+
throw new BabelParseError(cooked.node, 'Unexpected "makeTemplateObject()" function (expected an expression).');
|
|
159
|
+
}
|
|
160
|
+
} else if (right.isSequenceExpression()) {
|
|
161
|
+
const expressions = right.get("expressions");
|
|
162
|
+
if (expressions.length > 2) {
|
|
163
|
+
const [first, second] = expressions;
|
|
164
|
+
if (first.isAssignmentExpression()) {
|
|
165
|
+
cooked = first.get("right");
|
|
166
|
+
if (!cooked.isExpression()) {
|
|
167
|
+
throw new BabelParseError(first.node, "Unexpected cooked value, expected an expression.");
|
|
168
|
+
}
|
|
169
|
+
if (second.isAssignmentExpression()) {
|
|
170
|
+
raw = second.get("right");
|
|
171
|
+
if (!raw.isExpression()) {
|
|
172
|
+
throw new BabelParseError(second.node, "Unexpected raw value, expected an expression.");
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
raw = cooked;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (cooked.isCallExpression()) {
|
|
182
|
+
let call2 = cooked;
|
|
183
|
+
if (call2.get("arguments").length === 0) {
|
|
184
|
+
call2 = unwrapLazyLoadHelperCall(call2);
|
|
185
|
+
}
|
|
186
|
+
cooked = call2.get("arguments")[0];
|
|
187
|
+
if (!cooked.isExpression()) {
|
|
188
|
+
throw new BabelParseError(cooked.node, 'Unexpected `cooked` argument to the "makeTemplateObject()" function (expected an expression).');
|
|
189
|
+
}
|
|
190
|
+
const arg2 = call2.get("arguments")[1];
|
|
191
|
+
if (arg2 && !arg2.isExpression()) {
|
|
192
|
+
throw new BabelParseError(arg2.node, 'Unexpected `raw` argument to the "makeTemplateObject()" function (expected an expression).');
|
|
193
|
+
}
|
|
194
|
+
raw = arg2 !== void 0 ? arg2 : cooked;
|
|
195
|
+
}
|
|
196
|
+
const [cookedStrings] = unwrapStringLiteralArray(cooked, fs2);
|
|
197
|
+
const [rawStrings, rawLocations] = unwrapStringLiteralArray(raw, fs2);
|
|
198
|
+
return [\u0275makeTemplateObject(cookedStrings, rawStrings), rawLocations];
|
|
199
|
+
}
|
|
200
|
+
function unwrapSubstitutionsFromLocalizeCall(call, fs2 = getFileSystem()) {
|
|
201
|
+
const expressions = call.get("arguments").splice(1);
|
|
202
|
+
if (!isArrayOfExpressions(expressions)) {
|
|
203
|
+
const badExpression = expressions.find((expression) => !expression.isExpression());
|
|
204
|
+
throw new BabelParseError(badExpression.node, "Invalid substitutions for `$localize` (expected all substitution arguments to be expressions).");
|
|
205
|
+
}
|
|
206
|
+
return [
|
|
207
|
+
expressions.map((path) => path.node),
|
|
208
|
+
expressions.map((expression) => getLocation(fs2, expression))
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
function unwrapMessagePartsFromTemplateLiteral(elements, fs2 = getFileSystem()) {
|
|
212
|
+
const cooked = elements.map((q) => {
|
|
213
|
+
if (q.node.value.cooked === void 0) {
|
|
214
|
+
throw new BabelParseError(q.node, `Unexpected undefined message part in "${elements.map((q2) => q2.node.value.cooked)}"`);
|
|
215
|
+
}
|
|
216
|
+
return q.node.value.cooked;
|
|
217
|
+
});
|
|
218
|
+
const raw = elements.map((q) => q.node.value.raw);
|
|
219
|
+
const locations = elements.map((q) => getLocation(fs2, q));
|
|
220
|
+
return [\u0275makeTemplateObject(cooked, raw), locations];
|
|
221
|
+
}
|
|
222
|
+
function wrapInParensIfNecessary(expression) {
|
|
223
|
+
if (types.isBinaryExpression(expression)) {
|
|
224
|
+
return types.parenthesizedExpression(expression);
|
|
225
|
+
} else {
|
|
226
|
+
return expression;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function unwrapStringLiteralArray(array, fs2 = getFileSystem()) {
|
|
230
|
+
if (!isStringLiteralArray(array.node)) {
|
|
231
|
+
throw new BabelParseError(array.node, "Unexpected messageParts for `$localize` (expected an array of strings).");
|
|
232
|
+
}
|
|
233
|
+
const elements = array.get("elements");
|
|
234
|
+
return [elements.map((str) => str.node.value), elements.map((str) => getLocation(fs2, str))];
|
|
235
|
+
}
|
|
236
|
+
function unwrapLazyLoadHelperCall(call) {
|
|
237
|
+
const callee = call.get("callee");
|
|
238
|
+
if (!callee.isIdentifier()) {
|
|
239
|
+
throw new BabelParseError(callee.node, "Unexpected lazy-load helper call (expected a call of the form `_templateObject()`).");
|
|
240
|
+
}
|
|
241
|
+
const lazyLoadBinding = call.scope.getBinding(callee.node.name);
|
|
242
|
+
if (!lazyLoadBinding) {
|
|
243
|
+
throw new BabelParseError(callee.node, "Missing declaration for lazy-load helper function");
|
|
244
|
+
}
|
|
245
|
+
const lazyLoadFn = lazyLoadBinding.path;
|
|
246
|
+
if (!lazyLoadFn.isFunctionDeclaration()) {
|
|
247
|
+
throw new BabelParseError(lazyLoadFn.node, "Unexpected expression (expected a function declaration");
|
|
248
|
+
}
|
|
249
|
+
const returnedNode = getReturnedExpression(lazyLoadFn);
|
|
250
|
+
if (returnedNode.isCallExpression()) {
|
|
251
|
+
return returnedNode;
|
|
252
|
+
}
|
|
253
|
+
if (returnedNode.isIdentifier()) {
|
|
254
|
+
const identifierName = returnedNode.node.name;
|
|
255
|
+
const declaration = returnedNode.scope.getBinding(identifierName);
|
|
256
|
+
if (declaration === void 0) {
|
|
257
|
+
throw new BabelParseError(returnedNode.node, "Missing declaration for return value from helper.");
|
|
258
|
+
}
|
|
259
|
+
if (!declaration.path.isVariableDeclarator()) {
|
|
260
|
+
throw new BabelParseError(declaration.path.node, "Unexpected helper return value declaration (expected a variable declaration).");
|
|
261
|
+
}
|
|
262
|
+
const initializer = declaration.path.get("init");
|
|
263
|
+
if (!initializer.isCallExpression()) {
|
|
264
|
+
throw new BabelParseError(declaration.path.node, "Unexpected return value from helper (expected a call expression).");
|
|
265
|
+
}
|
|
266
|
+
if (lazyLoadBinding.references === 1) {
|
|
267
|
+
lazyLoadFn.remove();
|
|
268
|
+
}
|
|
269
|
+
return initializer;
|
|
270
|
+
}
|
|
271
|
+
return call;
|
|
272
|
+
}
|
|
273
|
+
function getReturnedExpression(fn) {
|
|
274
|
+
const bodyStatements = fn.get("body").get("body");
|
|
275
|
+
for (const statement of bodyStatements) {
|
|
276
|
+
if (statement.isReturnStatement()) {
|
|
277
|
+
const argument = statement.get("argument");
|
|
278
|
+
if (argument.isSequenceExpression()) {
|
|
279
|
+
const expressions = argument.get("expressions");
|
|
280
|
+
return Array.isArray(expressions) ? expressions[expressions.length - 1] : expressions;
|
|
281
|
+
} else if (argument.isExpression()) {
|
|
282
|
+
return argument;
|
|
283
|
+
} else {
|
|
284
|
+
throw new BabelParseError(statement.node, "Invalid return argument in helper function (expected an expression).");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
throw new BabelParseError(fn.node, "Missing return statement in helper function.");
|
|
289
|
+
}
|
|
290
|
+
function isStringLiteralArray(node) {
|
|
291
|
+
return types.isArrayExpression(node) && node.elements.every((element) => types.isStringLiteral(element));
|
|
292
|
+
}
|
|
293
|
+
function isArrayOfExpressions(paths) {
|
|
294
|
+
return paths.every((element) => element.isExpression());
|
|
295
|
+
}
|
|
296
|
+
function translate(diagnostics2, translations, messageParts, substitutions, missingTranslation2) {
|
|
297
|
+
try {
|
|
298
|
+
return \u0275translate(translations, messageParts, substitutions);
|
|
299
|
+
} catch (e) {
|
|
300
|
+
if (\u0275isMissingTranslationError(e)) {
|
|
301
|
+
diagnostics2.add(missingTranslation2, e.message);
|
|
302
|
+
return [
|
|
303
|
+
\u0275makeTemplateObject(e.parsedMessage.messageParts, e.parsedMessage.messageParts),
|
|
304
|
+
substitutions
|
|
305
|
+
];
|
|
306
|
+
} else {
|
|
307
|
+
diagnostics2.error(e.message);
|
|
308
|
+
return [messageParts, substitutions];
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
var BabelParseError = class extends Error {
|
|
313
|
+
constructor(node, message) {
|
|
314
|
+
super(message);
|
|
315
|
+
this.node = node;
|
|
316
|
+
this.type = "BabelParseError";
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
function isBabelParseError(e) {
|
|
320
|
+
return e.type === "BabelParseError";
|
|
321
|
+
}
|
|
322
|
+
function buildCodeFrameError(fs2, path, e) {
|
|
323
|
+
let filename = path.hub.file.opts.filename;
|
|
324
|
+
if (filename) {
|
|
325
|
+
filename = fs2.resolve(filename);
|
|
326
|
+
let cwd = path.hub.file.opts.cwd;
|
|
327
|
+
if (cwd) {
|
|
328
|
+
cwd = fs2.resolve(cwd);
|
|
329
|
+
filename = fs2.relative(cwd, filename);
|
|
330
|
+
}
|
|
331
|
+
} else {
|
|
332
|
+
filename = "(unknown file)";
|
|
333
|
+
}
|
|
334
|
+
const message = path.hub.file.buildCodeFrameError(e.node, e.message).message;
|
|
335
|
+
return `${filename}: ${message}`;
|
|
336
|
+
}
|
|
337
|
+
function getLocation(fs2, startPath, endPath) {
|
|
338
|
+
const startLocation = startPath.node.loc;
|
|
339
|
+
const file = getFileFromPath(fs2, startPath);
|
|
340
|
+
if (!startLocation || !file) {
|
|
341
|
+
return void 0;
|
|
342
|
+
}
|
|
343
|
+
const endLocation = endPath && getFileFromPath(fs2, endPath) === file && endPath.node.loc || startLocation;
|
|
344
|
+
return {
|
|
345
|
+
start: getLineAndColumn(startLocation.start),
|
|
346
|
+
end: getLineAndColumn(endLocation.end),
|
|
347
|
+
file,
|
|
348
|
+
text: getText(startPath)
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function getFileFromPath(fs2, path) {
|
|
352
|
+
var _a2;
|
|
353
|
+
const opts = path == null ? void 0 : path.hub.file.opts;
|
|
354
|
+
const filename = opts == null ? void 0 : opts.filename;
|
|
355
|
+
if (!filename) {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
const relativePath = fs2.relative(opts.cwd, filename);
|
|
359
|
+
const root = (_a2 = opts.generatorOpts.sourceRoot) != null ? _a2 : opts.cwd;
|
|
360
|
+
const absPath = fs2.resolve(root, relativePath);
|
|
361
|
+
return absPath;
|
|
362
|
+
}
|
|
363
|
+
function getLineAndColumn(loc) {
|
|
364
|
+
return { line: loc.line - 1, column: loc.column };
|
|
365
|
+
}
|
|
366
|
+
function getText(path) {
|
|
367
|
+
if (path.node.start === null || path.node.end === null) {
|
|
368
|
+
return void 0;
|
|
369
|
+
}
|
|
370
|
+
return path.hub.file.code.substring(path.node.start, path.node.end);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/es2015_translate_plugin.mjs
|
|
374
|
+
function makeEs2015TranslatePlugin(diagnostics2, translations, { missingTranslation: missingTranslation2 = "error", localizeName = "$localize" } = {}, fs2 = getFileSystem2()) {
|
|
375
|
+
return {
|
|
376
|
+
visitor: {
|
|
377
|
+
TaggedTemplateExpression(path) {
|
|
378
|
+
try {
|
|
379
|
+
const tag = path.get("tag");
|
|
380
|
+
if (isLocalize(tag, localizeName)) {
|
|
381
|
+
const [messageParts] = unwrapMessagePartsFromTemplateLiteral(path.get("quasi").get("quasis"), fs2);
|
|
382
|
+
const translated = translate(diagnostics2, translations, messageParts, path.node.quasi.expressions, missingTranslation2);
|
|
383
|
+
path.replaceWith(buildLocalizeReplacement(translated[0], translated[1]));
|
|
384
|
+
}
|
|
385
|
+
} catch (e) {
|
|
386
|
+
if (isBabelParseError(e)) {
|
|
387
|
+
throw buildCodeFrameError(fs2, path, e);
|
|
388
|
+
} else {
|
|
389
|
+
throw e;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/es5_translate_plugin.mjs
|
|
398
|
+
import { getFileSystem as getFileSystem3 } from "@angular/compiler-cli/private/localize";
|
|
399
|
+
function makeEs5TranslatePlugin(diagnostics2, translations, { missingTranslation: missingTranslation2 = "error", localizeName = "$localize" } = {}, fs2 = getFileSystem3()) {
|
|
400
|
+
return {
|
|
401
|
+
visitor: {
|
|
402
|
+
CallExpression(callPath) {
|
|
403
|
+
try {
|
|
404
|
+
const calleePath = callPath.get("callee");
|
|
405
|
+
if (isLocalize(calleePath, localizeName)) {
|
|
406
|
+
const [messageParts] = unwrapMessagePartsFromLocalizeCall(callPath, fs2);
|
|
407
|
+
const [expressions] = unwrapSubstitutionsFromLocalizeCall(callPath, fs2);
|
|
408
|
+
const translated = translate(diagnostics2, translations, messageParts, expressions, missingTranslation2);
|
|
409
|
+
callPath.replaceWith(buildLocalizeReplacement(translated[0], translated[1]));
|
|
410
|
+
}
|
|
411
|
+
} catch (e) {
|
|
412
|
+
if (isBabelParseError(e)) {
|
|
413
|
+
diagnostics2.error(buildCodeFrameError(fs2, callPath, e));
|
|
414
|
+
} else {
|
|
415
|
+
throw e;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/locale_plugin.mjs
|
|
424
|
+
function makeLocalePlugin(locale, { localizeName = "$localize" } = {}) {
|
|
425
|
+
return {
|
|
426
|
+
visitor: {
|
|
427
|
+
MemberExpression(expression) {
|
|
428
|
+
const obj = expression.get("object");
|
|
429
|
+
if (!isLocalize(obj, localizeName)) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const property = expression.get("property");
|
|
433
|
+
if (!property.isIdentifier({ name: "locale" })) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (expression.parentPath.isAssignmentExpression() && expression.parentPath.get("left") === expression) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const parent = expression.parentPath;
|
|
440
|
+
if (parent.isLogicalExpression({ operator: "&&" }) && parent.get("right") === expression) {
|
|
441
|
+
const left = parent.get("left");
|
|
442
|
+
if (isLocalizeGuard(left, localizeName)) {
|
|
443
|
+
parent.replaceWith(expression);
|
|
444
|
+
} else if (left.isLogicalExpression({ operator: "&&" }) && isLocalizeGuard(left.get("right"), localizeName)) {
|
|
445
|
+
left.replaceWith(left.get("left"));
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
expression.replaceWith(types.stringLiteral(locale));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
function isLocalizeGuard(expression, localizeName) {
|
|
454
|
+
if (!expression.isBinaryExpression() || !(expression.node.operator === "!==" || expression.node.operator === "!=")) {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
const left = expression.get("left");
|
|
458
|
+
const right = expression.get("right");
|
|
459
|
+
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" });
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/source_files/source_file_translation_handler.mjs
|
|
463
|
+
var SourceFileTranslationHandler = class {
|
|
464
|
+
constructor(fs2, translationOptions = {}) {
|
|
465
|
+
this.fs = fs2;
|
|
466
|
+
this.translationOptions = translationOptions;
|
|
467
|
+
this.sourceLocaleOptions = __spreadProps(__spreadValues({}, this.translationOptions), { missingTranslation: "ignore" });
|
|
468
|
+
}
|
|
469
|
+
canTranslate(relativeFilePath, _contents) {
|
|
470
|
+
return this.fs.extname(relativeFilePath) === ".js";
|
|
471
|
+
}
|
|
472
|
+
translate(diagnostics2, sourceRoot, relativeFilePath, contents, outputPathFn2, translations, sourceLocale2) {
|
|
473
|
+
const sourceCode = Buffer.from(contents).toString("utf8");
|
|
474
|
+
if (!sourceCode.includes("$localize")) {
|
|
475
|
+
for (const translation of translations) {
|
|
476
|
+
this.writeSourceFile(diagnostics2, outputPathFn2, translation.locale, relativeFilePath, contents);
|
|
477
|
+
}
|
|
478
|
+
if (sourceLocale2 !== void 0) {
|
|
479
|
+
this.writeSourceFile(diagnostics2, outputPathFn2, sourceLocale2, relativeFilePath, contents);
|
|
480
|
+
}
|
|
481
|
+
} else {
|
|
482
|
+
const ast = parseSync(sourceCode, { sourceRoot, filename: relativeFilePath });
|
|
483
|
+
if (!ast) {
|
|
484
|
+
diagnostics2.error(`Unable to parse source file: ${this.fs.join(sourceRoot, relativeFilePath)}`);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
for (const translationBundle of translations) {
|
|
488
|
+
this.translateFile(diagnostics2, ast, translationBundle, sourceRoot, relativeFilePath, outputPathFn2, this.translationOptions);
|
|
489
|
+
}
|
|
490
|
+
if (sourceLocale2 !== void 0) {
|
|
491
|
+
this.translateFile(diagnostics2, ast, { locale: sourceLocale2, translations: {} }, sourceRoot, relativeFilePath, outputPathFn2, this.sourceLocaleOptions);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
translateFile(diagnostics2, ast, translationBundle, sourceRoot, filename, outputPathFn2, options2) {
|
|
496
|
+
const translated = transformFromAstSync(ast, void 0, {
|
|
497
|
+
compact: true,
|
|
498
|
+
generatorOpts: { minified: true },
|
|
499
|
+
plugins: [
|
|
500
|
+
makeLocalePlugin(translationBundle.locale),
|
|
501
|
+
makeEs2015TranslatePlugin(diagnostics2, translationBundle.translations, options2, this.fs),
|
|
502
|
+
makeEs5TranslatePlugin(diagnostics2, translationBundle.translations, options2, this.fs)
|
|
503
|
+
],
|
|
504
|
+
cwd: sourceRoot,
|
|
505
|
+
filename
|
|
506
|
+
});
|
|
507
|
+
if (translated && translated.code) {
|
|
508
|
+
this.writeSourceFile(diagnostics2, outputPathFn2, translationBundle.locale, filename, translated.code);
|
|
509
|
+
const outputPath = absoluteFrom2(outputPathFn2(translationBundle.locale, filename));
|
|
510
|
+
this.fs.ensureDir(this.fs.dirname(outputPath));
|
|
511
|
+
this.fs.writeFile(outputPath, translated.code);
|
|
512
|
+
} else {
|
|
513
|
+
diagnostics2.error(`Unable to translate source file: ${this.fs.join(sourceRoot, filename)}`);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
writeSourceFile(diagnostics2, outputPathFn2, locale, relativeFilePath, contents) {
|
|
518
|
+
try {
|
|
519
|
+
const outputPath = absoluteFrom2(outputPathFn2(locale, relativeFilePath));
|
|
520
|
+
this.fs.ensureDir(this.fs.dirname(outputPath));
|
|
521
|
+
this.fs.writeFile(outputPath, contents);
|
|
522
|
+
} catch (e) {
|
|
523
|
+
diagnostics2.error(e.message);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_loader.mjs
|
|
529
|
+
var TranslationLoader = class {
|
|
530
|
+
constructor(fs2, translationParsers, duplicateTranslation2, diagnostics2) {
|
|
531
|
+
this.fs = fs2;
|
|
532
|
+
this.translationParsers = translationParsers;
|
|
533
|
+
this.duplicateTranslation = duplicateTranslation2;
|
|
534
|
+
this.diagnostics = diagnostics2;
|
|
535
|
+
}
|
|
536
|
+
loadBundles(translationFilePaths2, translationFileLocales2) {
|
|
537
|
+
return translationFilePaths2.map((filePaths, index) => {
|
|
538
|
+
const providedLocale = translationFileLocales2[index];
|
|
539
|
+
return this.mergeBundles(filePaths, providedLocale);
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
loadBundle(filePath, providedLocale) {
|
|
543
|
+
const fileContents = this.fs.readFile(filePath);
|
|
544
|
+
const unusedParsers = new Map();
|
|
545
|
+
for (const translationParser of this.translationParsers) {
|
|
546
|
+
const result = translationParser.analyze(filePath, fileContents);
|
|
547
|
+
if (!result.canParse) {
|
|
548
|
+
unusedParsers.set(translationParser, result);
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
const { locale: parsedLocale, translations, diagnostics: diagnostics2 } = translationParser.parse(filePath, fileContents, result.hint);
|
|
552
|
+
if (diagnostics2.hasErrors) {
|
|
553
|
+
throw new Error(diagnostics2.formatDiagnostics(`The translation file "${filePath}" could not be parsed.`));
|
|
554
|
+
}
|
|
555
|
+
const locale = providedLocale || parsedLocale;
|
|
556
|
+
if (locale === void 0) {
|
|
557
|
+
throw new Error(`The translation file "${filePath}" does not contain a target locale and no explicit locale was provided for this file.`);
|
|
558
|
+
}
|
|
559
|
+
if (parsedLocale !== void 0 && providedLocale !== void 0 && parsedLocale !== providedLocale) {
|
|
560
|
+
diagnostics2.warn(`The provided locale "${providedLocale}" does not match the target locale "${parsedLocale}" found in the translation file "${filePath}".`);
|
|
561
|
+
}
|
|
562
|
+
if (this.diagnostics) {
|
|
563
|
+
this.diagnostics.merge(diagnostics2);
|
|
564
|
+
}
|
|
565
|
+
return { locale, translations, diagnostics: diagnostics2 };
|
|
566
|
+
}
|
|
567
|
+
const diagnosticsMessages = [];
|
|
568
|
+
for (const [parser, result] of unusedParsers.entries()) {
|
|
569
|
+
diagnosticsMessages.push(result.diagnostics.formatDiagnostics(`
|
|
570
|
+
${parser.constructor.name} cannot parse translation file.`));
|
|
571
|
+
}
|
|
572
|
+
throw new Error(`There is no "TranslationParser" that can parse this translation file: ${filePath}.` + diagnosticsMessages.join("\n"));
|
|
573
|
+
}
|
|
574
|
+
mergeBundles(filePaths, providedLocale) {
|
|
575
|
+
const bundles = filePaths.map((filePath) => this.loadBundle(filePath, providedLocale));
|
|
576
|
+
const bundle = bundles[0];
|
|
577
|
+
for (let i = 1; i < bundles.length; i++) {
|
|
578
|
+
const nextBundle = bundles[i];
|
|
579
|
+
if (nextBundle.locale !== bundle.locale) {
|
|
580
|
+
if (this.diagnostics) {
|
|
581
|
+
const previousFiles = filePaths.slice(0, i).map((f) => `"${f}"`).join(", ");
|
|
582
|
+
this.diagnostics.warn(`When merging multiple translation files, the target locale "${nextBundle.locale}" found in "${filePaths[i]}" does not match the target locale "${bundle.locale}" found in earlier files [${previousFiles}].`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
Object.keys(nextBundle.translations).forEach((messageId) => {
|
|
586
|
+
var _a2;
|
|
587
|
+
if (bundle.translations[messageId] !== void 0) {
|
|
588
|
+
(_a2 = this.diagnostics) == null ? void 0 : _a2.add(this.duplicateTranslation, `Duplicate translations for message "${messageId}" when merging "${filePaths[i]}".`);
|
|
589
|
+
} else {
|
|
590
|
+
bundle.translations[messageId] = nextBundle.translations[messageId];
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
return bundle;
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.mjs
|
|
599
|
+
import { \u0275parseTranslation } from "@angular/localize";
|
|
600
|
+
var ArbTranslationParser = class {
|
|
601
|
+
canParse(filePath, contents) {
|
|
602
|
+
const result = this.analyze(filePath, contents);
|
|
603
|
+
return result.canParse && result.hint;
|
|
604
|
+
}
|
|
605
|
+
analyze(_filePath, contents) {
|
|
606
|
+
const diagnostics2 = new Diagnostics();
|
|
607
|
+
if (!contents.includes('"@@locale"')) {
|
|
608
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
609
|
+
}
|
|
610
|
+
try {
|
|
611
|
+
return { canParse: true, diagnostics: diagnostics2, hint: this.tryParseArbFormat(contents) };
|
|
612
|
+
} catch {
|
|
613
|
+
diagnostics2.warn("File is not valid JSON.");
|
|
614
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
parse(_filePath, contents, arb = this.tryParseArbFormat(contents)) {
|
|
618
|
+
const bundle = {
|
|
619
|
+
locale: arb["@@locale"],
|
|
620
|
+
translations: {},
|
|
621
|
+
diagnostics: new Diagnostics()
|
|
622
|
+
};
|
|
623
|
+
for (const messageId of Object.keys(arb)) {
|
|
624
|
+
if (messageId.startsWith("@")) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
const targetMessage = arb[messageId];
|
|
628
|
+
bundle.translations[messageId] = \u0275parseTranslation(targetMessage);
|
|
629
|
+
}
|
|
630
|
+
return bundle;
|
|
631
|
+
}
|
|
632
|
+
tryParseArbFormat(contents) {
|
|
633
|
+
const json = JSON.parse(contents);
|
|
634
|
+
if (typeof json["@@locale"] !== "string") {
|
|
635
|
+
throw new Error("Missing @@locale property.");
|
|
636
|
+
}
|
|
637
|
+
return json;
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.mjs
|
|
642
|
+
import { \u0275parseTranslation as \u0275parseTranslation2 } from "@angular/localize";
|
|
643
|
+
import { extname } from "path";
|
|
644
|
+
var SimpleJsonTranslationParser = class {
|
|
645
|
+
canParse(filePath, contents) {
|
|
646
|
+
const result = this.analyze(filePath, contents);
|
|
647
|
+
return result.canParse && result.hint;
|
|
648
|
+
}
|
|
649
|
+
analyze(filePath, contents) {
|
|
650
|
+
const diagnostics2 = new Diagnostics();
|
|
651
|
+
if (extname(filePath) !== ".json" || !(contents.includes('"locale"') && contents.includes('"translations"'))) {
|
|
652
|
+
diagnostics2.warn("File does not have .json extension.");
|
|
653
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
654
|
+
}
|
|
655
|
+
try {
|
|
656
|
+
const json = JSON.parse(contents);
|
|
657
|
+
if (json.locale === void 0) {
|
|
658
|
+
diagnostics2.warn('Required "locale" property missing.');
|
|
659
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
660
|
+
}
|
|
661
|
+
if (typeof json.locale !== "string") {
|
|
662
|
+
diagnostics2.warn('The "locale" property is not a string.');
|
|
663
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
664
|
+
}
|
|
665
|
+
if (json.translations === void 0) {
|
|
666
|
+
diagnostics2.warn('Required "translations" property missing.');
|
|
667
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
668
|
+
}
|
|
669
|
+
if (typeof json.translations !== "object") {
|
|
670
|
+
diagnostics2.warn('The "translations" is not an object.');
|
|
671
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
672
|
+
}
|
|
673
|
+
return { canParse: true, diagnostics: diagnostics2, hint: json };
|
|
674
|
+
} catch (e) {
|
|
675
|
+
diagnostics2.warn("File is not valid JSON.");
|
|
676
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
parse(_filePath, contents, json) {
|
|
680
|
+
const { locale: parsedLocale, translations } = json || JSON.parse(contents);
|
|
681
|
+
const parsedTranslations = {};
|
|
682
|
+
for (const messageId in translations) {
|
|
683
|
+
const targetMessage = translations[messageId];
|
|
684
|
+
parsedTranslations[messageId] = \u0275parseTranslation2(targetMessage);
|
|
685
|
+
}
|
|
686
|
+
return { locale: parsedLocale, translations: parsedTranslations, diagnostics: new Diagnostics() };
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.mjs
|
|
691
|
+
import { ParseErrorLevel as ParseErrorLevel3, visitAll as visitAll2 } from "@angular/compiler";
|
|
692
|
+
|
|
693
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/base_visitor.mjs
|
|
694
|
+
var BaseVisitor = class {
|
|
695
|
+
visitElement(_element, _context) {
|
|
696
|
+
}
|
|
697
|
+
visitAttribute(_attribute, _context) {
|
|
698
|
+
}
|
|
699
|
+
visitText(_text, _context) {
|
|
700
|
+
}
|
|
701
|
+
visitComment(_comment, _context) {
|
|
702
|
+
}
|
|
703
|
+
visitExpansion(_expansion, _context) {
|
|
704
|
+
}
|
|
705
|
+
visitExpansionCase(_expansionCase, _context) {
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/message_serializer.mjs
|
|
710
|
+
import { Element as Element2, visitAll } from "@angular/compiler";
|
|
711
|
+
|
|
712
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parse_error.mjs
|
|
713
|
+
import { ParseErrorLevel } from "@angular/compiler";
|
|
714
|
+
var TranslationParseError = class extends Error {
|
|
715
|
+
constructor(span, msg, level = ParseErrorLevel.ERROR) {
|
|
716
|
+
super(contextualMessage(span, msg, level));
|
|
717
|
+
this.span = span;
|
|
718
|
+
this.msg = msg;
|
|
719
|
+
this.level = level;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
function contextualMessage(span, msg, level) {
|
|
723
|
+
const ctx = span.start.getContext(100, 2);
|
|
724
|
+
msg += `
|
|
725
|
+
At ${span.start}${span.details ? `, ${span.details}` : ""}:
|
|
726
|
+
`;
|
|
727
|
+
if (ctx) {
|
|
728
|
+
msg += `...${ctx.before}[${ParseErrorLevel[level]} ->]${ctx.after}...
|
|
729
|
+
`;
|
|
730
|
+
}
|
|
731
|
+
return msg;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.mjs
|
|
735
|
+
import { Element, ParseError, ParseErrorLevel as ParseErrorLevel2, XmlParser } from "@angular/compiler";
|
|
736
|
+
function getAttrOrThrow(element, attrName) {
|
|
737
|
+
const attrValue = getAttribute(element, attrName);
|
|
738
|
+
if (attrValue === void 0) {
|
|
739
|
+
throw new TranslationParseError(element.sourceSpan, `Missing required "${attrName}" attribute:`);
|
|
740
|
+
}
|
|
741
|
+
return attrValue;
|
|
742
|
+
}
|
|
743
|
+
function getAttribute(element, attrName) {
|
|
744
|
+
const attr = element.attrs.find((a) => a.name === attrName);
|
|
745
|
+
return attr !== void 0 ? attr.value : void 0;
|
|
746
|
+
}
|
|
747
|
+
function parseInnerRange(element) {
|
|
748
|
+
const xmlParser = new XmlParser();
|
|
749
|
+
const xml = xmlParser.parse(element.sourceSpan.start.file.content, element.sourceSpan.start.file.url, { tokenizeExpansionForms: true, range: getInnerRange(element) });
|
|
750
|
+
return xml;
|
|
751
|
+
}
|
|
752
|
+
function getInnerRange(element) {
|
|
753
|
+
const start = element.startSourceSpan.end;
|
|
754
|
+
const end = element.endSourceSpan.start;
|
|
755
|
+
return {
|
|
756
|
+
startPos: start.offset,
|
|
757
|
+
startLine: start.line,
|
|
758
|
+
startCol: start.col,
|
|
759
|
+
endPos: end.offset
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function canParseXml(filePath, contents, rootNodeName, attributes) {
|
|
763
|
+
const diagnostics2 = new Diagnostics();
|
|
764
|
+
const xmlParser = new XmlParser();
|
|
765
|
+
const xml = xmlParser.parse(contents, filePath);
|
|
766
|
+
if (xml.rootNodes.length === 0 || xml.errors.some((error) => error.level === ParseErrorLevel2.ERROR)) {
|
|
767
|
+
xml.errors.forEach((e) => addParseError(diagnostics2, e));
|
|
768
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
769
|
+
}
|
|
770
|
+
const rootElements = xml.rootNodes.filter(isNamedElement(rootNodeName));
|
|
771
|
+
const rootElement = rootElements[0];
|
|
772
|
+
if (rootElement === void 0) {
|
|
773
|
+
diagnostics2.warn(`The XML file does not contain a <${rootNodeName}> root node.`);
|
|
774
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
775
|
+
}
|
|
776
|
+
for (const attrKey of Object.keys(attributes)) {
|
|
777
|
+
const attr = rootElement.attrs.find((attr2) => attr2.name === attrKey);
|
|
778
|
+
if (attr === void 0 || attr.value !== attributes[attrKey]) {
|
|
779
|
+
addParseDiagnostic(diagnostics2, rootElement.sourceSpan, `The <${rootNodeName}> node does not have the required attribute: ${attrKey}="${attributes[attrKey]}".`, ParseErrorLevel2.WARNING);
|
|
780
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (rootElements.length > 1) {
|
|
784
|
+
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));
|
|
785
|
+
}
|
|
786
|
+
return { canParse: true, diagnostics: diagnostics2, hint: { element: rootElement, errors: xml.errors } };
|
|
787
|
+
}
|
|
788
|
+
function isNamedElement(name) {
|
|
789
|
+
function predicate(node) {
|
|
790
|
+
return node instanceof Element && node.name === name;
|
|
791
|
+
}
|
|
792
|
+
return predicate;
|
|
793
|
+
}
|
|
794
|
+
function addParseDiagnostic(diagnostics2, sourceSpan, message, level) {
|
|
795
|
+
addParseError(diagnostics2, new ParseError(sourceSpan, message, level));
|
|
796
|
+
}
|
|
797
|
+
function addParseError(diagnostics2, parseError) {
|
|
798
|
+
if (parseError.level === ParseErrorLevel2.ERROR) {
|
|
799
|
+
diagnostics2.error(parseError.toString());
|
|
800
|
+
} else {
|
|
801
|
+
diagnostics2.warn(parseError.toString());
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
function addErrorsToBundle(bundle, errors) {
|
|
805
|
+
for (const error of errors) {
|
|
806
|
+
addParseError(bundle.diagnostics, error);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/message_serializer.mjs
|
|
811
|
+
var MessageSerializer = class extends BaseVisitor {
|
|
812
|
+
constructor(renderer, config) {
|
|
813
|
+
super();
|
|
814
|
+
this.renderer = renderer;
|
|
815
|
+
this.config = config;
|
|
816
|
+
}
|
|
817
|
+
serialize(nodes) {
|
|
818
|
+
this.renderer.startRender();
|
|
819
|
+
visitAll(this, nodes);
|
|
820
|
+
this.renderer.endRender();
|
|
821
|
+
return this.renderer.message;
|
|
822
|
+
}
|
|
823
|
+
visitElement(element) {
|
|
824
|
+
if (this.config.placeholder && element.name === this.config.placeholder.elementName) {
|
|
825
|
+
const name = getAttrOrThrow(element, this.config.placeholder.nameAttribute);
|
|
826
|
+
const body = this.config.placeholder.bodyAttribute && getAttribute(element, this.config.placeholder.bodyAttribute);
|
|
827
|
+
this.visitPlaceholder(name, body);
|
|
828
|
+
} else if (this.config.placeholderContainer && element.name === this.config.placeholderContainer.elementName) {
|
|
829
|
+
const start = getAttrOrThrow(element, this.config.placeholderContainer.startAttribute);
|
|
830
|
+
const end = getAttrOrThrow(element, this.config.placeholderContainer.endAttribute);
|
|
831
|
+
this.visitPlaceholderContainer(start, element.children, end);
|
|
832
|
+
} else if (this.config.inlineElements.indexOf(element.name) !== -1) {
|
|
833
|
+
visitAll(this, element.children);
|
|
834
|
+
} else {
|
|
835
|
+
throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
visitText(text) {
|
|
839
|
+
this.renderer.text(text.value);
|
|
840
|
+
}
|
|
841
|
+
visitExpansion(expansion) {
|
|
842
|
+
this.renderer.startIcu();
|
|
843
|
+
this.renderer.text(`${expansion.switchValue}, ${expansion.type},`);
|
|
844
|
+
visitAll(this, expansion.cases);
|
|
845
|
+
this.renderer.endIcu();
|
|
846
|
+
}
|
|
847
|
+
visitExpansionCase(expansionCase) {
|
|
848
|
+
this.renderer.text(` ${expansionCase.value} {`);
|
|
849
|
+
this.renderer.startContainer();
|
|
850
|
+
visitAll(this, expansionCase.expression);
|
|
851
|
+
this.renderer.closeContainer();
|
|
852
|
+
this.renderer.text(`}`);
|
|
853
|
+
}
|
|
854
|
+
visitContainedNodes(nodes) {
|
|
855
|
+
this.renderer.startContainer();
|
|
856
|
+
visitAll(this, nodes);
|
|
857
|
+
this.renderer.closeContainer();
|
|
858
|
+
}
|
|
859
|
+
visitPlaceholder(name, body) {
|
|
860
|
+
this.renderer.placeholder(name, body);
|
|
861
|
+
}
|
|
862
|
+
visitPlaceholderContainer(startName, children, closeName) {
|
|
863
|
+
this.renderer.startPlaceholder(startName);
|
|
864
|
+
this.visitContainedNodes(children);
|
|
865
|
+
this.renderer.closePlaceholder(closeName);
|
|
866
|
+
}
|
|
867
|
+
isPlaceholderContainer(node) {
|
|
868
|
+
return node instanceof Element2 && node.name === this.config.placeholderContainer.elementName;
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/message_serialization/target_message_renderer.mjs
|
|
873
|
+
import { \u0275makeParsedTranslation } from "@angular/localize";
|
|
874
|
+
var TargetMessageRenderer = class {
|
|
875
|
+
constructor() {
|
|
876
|
+
this.current = { messageParts: [], placeholderNames: [], text: "" };
|
|
877
|
+
this.icuDepth = 0;
|
|
878
|
+
}
|
|
879
|
+
get message() {
|
|
880
|
+
const { messageParts, placeholderNames } = this.current;
|
|
881
|
+
return \u0275makeParsedTranslation(messageParts, placeholderNames);
|
|
882
|
+
}
|
|
883
|
+
startRender() {
|
|
884
|
+
}
|
|
885
|
+
endRender() {
|
|
886
|
+
this.storeMessagePart();
|
|
887
|
+
}
|
|
888
|
+
text(text) {
|
|
889
|
+
this.current.text += text;
|
|
890
|
+
}
|
|
891
|
+
placeholder(name, body) {
|
|
892
|
+
this.renderPlaceholder(name);
|
|
893
|
+
}
|
|
894
|
+
startPlaceholder(name) {
|
|
895
|
+
this.renderPlaceholder(name);
|
|
896
|
+
}
|
|
897
|
+
closePlaceholder(name) {
|
|
898
|
+
this.renderPlaceholder(name);
|
|
899
|
+
}
|
|
900
|
+
startContainer() {
|
|
901
|
+
}
|
|
902
|
+
closeContainer() {
|
|
903
|
+
}
|
|
904
|
+
startIcu() {
|
|
905
|
+
this.icuDepth++;
|
|
906
|
+
this.text("{");
|
|
907
|
+
}
|
|
908
|
+
endIcu() {
|
|
909
|
+
this.icuDepth--;
|
|
910
|
+
this.text("}");
|
|
911
|
+
}
|
|
912
|
+
normalizePlaceholderName(name) {
|
|
913
|
+
return name.replace(/-/g, "_");
|
|
914
|
+
}
|
|
915
|
+
renderPlaceholder(name) {
|
|
916
|
+
name = this.normalizePlaceholderName(name);
|
|
917
|
+
if (this.icuDepth > 0) {
|
|
918
|
+
this.text(`{${name}}`);
|
|
919
|
+
} else {
|
|
920
|
+
this.storeMessagePart();
|
|
921
|
+
this.current.placeholderNames.push(name);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
storeMessagePart() {
|
|
925
|
+
this.current.messageParts.push(this.current.text);
|
|
926
|
+
this.current.text = "";
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/serialize_translation_message.mjs
|
|
931
|
+
function serializeTranslationMessage(element, config) {
|
|
932
|
+
const { rootNodes, errors: parseErrors } = parseInnerRange(element);
|
|
933
|
+
try {
|
|
934
|
+
const serializer = new MessageSerializer(new TargetMessageRenderer(), config);
|
|
935
|
+
const translation = serializer.serialize(rootNodes);
|
|
936
|
+
return { translation, parseErrors, serializeErrors: [] };
|
|
937
|
+
} catch (e) {
|
|
938
|
+
return { translation: null, parseErrors, serializeErrors: [e] };
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.mjs
|
|
943
|
+
var Xliff1TranslationParser = class {
|
|
944
|
+
canParse(filePath, contents) {
|
|
945
|
+
const result = this.analyze(filePath, contents);
|
|
946
|
+
return result.canParse && result.hint;
|
|
947
|
+
}
|
|
948
|
+
analyze(filePath, contents) {
|
|
949
|
+
return canParseXml(filePath, contents, "xliff", { version: "1.2" });
|
|
950
|
+
}
|
|
951
|
+
parse(filePath, contents, hint) {
|
|
952
|
+
if (hint) {
|
|
953
|
+
return this.extractBundle(hint);
|
|
954
|
+
} else {
|
|
955
|
+
return this.extractBundleDeprecated(filePath, contents);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
extractBundle({ element, errors }) {
|
|
959
|
+
const diagnostics2 = new Diagnostics();
|
|
960
|
+
errors.forEach((e) => addParseError(diagnostics2, e));
|
|
961
|
+
if (element.children.length === 0) {
|
|
962
|
+
addParseDiagnostic(diagnostics2, element.sourceSpan, "Missing expected <file> element", ParseErrorLevel3.WARNING);
|
|
963
|
+
return { locale: void 0, translations: {}, diagnostics: diagnostics2 };
|
|
964
|
+
}
|
|
965
|
+
const files = element.children.filter(isNamedElement("file"));
|
|
966
|
+
if (files.length === 0) {
|
|
967
|
+
addParseDiagnostic(diagnostics2, element.sourceSpan, "No <file> elements found in <xliff>", ParseErrorLevel3.WARNING);
|
|
968
|
+
} else if (files.length > 1) {
|
|
969
|
+
addParseDiagnostic(diagnostics2, files[1].sourceSpan, "More than one <file> element found in <xliff>", ParseErrorLevel3.WARNING);
|
|
970
|
+
}
|
|
971
|
+
const bundle = { locale: void 0, translations: {}, diagnostics: diagnostics2 };
|
|
972
|
+
const translationVisitor = new XliffTranslationVisitor();
|
|
973
|
+
const localesFound = new Set();
|
|
974
|
+
for (const file of files) {
|
|
975
|
+
const locale = getAttribute(file, "target-language");
|
|
976
|
+
if (locale !== void 0) {
|
|
977
|
+
localesFound.add(locale);
|
|
978
|
+
bundle.locale = locale;
|
|
979
|
+
}
|
|
980
|
+
visitAll2(translationVisitor, file.children, bundle);
|
|
981
|
+
}
|
|
982
|
+
if (localesFound.size > 1) {
|
|
983
|
+
addParseDiagnostic(diagnostics2, element.sourceSpan, `More than one locale found in translation file: ${JSON.stringify(Array.from(localesFound))}. Using "${bundle.locale}"`, ParseErrorLevel3.WARNING);
|
|
984
|
+
}
|
|
985
|
+
return bundle;
|
|
986
|
+
}
|
|
987
|
+
extractBundleDeprecated(filePath, contents) {
|
|
988
|
+
const hint = this.canParse(filePath, contents);
|
|
989
|
+
if (!hint) {
|
|
990
|
+
throw new Error(`Unable to parse "${filePath}" as XLIFF 1.2 format.`);
|
|
991
|
+
}
|
|
992
|
+
const bundle = this.extractBundle(hint);
|
|
993
|
+
if (bundle.diagnostics.hasErrors) {
|
|
994
|
+
const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 1.2 format`);
|
|
995
|
+
throw new Error(message);
|
|
996
|
+
}
|
|
997
|
+
return bundle;
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
var XliffTranslationVisitor = class extends BaseVisitor {
|
|
1001
|
+
visitElement(element, bundle) {
|
|
1002
|
+
if (element.name === "trans-unit") {
|
|
1003
|
+
this.visitTransUnitElement(element, bundle);
|
|
1004
|
+
} else {
|
|
1005
|
+
visitAll2(this, element.children, bundle);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
visitTransUnitElement(element, bundle) {
|
|
1009
|
+
const id = getAttribute(element, "id");
|
|
1010
|
+
if (id === void 0) {
|
|
1011
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <trans-unit> element.`, ParseErrorLevel3.ERROR);
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
if (bundle.translations[id] !== void 0) {
|
|
1015
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${id}"`, ParseErrorLevel3.ERROR);
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
let targetMessage = element.children.find(isNamedElement("target"));
|
|
1019
|
+
if (targetMessage === void 0) {
|
|
1020
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing <target> element", ParseErrorLevel3.WARNING);
|
|
1021
|
+
targetMessage = element.children.find(isNamedElement("source"));
|
|
1022
|
+
if (targetMessage === void 0) {
|
|
1023
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing required element: one of <target> or <source> is required", ParseErrorLevel3.ERROR);
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(targetMessage, {
|
|
1028
|
+
inlineElements: ["g", "bx", "ex", "bpt", "ept", "ph", "it", "mrk"],
|
|
1029
|
+
placeholder: { elementName: "x", nameAttribute: "id" }
|
|
1030
|
+
});
|
|
1031
|
+
if (translation !== null) {
|
|
1032
|
+
bundle.translations[id] = translation;
|
|
1033
|
+
}
|
|
1034
|
+
addErrorsToBundle(bundle, parseErrors);
|
|
1035
|
+
addErrorsToBundle(bundle, serializeErrors);
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser.mjs
|
|
1040
|
+
import { Element as Element3, ParseErrorLevel as ParseErrorLevel4, visitAll as visitAll3 } from "@angular/compiler";
|
|
1041
|
+
var Xliff2TranslationParser = class {
|
|
1042
|
+
canParse(filePath, contents) {
|
|
1043
|
+
const result = this.analyze(filePath, contents);
|
|
1044
|
+
return result.canParse && result.hint;
|
|
1045
|
+
}
|
|
1046
|
+
analyze(filePath, contents) {
|
|
1047
|
+
return canParseXml(filePath, contents, "xliff", { version: "2.0" });
|
|
1048
|
+
}
|
|
1049
|
+
parse(filePath, contents, hint) {
|
|
1050
|
+
if (hint) {
|
|
1051
|
+
return this.extractBundle(hint);
|
|
1052
|
+
} else {
|
|
1053
|
+
return this.extractBundleDeprecated(filePath, contents);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
extractBundle({ element, errors }) {
|
|
1057
|
+
const diagnostics2 = new Diagnostics();
|
|
1058
|
+
errors.forEach((e) => addParseError(diagnostics2, e));
|
|
1059
|
+
const locale = getAttribute(element, "trgLang");
|
|
1060
|
+
const files = element.children.filter(isFileElement);
|
|
1061
|
+
if (files.length === 0) {
|
|
1062
|
+
addParseDiagnostic(diagnostics2, element.sourceSpan, "No <file> elements found in <xliff>", ParseErrorLevel4.WARNING);
|
|
1063
|
+
} else if (files.length > 1) {
|
|
1064
|
+
addParseDiagnostic(diagnostics2, files[1].sourceSpan, "More than one <file> element found in <xliff>", ParseErrorLevel4.WARNING);
|
|
1065
|
+
}
|
|
1066
|
+
const bundle = { locale, translations: {}, diagnostics: diagnostics2 };
|
|
1067
|
+
const translationVisitor = new Xliff2TranslationVisitor();
|
|
1068
|
+
for (const file of files) {
|
|
1069
|
+
visitAll3(translationVisitor, file.children, { bundle });
|
|
1070
|
+
}
|
|
1071
|
+
return bundle;
|
|
1072
|
+
}
|
|
1073
|
+
extractBundleDeprecated(filePath, contents) {
|
|
1074
|
+
const hint = this.canParse(filePath, contents);
|
|
1075
|
+
if (!hint) {
|
|
1076
|
+
throw new Error(`Unable to parse "${filePath}" as XLIFF 2.0 format.`);
|
|
1077
|
+
}
|
|
1078
|
+
const bundle = this.extractBundle(hint);
|
|
1079
|
+
if (bundle.diagnostics.hasErrors) {
|
|
1080
|
+
const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 2.0 format`);
|
|
1081
|
+
throw new Error(message);
|
|
1082
|
+
}
|
|
1083
|
+
return bundle;
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
var Xliff2TranslationVisitor = class extends BaseVisitor {
|
|
1087
|
+
visitElement(element, { bundle, unit }) {
|
|
1088
|
+
if (element.name === "unit") {
|
|
1089
|
+
this.visitUnitElement(element, bundle);
|
|
1090
|
+
} else if (element.name === "segment") {
|
|
1091
|
+
this.visitSegmentElement(element, bundle, unit);
|
|
1092
|
+
} else {
|
|
1093
|
+
visitAll3(this, element.children, { bundle, unit });
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
visitUnitElement(element, bundle) {
|
|
1097
|
+
const externalId = getAttribute(element, "id");
|
|
1098
|
+
if (externalId === void 0) {
|
|
1099
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <trans-unit> element.`, ParseErrorLevel4.ERROR);
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
if (bundle.translations[externalId] !== void 0) {
|
|
1103
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${externalId}"`, ParseErrorLevel4.ERROR);
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
visitAll3(this, element.children, { bundle, unit: externalId });
|
|
1107
|
+
}
|
|
1108
|
+
visitSegmentElement(element, bundle, unit) {
|
|
1109
|
+
if (unit === void 0) {
|
|
1110
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Invalid <segment> element: should be a child of a <unit> element.", ParseErrorLevel4.ERROR);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
let targetMessage = element.children.find(isNamedElement("target"));
|
|
1114
|
+
if (targetMessage === void 0) {
|
|
1115
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing <target> element", ParseErrorLevel4.WARNING);
|
|
1116
|
+
targetMessage = element.children.find(isNamedElement("source"));
|
|
1117
|
+
if (targetMessage === void 0) {
|
|
1118
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, "Missing required element: one of <target> or <source> is required", ParseErrorLevel4.ERROR);
|
|
1119
|
+
return;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(targetMessage, {
|
|
1123
|
+
inlineElements: ["cp", "sc", "ec", "mrk", "sm", "em"],
|
|
1124
|
+
placeholder: { elementName: "ph", nameAttribute: "equiv", bodyAttribute: "disp" },
|
|
1125
|
+
placeholderContainer: { elementName: "pc", startAttribute: "equivStart", endAttribute: "equivEnd" }
|
|
1126
|
+
});
|
|
1127
|
+
if (translation !== null) {
|
|
1128
|
+
bundle.translations[unit] = translation;
|
|
1129
|
+
}
|
|
1130
|
+
addErrorsToBundle(bundle, parseErrors);
|
|
1131
|
+
addErrorsToBundle(bundle, serializeErrors);
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
function isFileElement(node) {
|
|
1135
|
+
return node instanceof Element3 && node.name === "file";
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser.mjs
|
|
1139
|
+
import { ParseErrorLevel as ParseErrorLevel5, visitAll as visitAll4 } from "@angular/compiler";
|
|
1140
|
+
import { extname as extname2 } from "path";
|
|
1141
|
+
var XtbTranslationParser = class {
|
|
1142
|
+
canParse(filePath, contents) {
|
|
1143
|
+
const result = this.analyze(filePath, contents);
|
|
1144
|
+
return result.canParse && result.hint;
|
|
1145
|
+
}
|
|
1146
|
+
analyze(filePath, contents) {
|
|
1147
|
+
const extension = extname2(filePath);
|
|
1148
|
+
if (extension !== ".xtb" && extension !== ".xmb") {
|
|
1149
|
+
const diagnostics2 = new Diagnostics();
|
|
1150
|
+
diagnostics2.warn("Must have xtb or xmb extension.");
|
|
1151
|
+
return { canParse: false, diagnostics: diagnostics2 };
|
|
1152
|
+
}
|
|
1153
|
+
return canParseXml(filePath, contents, "translationbundle", {});
|
|
1154
|
+
}
|
|
1155
|
+
parse(filePath, contents, hint) {
|
|
1156
|
+
if (hint) {
|
|
1157
|
+
return this.extractBundle(hint);
|
|
1158
|
+
} else {
|
|
1159
|
+
return this.extractBundleDeprecated(filePath, contents);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
extractBundle({ element, errors }) {
|
|
1163
|
+
const langAttr = element.attrs.find((attr) => attr.name === "lang");
|
|
1164
|
+
const bundle = {
|
|
1165
|
+
locale: langAttr && langAttr.value,
|
|
1166
|
+
translations: {},
|
|
1167
|
+
diagnostics: new Diagnostics()
|
|
1168
|
+
};
|
|
1169
|
+
errors.forEach((e) => addParseError(bundle.diagnostics, e));
|
|
1170
|
+
const bundleVisitor = new XtbVisitor();
|
|
1171
|
+
visitAll4(bundleVisitor, element.children, bundle);
|
|
1172
|
+
return bundle;
|
|
1173
|
+
}
|
|
1174
|
+
extractBundleDeprecated(filePath, contents) {
|
|
1175
|
+
const hint = this.canParse(filePath, contents);
|
|
1176
|
+
if (!hint) {
|
|
1177
|
+
throw new Error(`Unable to parse "${filePath}" as XMB/XTB format.`);
|
|
1178
|
+
}
|
|
1179
|
+
const bundle = this.extractBundle(hint);
|
|
1180
|
+
if (bundle.diagnostics.hasErrors) {
|
|
1181
|
+
const message = bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XMB/XTB format`);
|
|
1182
|
+
throw new Error(message);
|
|
1183
|
+
}
|
|
1184
|
+
return bundle;
|
|
1185
|
+
}
|
|
1186
|
+
};
|
|
1187
|
+
var XtbVisitor = class extends BaseVisitor {
|
|
1188
|
+
visitElement(element, bundle) {
|
|
1189
|
+
switch (element.name) {
|
|
1190
|
+
case "translation":
|
|
1191
|
+
const id = getAttribute(element, "id");
|
|
1192
|
+
if (id === void 0) {
|
|
1193
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Missing required "id" attribute on <translation> element.`, ParseErrorLevel5.ERROR);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
if (bundle.translations[id] !== void 0) {
|
|
1197
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Duplicated translations for message "${id}"`, ParseErrorLevel5.ERROR);
|
|
1198
|
+
return;
|
|
1199
|
+
}
|
|
1200
|
+
const { translation, parseErrors, serializeErrors } = serializeTranslationMessage(element, { inlineElements: [], placeholder: { elementName: "ph", nameAttribute: "name" } });
|
|
1201
|
+
if (parseErrors.length) {
|
|
1202
|
+
bundle.diagnostics.warn(computeParseWarning(id, parseErrors));
|
|
1203
|
+
} else if (translation !== null) {
|
|
1204
|
+
bundle.translations[id] = translation;
|
|
1205
|
+
}
|
|
1206
|
+
addErrorsToBundle(bundle, serializeErrors);
|
|
1207
|
+
break;
|
|
1208
|
+
default:
|
|
1209
|
+
addParseDiagnostic(bundle.diagnostics, element.sourceSpan, `Unexpected <${element.name}> tag.`, ParseErrorLevel5.ERROR);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
function computeParseWarning(id, errors) {
|
|
1214
|
+
const msg = errors.map((e) => e.toString()).join("\n");
|
|
1215
|
+
return `Could not parse message with id "${id}" - perhaps it has an unrecognised ICU format?
|
|
1216
|
+
` + msg;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/translator.mjs
|
|
1220
|
+
var Translator = class {
|
|
1221
|
+
constructor(fs2, resourceHandlers, diagnostics2) {
|
|
1222
|
+
this.fs = fs2;
|
|
1223
|
+
this.resourceHandlers = resourceHandlers;
|
|
1224
|
+
this.diagnostics = diagnostics2;
|
|
1225
|
+
}
|
|
1226
|
+
translateFiles(inputPaths, rootPath, outputPathFn2, translations, sourceLocale2) {
|
|
1227
|
+
inputPaths.forEach((inputPath) => {
|
|
1228
|
+
const absInputPath = this.fs.resolve(rootPath, inputPath);
|
|
1229
|
+
const contents = this.fs.readFileBuffer(absInputPath);
|
|
1230
|
+
const relativePath = this.fs.relative(rootPath, absInputPath);
|
|
1231
|
+
for (const resourceHandler of this.resourceHandlers) {
|
|
1232
|
+
if (resourceHandler.canTranslate(relativePath, contents)) {
|
|
1233
|
+
return resourceHandler.translate(this.diagnostics, rootPath, relativePath, contents, outputPathFn2, translations, sourceLocale2);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
this.diagnostics.error(`Unable to handle resource file: ${inputPath}`);
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
|
|
1241
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/index.mjs
|
|
1242
|
+
function translateFiles({ sourceRootPath: sourceRootPath2, sourceFilePaths: sourceFilePaths2, translationFilePaths: translationFilePaths2, translationFileLocales: translationFileLocales2, outputPathFn: outputPathFn2, diagnostics: diagnostics2, missingTranslation: missingTranslation2, duplicateTranslation: duplicateTranslation2, sourceLocale: sourceLocale2 }) {
|
|
1243
|
+
const fs2 = getFileSystem4();
|
|
1244
|
+
const translationLoader = new TranslationLoader(fs2, [
|
|
1245
|
+
new Xliff2TranslationParser(),
|
|
1246
|
+
new Xliff1TranslationParser(),
|
|
1247
|
+
new XtbTranslationParser(),
|
|
1248
|
+
new SimpleJsonTranslationParser(),
|
|
1249
|
+
new ArbTranslationParser()
|
|
1250
|
+
], duplicateTranslation2, diagnostics2);
|
|
1251
|
+
const resourceProcessor = new Translator(fs2, [
|
|
1252
|
+
new SourceFileTranslationHandler(fs2, { missingTranslation: missingTranslation2 }),
|
|
1253
|
+
new AssetTranslationHandler(fs2)
|
|
1254
|
+
], diagnostics2);
|
|
1255
|
+
const translationFilePathsArrays = translationFilePaths2.map((filePaths) => Array.isArray(filePaths) ? filePaths.map((p) => fs2.resolve(p)) : [fs2.resolve(filePaths)]);
|
|
1256
|
+
const translations = translationLoader.loadBundles(translationFilePathsArrays, translationFileLocales2);
|
|
1257
|
+
sourceRootPath2 = fs2.resolve(sourceRootPath2);
|
|
1258
|
+
resourceProcessor.translateFiles(sourceFilePaths2.map(relativeFrom), fs2.resolve(sourceRootPath2), outputPathFn2, translations, sourceLocale2);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
// bazel-out/darwin-fastbuild/bin/packages/localize/tools/src/translate/cli.mjs
|
|
1262
|
+
process.title = "Angular Localization Message Translator (localize-translate)";
|
|
1263
|
+
var args = process.argv.slice(2);
|
|
1264
|
+
var options = yargs(args).option("r", {
|
|
1265
|
+
alias: "root",
|
|
1266
|
+
required: true,
|
|
1267
|
+
describe: "The root path of the files to translate, either absolute or relative to the current working directory. E.g. `dist/en`.",
|
|
1268
|
+
type: "string"
|
|
1269
|
+
}).option("s", {
|
|
1270
|
+
alias: "source",
|
|
1271
|
+
required: true,
|
|
1272
|
+
describe: "A glob pattern indicating what files to translate, relative to the `root` path. E.g. `bundles/**/*`.",
|
|
1273
|
+
type: "string"
|
|
1274
|
+
}).option("l", {
|
|
1275
|
+
alias: "source-locale",
|
|
1276
|
+
describe: "The source locale of the application. If this is provided then a copy of the application will be created with no translation but just the `$localize` calls stripped out.",
|
|
1277
|
+
type: "string"
|
|
1278
|
+
}).option("t", {
|
|
1279
|
+
alias: "translations",
|
|
1280
|
+
required: true,
|
|
1281
|
+
array: true,
|
|
1282
|
+
describe: 'A list of paths to the translation files to load, either absolute or relative to the current working directory.\nE.g. `-t src/locale/messages.en.xlf src/locale/messages.fr.xlf src/locale/messages.de.xlf`.\nIf you want to merge multiple translation files for each locale, then provide the list of files in an array.\nNote that the arrays must be in double quotes if you include any whitespace within the array.\nE.g. `-t "[src/locale/messages.en.xlf, src/locale/messages-2.en.xlf]" [src/locale/messages.fr.xlf,src/locale/messages-2.fr.xlf]`',
|
|
1283
|
+
type: "string"
|
|
1284
|
+
}).option("target-locales", {
|
|
1285
|
+
array: true,
|
|
1286
|
+
describe: 'A list of target locales for the translation files, which will override any target locale parsed from the translation file.\nE.g. "-t en fr de".',
|
|
1287
|
+
type: "string"
|
|
1288
|
+
}).option("o", {
|
|
1289
|
+
alias: "outputPath",
|
|
1290
|
+
required: true,
|
|
1291
|
+
describe: "A output path pattern to where the translated files will be written.\nThe path must be either absolute or relative to the current working directory.\nThe marker `{{LOCALE}}` will be replaced with the target locale. E.g. `dist/{{LOCALE}}`.",
|
|
1292
|
+
type: "string"
|
|
1293
|
+
}).option("m", {
|
|
1294
|
+
alias: "missingTranslation",
|
|
1295
|
+
describe: "How to handle missing translations.",
|
|
1296
|
+
choices: ["error", "warning", "ignore"],
|
|
1297
|
+
default: "warning",
|
|
1298
|
+
type: "string"
|
|
1299
|
+
}).option("d", {
|
|
1300
|
+
alias: "duplicateTranslation",
|
|
1301
|
+
describe: "How to handle duplicate translations.",
|
|
1302
|
+
choices: ["error", "warning", "ignore"],
|
|
1303
|
+
default: "warning",
|
|
1304
|
+
type: "string"
|
|
1305
|
+
}).strict().help().parseSync();
|
|
1306
|
+
var fs = new NodeJSFileSystem();
|
|
1307
|
+
setFileSystem(fs);
|
|
1308
|
+
var sourceRootPath = options.r;
|
|
1309
|
+
var sourceFilePaths = glob.sync(options.s, { cwd: sourceRootPath, nodir: true });
|
|
1310
|
+
var translationFilePaths = convertArraysFromArgs(options.t);
|
|
1311
|
+
var outputPathFn = getOutputPathFn(fs, fs.resolve(options.o));
|
|
1312
|
+
var diagnostics = new Diagnostics();
|
|
1313
|
+
var missingTranslation = options.m;
|
|
1314
|
+
var duplicateTranslation = options.d;
|
|
1315
|
+
var sourceLocale = options.l;
|
|
1316
|
+
var translationFileLocales = options["target-locales"] || [];
|
|
1317
|
+
translateFiles({
|
|
1318
|
+
sourceRootPath,
|
|
1319
|
+
sourceFilePaths,
|
|
1320
|
+
translationFilePaths,
|
|
1321
|
+
translationFileLocales,
|
|
1322
|
+
outputPathFn,
|
|
1323
|
+
diagnostics,
|
|
1324
|
+
missingTranslation,
|
|
1325
|
+
duplicateTranslation,
|
|
1326
|
+
sourceLocale
|
|
1327
|
+
});
|
|
1328
|
+
diagnostics.messages.forEach((m) => console.warn(`${m.type}: ${m.message}`));
|
|
1329
|
+
process.exit(diagnostics.hasErrors ? 1 : 0);
|
|
1330
|
+
function convertArraysFromArgs(args2) {
|
|
1331
|
+
return args2.map((arg) => arg.startsWith("[") && arg.endsWith("]") ? arg.slice(1, -1).split(",").map((arg2) => arg2.trim()) : arg);
|
|
1332
|
+
}
|
|
1333
|
+
/**
|
|
1334
|
+
* @license
|
|
1335
|
+
* Copyright Google LLC All Rights Reserved.
|
|
1336
|
+
*
|
|
1337
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
1338
|
+
* found in the LICENSE file at https://angular.io/license
|
|
1339
|
+
*/
|
|
1340
|
+
//# sourceMappingURL=cli.js.map
|