@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,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Angular v13.0.0-rc.3
|
|
3
|
+
* (c) 2010-2021 Google LLC. https://angular.io/
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @license
|
|
9
|
+
* Copyright Google LLC All Rights Reserved.
|
|
10
|
+
*
|
|
11
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
12
|
+
* found in the LICENSE file at https://angular.io/license
|
|
13
|
+
*/
|
|
14
|
+
const __globalThis = typeof globalThis !== 'undefined' && globalThis;
|
|
15
|
+
const __window = typeof window !== 'undefined' && window;
|
|
16
|
+
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
|
|
17
|
+
self instanceof WorkerGlobalScope && self;
|
|
18
|
+
const __global = typeof global !== 'undefined' && global;
|
|
19
|
+
// Always use __globalThis if available; this is the spec-defined global variable across all
|
|
20
|
+
// environments.
|
|
21
|
+
// Then fallback to __global first; in Node tests both __global and __window may be defined.
|
|
22
|
+
const _global = __globalThis || __global || __window || __self;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @license
|
|
26
|
+
* Copyright Google LLC All Rights Reserved.
|
|
27
|
+
*
|
|
28
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
29
|
+
* found in the LICENSE file at https://angular.io/license
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Tag a template literal string for localization.
|
|
33
|
+
*
|
|
34
|
+
* For example:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* $localize `some string to localize`
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* **Providing meaning, description and id**
|
|
41
|
+
*
|
|
42
|
+
* You can optionally specify one or more of `meaning`, `description` and `id` for a localized
|
|
43
|
+
* string by pre-pending it with a colon delimited block of the form:
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* $localize`:meaning|description@@id:source message text`;
|
|
47
|
+
*
|
|
48
|
+
* $localize`:meaning|:source message text`;
|
|
49
|
+
* $localize`:description:source message text`;
|
|
50
|
+
* $localize`:@@id:source message text`;
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* This format is the same as that used for `i18n` markers in Angular templates. See the
|
|
54
|
+
* [Angular i18n guide](guide/i18n-common-prepare#mark-text-in-component-template).
|
|
55
|
+
*
|
|
56
|
+
* **Naming placeholders**
|
|
57
|
+
*
|
|
58
|
+
* If the template literal string contains expressions, then the expressions will be automatically
|
|
59
|
+
* associated with placeholder names for you.
|
|
60
|
+
*
|
|
61
|
+
* For example:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* $localize `Hi ${name}! There are ${items.length} items.`;
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* will generate a message-source of `Hi {$PH}! There are {$PH_1} items`.
|
|
68
|
+
*
|
|
69
|
+
* The recommended practice is to name the placeholder associated with each expression though.
|
|
70
|
+
*
|
|
71
|
+
* Do this by providing the placeholder name wrapped in `:` characters directly after the
|
|
72
|
+
* expression. These placeholder names are stripped out of the rendered localized string.
|
|
73
|
+
*
|
|
74
|
+
* For example, to name the `items.length` expression placeholder `itemCount` you write:
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* $localize `There are ${items.length}:itemCount: items`;
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* **Escaping colon markers**
|
|
81
|
+
*
|
|
82
|
+
* If you need to use a `:` character directly at the start of a tagged string that has no
|
|
83
|
+
* metadata block, or directly after a substitution expression that has no name you must escape
|
|
84
|
+
* the `:` by preceding it with a backslash:
|
|
85
|
+
*
|
|
86
|
+
* For example:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* // message has a metadata block so no need to escape colon
|
|
90
|
+
* $localize `:some description::this message starts with a colon (:)`;
|
|
91
|
+
* // no metadata block so the colon must be escaped
|
|
92
|
+
* $localize `\:this message starts with a colon (:)`;
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
* // named substitution so no need to escape colon
|
|
97
|
+
* $localize `${label}:label:: ${}`
|
|
98
|
+
* // anonymous substitution so colon must be escaped
|
|
99
|
+
* $localize `${label}\: ${}`
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* **Processing localized strings:**
|
|
103
|
+
*
|
|
104
|
+
* There are three scenarios:
|
|
105
|
+
*
|
|
106
|
+
* * **compile-time inlining**: the `$localize` tag is transformed at compile time by a
|
|
107
|
+
* transpiler, removing the tag and replacing the template literal string with a translated
|
|
108
|
+
* literal string from a collection of translations provided to the transpilation tool.
|
|
109
|
+
*
|
|
110
|
+
* * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and
|
|
111
|
+
* reorders the parts (static strings and expressions) of the template literal string with strings
|
|
112
|
+
* from a collection of translations loaded at run-time.
|
|
113
|
+
*
|
|
114
|
+
* * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates
|
|
115
|
+
* the original template literal string without applying any translations to the parts. This
|
|
116
|
+
* version is used during development or where there is no need to translate the localized
|
|
117
|
+
* template literals.
|
|
118
|
+
*
|
|
119
|
+
* @param messageParts a collection of the static parts of the template string.
|
|
120
|
+
* @param expressions a collection of the values of each placeholder in the template string.
|
|
121
|
+
* @returns the translated string, with the `messageParts` and `expressions` interleaved together.
|
|
122
|
+
*
|
|
123
|
+
* @globalApi
|
|
124
|
+
* @publicApi
|
|
125
|
+
*/
|
|
126
|
+
const $localize = function (messageParts, ...expressions) {
|
|
127
|
+
if ($localize.translate) {
|
|
128
|
+
// Don't use array expansion here to avoid the compiler adding `__read()` helper unnecessarily.
|
|
129
|
+
const translation = $localize.translate(messageParts, expressions);
|
|
130
|
+
messageParts = translation[0];
|
|
131
|
+
expressions = translation[1];
|
|
132
|
+
}
|
|
133
|
+
let message = stripBlock(messageParts[0], messageParts.raw[0]);
|
|
134
|
+
for (let i = 1; i < messageParts.length; i++) {
|
|
135
|
+
message += expressions[i - 1] + stripBlock(messageParts[i], messageParts.raw[i]);
|
|
136
|
+
}
|
|
137
|
+
return message;
|
|
138
|
+
};
|
|
139
|
+
const BLOCK_MARKER = ':';
|
|
140
|
+
/**
|
|
141
|
+
* Strip a delimited "block" from the start of the `messagePart`, if it is found.
|
|
142
|
+
*
|
|
143
|
+
* If a marker character (:) actually appears in the content at the start of a tagged string or
|
|
144
|
+
* after a substitution expression, where a block has not been provided the character must be
|
|
145
|
+
* escaped with a backslash, `\:`. This function checks for this by looking at the `raw`
|
|
146
|
+
* messagePart, which should still contain the backslash.
|
|
147
|
+
*
|
|
148
|
+
* @param messagePart The cooked message part to process.
|
|
149
|
+
* @param rawMessagePart The raw message part to check.
|
|
150
|
+
* @returns the message part with the placeholder name stripped, if found.
|
|
151
|
+
* @throws an error if the block is unterminated
|
|
152
|
+
*/
|
|
153
|
+
function stripBlock(messagePart, rawMessagePart) {
|
|
154
|
+
return rawMessagePart.charAt(0) === BLOCK_MARKER ?
|
|
155
|
+
messagePart.substring(findEndOfBlock(messagePart, rawMessagePart) + 1) :
|
|
156
|
+
messagePart;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Find the end of a "marked block" indicated by the first non-escaped colon.
|
|
160
|
+
*
|
|
161
|
+
* @param cooked The cooked string (where escaped chars have been processed)
|
|
162
|
+
* @param raw The raw string (where escape sequences are still in place)
|
|
163
|
+
*
|
|
164
|
+
* @returns the index of the end of block marker
|
|
165
|
+
* @throws an error if the block is unterminated
|
|
166
|
+
*/
|
|
167
|
+
function findEndOfBlock(cooked, raw) {
|
|
168
|
+
/***********************************************************************************************
|
|
169
|
+
* This function is repeated in `src/utils/messages.ts` and the two should be kept in sync.
|
|
170
|
+
* The reason is that this file is marked as having side-effects, and if we import `messages.ts`
|
|
171
|
+
* into it, the whole of `src/utils` will be included in this bundle and none of the functions
|
|
172
|
+
* will be tree shaken.
|
|
173
|
+
***********************************************************************************************/
|
|
174
|
+
for (let cookedIndex = 1, rawIndex = 1; cookedIndex < cooked.length; cookedIndex++, rawIndex++) {
|
|
175
|
+
if (raw[rawIndex] === '\\') {
|
|
176
|
+
rawIndex++;
|
|
177
|
+
}
|
|
178
|
+
else if (cooked[cookedIndex] === BLOCK_MARKER) {
|
|
179
|
+
return cookedIndex;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
throw new Error(`Unterminated $localize metadata block in "${raw}".`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @license
|
|
187
|
+
* Copyright Google LLC All Rights Reserved.
|
|
188
|
+
*
|
|
189
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
190
|
+
* found in the LICENSE file at https://angular.io/license
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @license
|
|
195
|
+
* Copyright Google LLC All Rights Reserved.
|
|
196
|
+
*
|
|
197
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
198
|
+
* found in the LICENSE file at https://angular.io/license
|
|
199
|
+
*/
|
|
200
|
+
// Attach $localize to the global context, as a side-effect of this module.
|
|
201
|
+
_global.$localize = $localize;
|
|
202
|
+
|
|
203
|
+
export { $localize };
|
|
204
|
+
//# sourceMappingURL=init.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.mjs","sources":["../../../../../../packages/localize/src/localize/src/global.ts","../../../../../../packages/localize/src/localize/src/localize.ts","../../../../../../packages/localize/src/localize/index.ts","../../../../../../packages/localize/init/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n// **********************************************************************************************\n// This code to access the global object is mostly copied from `packages/core/src/util/global.ts`\n\ndeclare global {\n // The definition of `WorkerGlobalScope` must be compatible with the one in `lib.webworker.d.ts`,\n // because all files under `packages/` are compiled together as part of the\n // [legacy-unit-tests-saucelabs][1] CI job, including the `lib.webworker.d.ts` typings brought in\n // by [service-worker/worker/src/service-worker.d.ts][2].\n //\n // [1]:\n // https://github.com/angular/angular/blob/ffeea63f43e6a7fd46be4a8cd5a5d254c98dea08/.circleci/config.yml#L681\n // [2]:\n // https://github.com/angular/angular/blob/316dc2f12ce8931f5ff66fa5f8da21c0d251a337/packages/service-worker/worker/src/service-worker.d.ts#L9\n interface WorkerGlobalScope extends EventTarget, WindowOrWorkerGlobalScope {}\n\n var WorkerGlobalScope: {prototype: WorkerGlobalScope; new (): WorkerGlobalScope;};\n}\n\nconst __globalThis = typeof globalThis !== 'undefined' && globalThis;\nconst __window = typeof window !== 'undefined' && window;\nconst __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&\n self instanceof WorkerGlobalScope && self;\nconst __global = typeof global !== 'undefined' && global;\n// Always use __globalThis if available; this is the spec-defined global variable across all\n// environments.\n// Then fallback to __global first; in Node tests both __global and __window may be defined.\nexport const _global: any = __globalThis || __global || __window || __self;\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/** @nodoc */\nexport interface LocalizeFn {\n (messageParts: TemplateStringsArray, ...expressions: readonly any[]): string;\n\n /**\n * A function that converts an input \"message with expressions\" into a translated \"message with\n * expressions\".\n *\n * The conversion may be done in place, modifying the array passed to the function, so\n * don't assume that this has no side-effects.\n *\n * The expressions must be passed in since it might be they need to be reordered for\n * different translations.\n */\n translate?: TranslateFn;\n /**\n * The current locale of the translated messages.\n *\n * The compile-time translation inliner is able to replace the following code:\n *\n * ```\n * typeof $localize !== \"undefined\" && $localize.locale\n * ```\n *\n * with a string literal of the current locale. E.g.\n *\n * ```\n * \"fr\"\n * ```\n */\n locale?: string;\n}\n\n/** @nodoc */\nexport interface TranslateFn {\n (messageParts: TemplateStringsArray,\n expressions: readonly any[]): [TemplateStringsArray, readonly any[]];\n}\n\n/**\n * Tag a template literal string for localization.\n *\n * For example:\n *\n * ```ts\n * $localize `some string to localize`\n * ```\n *\n * **Providing meaning, description and id**\n *\n * You can optionally specify one or more of `meaning`, `description` and `id` for a localized\n * string by pre-pending it with a colon delimited block of the form:\n *\n * ```ts\n * $localize`:meaning|description@@id:source message text`;\n *\n * $localize`:meaning|:source message text`;\n * $localize`:description:source message text`;\n * $localize`:@@id:source message text`;\n * ```\n *\n * This format is the same as that used for `i18n` markers in Angular templates. See the\n * [Angular i18n guide](guide/i18n-common-prepare#mark-text-in-component-template).\n *\n * **Naming placeholders**\n *\n * If the template literal string contains expressions, then the expressions will be automatically\n * associated with placeholder names for you.\n *\n * For example:\n *\n * ```ts\n * $localize `Hi ${name}! There are ${items.length} items.`;\n * ```\n *\n * will generate a message-source of `Hi {$PH}! There are {$PH_1} items`.\n *\n * The recommended practice is to name the placeholder associated with each expression though.\n *\n * Do this by providing the placeholder name wrapped in `:` characters directly after the\n * expression. These placeholder names are stripped out of the rendered localized string.\n *\n * For example, to name the `items.length` expression placeholder `itemCount` you write:\n *\n * ```ts\n * $localize `There are ${items.length}:itemCount: items`;\n * ```\n *\n * **Escaping colon markers**\n *\n * If you need to use a `:` character directly at the start of a tagged string that has no\n * metadata block, or directly after a substitution expression that has no name you must escape\n * the `:` by preceding it with a backslash:\n *\n * For example:\n *\n * ```ts\n * // message has a metadata block so no need to escape colon\n * $localize `:some description::this message starts with a colon (:)`;\n * // no metadata block so the colon must be escaped\n * $localize `\\:this message starts with a colon (:)`;\n * ```\n *\n * ```ts\n * // named substitution so no need to escape colon\n * $localize `${label}:label:: ${}`\n * // anonymous substitution so colon must be escaped\n * $localize `${label}\\: ${}`\n * ```\n *\n * **Processing localized strings:**\n *\n * There are three scenarios:\n *\n * * **compile-time inlining**: the `$localize` tag is transformed at compile time by a\n * transpiler, removing the tag and replacing the template literal string with a translated\n * literal string from a collection of translations provided to the transpilation tool.\n *\n * * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and\n * reorders the parts (static strings and expressions) of the template literal string with strings\n * from a collection of translations loaded at run-time.\n *\n * * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates\n * the original template literal string without applying any translations to the parts. This\n * version is used during development or where there is no need to translate the localized\n * template literals.\n *\n * @param messageParts a collection of the static parts of the template string.\n * @param expressions a collection of the values of each placeholder in the template string.\n * @returns the translated string, with the `messageParts` and `expressions` interleaved together.\n *\n * @globalApi\n * @publicApi\n */\nexport const $localize: LocalizeFn = function(\n messageParts: TemplateStringsArray, ...expressions: readonly any[]) {\n if ($localize.translate) {\n // Don't use array expansion here to avoid the compiler adding `__read()` helper unnecessarily.\n const translation = $localize.translate(messageParts, expressions);\n messageParts = translation[0];\n expressions = translation[1];\n }\n let message = stripBlock(messageParts[0], messageParts.raw[0]);\n for (let i = 1; i < messageParts.length; i++) {\n message += expressions[i - 1] + stripBlock(messageParts[i], messageParts.raw[i]);\n }\n return message;\n};\n\nconst BLOCK_MARKER = ':';\n\n/**\n * Strip a delimited \"block\" from the start of the `messagePart`, if it is found.\n *\n * If a marker character (:) actually appears in the content at the start of a tagged string or\n * after a substitution expression, where a block has not been provided the character must be\n * escaped with a backslash, `\\:`. This function checks for this by looking at the `raw`\n * messagePart, which should still contain the backslash.\n *\n * @param messagePart The cooked message part to process.\n * @param rawMessagePart The raw message part to check.\n * @returns the message part with the placeholder name stripped, if found.\n * @throws an error if the block is unterminated\n */\nfunction stripBlock(messagePart: string, rawMessagePart: string) {\n return rawMessagePart.charAt(0) === BLOCK_MARKER ?\n messagePart.substring(findEndOfBlock(messagePart, rawMessagePart) + 1) :\n messagePart;\n}\n\n/**\n * Find the end of a \"marked block\" indicated by the first non-escaped colon.\n *\n * @param cooked The cooked string (where escaped chars have been processed)\n * @param raw The raw string (where escape sequences are still in place)\n *\n * @returns the index of the end of block marker\n * @throws an error if the block is unterminated\n */\nfunction findEndOfBlock(cooked: string, raw: string): number {\n /***********************************************************************************************\n * This function is repeated in `src/utils/messages.ts` and the two should be kept in sync.\n * The reason is that this file is marked as having side-effects, and if we import `messages.ts`\n * into it, the whole of `src/utils` will be included in this bundle and none of the functions\n * will be tree shaken.\n ***********************************************************************************************/\n for (let cookedIndex = 1, rawIndex = 1; cookedIndex < cooked.length; cookedIndex++, rawIndex++) {\n if (raw[rawIndex] === '\\\\') {\n rawIndex++;\n } else if (cooked[cookedIndex] === BLOCK_MARKER) {\n return cookedIndex;\n }\n }\n throw new Error(`Unterminated $localize metadata block in \"${raw}\".`);\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\nexport {_global} from './src/global';\nexport {$localize, LocalizeFn, TranslateFn} from './src/localize';\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\nimport {$localize, _global, LocalizeFn} from '../src/localize';\n\nexport {$localize, LocalizeFn, TranslateFn} from '../src/localize';\n\n// Attach $localize to the global context, as a side-effect of this module.\n_global.$localize = $localize;\n\n// `declare global` allows us to escape the current module and place types on the global namespace\ndeclare global {\n /**\n * Tag a template literal string for localization.\n *\n * For example:\n *\n * ```ts\n * $localize `some string to localize`\n * ```\n *\n * **Providing meaning, description and id**\n *\n * You can optionally specify one or more of `meaning`, `description` and `id` for a localized\n * string by pre-pending it with a colon delimited block of the form:\n *\n * ```ts\n * $localize`:meaning|description@@id:source message text`;\n *\n * $localize`:meaning|:source message text`;\n * $localize`:description:source message text`;\n * $localize`:@@id:source message text`;\n * ```\n *\n * This format is the same as that used for `i18n` markers in Angular templates. See the\n * [Angular 18n guide](guide/i18n-common-prepare#mark-text-in-component-template).\n *\n * **Naming placeholders**\n *\n * If the template literal string contains expressions, then the expressions will be automatically\n * associated with placeholder names for you.\n *\n * For example:\n *\n * ```ts\n * $localize `Hi ${name}! There are ${items.length} items.`;\n * ```\n *\n * will generate a message-source of `Hi {$PH}! There are {$PH_1} items`.\n *\n * The recommended practice is to name the placeholder associated with each expression though.\n *\n * Do this by providing the placeholder name wrapped in `:` characters directly after the\n * expression. These placeholder names are stripped out of the rendered localized string.\n *\n * For example, to name the `items.length` expression placeholder `itemCount` you write:\n *\n * ```ts\n * $localize `There are ${items.length}:itemCount: items`;\n * ```\n *\n * **Escaping colon markers**\n *\n * If you need to use a `:` character directly at the start of a tagged string that has no\n * metadata block, or directly after a substitution expression that has no name you must escape\n * the `:` by preceding it with a backslash:\n *\n * For example:\n *\n * ```ts\n * // message has a metadata block so no need to escape colon\n * $localize `:some description::this message starts with a colon (:)`;\n * // no metadata block so the colon must be escaped\n * $localize `\\:this message starts with a colon (:)`;\n * ```\n *\n * ```ts\n * // named substitution so no need to escape colon\n * $localize `${label}:label:: ${}`\n * // anonymous substitution so colon must be escaped\n * $localize `${label}\\: ${}`\n * ```\n *\n * **Processing localized strings:**\n *\n * There are three scenarios:\n *\n * * **compile-time inlining**: the `$localize` tag is transformed at compile time by a\n * transpiler, removing the tag and replacing the template literal string with a translated\n * literal string from a collection of translations provided to the transpilation tool.\n *\n * * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and\n * reorders the parts (static strings and expressions) of the template literal string with strings\n * from a collection of translations loaded at run-time.\n *\n * * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates\n * the original template literal string without applying any translations to the parts. This\n * version is used during development or where there is no need to translate the localized\n * template literals.\n *\n * @param messageParts a collection of the static parts of the template string.\n * @param expressions a collection of the values of each placeholder in the template string.\n * @returns the translated string, with the `messageParts` and `expressions` interleaved together.\n */\n const $localize: LocalizeFn;\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;;;;;AA0BA,MAAM,YAAY,GAAG,OAAO,UAAU,KAAK,WAAW,IAAI,UAAU,CAAC;AACrE,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC;AACzD,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,WAAW,IAAI,OAAO,iBAAiB,KAAK,WAAW;IAClF,IAAI,YAAY,iBAAiB,IAAI,IAAI,CAAC;AAC9C,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC;AACzD;AACA;AACA;AACO,MAAM,OAAO,GAAQ,YAAY,IAAI,QAAQ,IAAI,QAAQ,IAAI,MAAM;;AClC1E;;;;;;;AA+CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MA+Fa,SAAS,GAAe,UACjC,YAAkC,EAAE,GAAG,WAA2B;IACpE,IAAI,SAAS,CAAC,SAAS,EAAE;;QAEvB,MAAM,WAAW,GAAG,SAAS,CAAC,SAAS,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;QACnE,YAAY,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;QAC9B,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;KAC9B;IACD,IAAI,OAAO,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC5C,OAAO,IAAI,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;KAClF;IACD,OAAO,OAAO,CAAC;AACjB,EAAE;AAEF,MAAM,YAAY,GAAG,GAAG,CAAC;AAEzB;;;;;;;;;;;;;AAaA,SAAS,UAAU,CAAC,WAAmB,EAAE,cAAsB;IAC7D,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,YAAY;QAC5C,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,CAAC,CAAC;QACtE,WAAW,CAAC;AAClB,CAAC;AAED;;;;;;;;;AASA,SAAS,cAAc,CAAC,MAAc,EAAE,GAAW;;;;;;;IAOjD,KAAK,IAAI,WAAW,GAAG,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE,QAAQ,EAAE,EAAE;QAC9F,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE;YAC1B,QAAQ,EAAE,CAAC;SACZ;aAAM,IAAI,MAAM,CAAC,WAAW,CAAC,KAAK,YAAY,EAAE;YAC/C,OAAO,WAAW,CAAC;SACpB;KACF;IACD,MAAM,IAAI,KAAK,CAAC,6CAA6C,GAAG,IAAI,CAAC,CAAC;AACxE;;AC1MA;;;;;;;;ACAA;;;;;;;AAWA;AACA,OAAO,CAAC,SAAS,GAAG,SAAS;;;;"}
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Angular v13.0.0-rc.3
|
|
3
|
+
* (c) 2010-2021 Google LLC. https://angular.io/
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { computeMsgId } from '@angular/compiler';
|
|
8
|
+
export { computeMsgId as ɵcomputeMsgId } from '@angular/compiler';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @license
|
|
12
|
+
* Copyright Google LLC All Rights Reserved.
|
|
13
|
+
*
|
|
14
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
15
|
+
* found in the LICENSE file at https://angular.io/license
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* The character used to mark the start and end of a "block" in a `$localize` tagged string.
|
|
19
|
+
* A block can indicate metadata about the message or specify a name of a placeholder for a
|
|
20
|
+
* substitution expressions.
|
|
21
|
+
*
|
|
22
|
+
* For example:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* $localize`Hello, ${title}:title:!`;
|
|
26
|
+
* $localize`:meaning|description@@id:source message text`;
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
const BLOCK_MARKER = ':';
|
|
30
|
+
/**
|
|
31
|
+
* The marker used to separate a message's "meaning" from its "description" in a metadata block.
|
|
32
|
+
*
|
|
33
|
+
* For example:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* $localize `:correct|Indicates that the user got the answer correct: Right!`;
|
|
37
|
+
* $localize `:movement|Button label for moving to the right: Right!`;
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const MEANING_SEPARATOR = '|';
|
|
41
|
+
/**
|
|
42
|
+
* The marker used to separate a message's custom "id" from its "description" in a metadata block.
|
|
43
|
+
*
|
|
44
|
+
* For example:
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* $localize `:A welcome message on the home page@@myApp-homepage-welcome: Welcome!`;
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
const ID_SEPARATOR = '@@';
|
|
51
|
+
/**
|
|
52
|
+
* The marker used to separate legacy message ids from the rest of a metadata block.
|
|
53
|
+
*
|
|
54
|
+
* For example:
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* $localize `:@@custom-id␟2df64767cd895a8fabe3e18b94b5b6b6f9e2e3f0: Welcome!`;
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* Note that this character is the "symbol for the unit separator" (␟) not the "unit separator
|
|
61
|
+
* character" itself, since that has no visual representation. See https://graphemica.com/%E2%90%9F.
|
|
62
|
+
*
|
|
63
|
+
* Here is some background for the original "unit separator character":
|
|
64
|
+
* https://stackoverflow.com/questions/8695118/whats-the-file-group-record-unit-separator-control-characters-and-its-usage
|
|
65
|
+
*/
|
|
66
|
+
const LEGACY_ID_INDICATOR = '\u241F';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @license
|
|
70
|
+
* Copyright Google LLC All Rights Reserved.
|
|
71
|
+
*
|
|
72
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
73
|
+
* found in the LICENSE file at https://angular.io/license
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Parse a `$localize` tagged string into a structure that can be used for translation or
|
|
77
|
+
* extraction.
|
|
78
|
+
*
|
|
79
|
+
* See `ParsedMessage` for an example.
|
|
80
|
+
*/
|
|
81
|
+
function parseMessage(messageParts, expressions, location, messagePartLocations, expressionLocations = []) {
|
|
82
|
+
const substitutions = {};
|
|
83
|
+
const substitutionLocations = {};
|
|
84
|
+
const metadata = parseMetadata(messageParts[0], messageParts.raw[0]);
|
|
85
|
+
const cleanedMessageParts = [metadata.text];
|
|
86
|
+
const placeholderNames = [];
|
|
87
|
+
let messageString = metadata.text;
|
|
88
|
+
for (let i = 1; i < messageParts.length; i++) {
|
|
89
|
+
const { text: messagePart, block: placeholderName = computePlaceholderName(i) } = splitBlock(messageParts[i], messageParts.raw[i]);
|
|
90
|
+
messageString += `{$${placeholderName}}${messagePart}`;
|
|
91
|
+
if (expressions !== undefined) {
|
|
92
|
+
substitutions[placeholderName] = expressions[i - 1];
|
|
93
|
+
substitutionLocations[placeholderName] = expressionLocations[i - 1];
|
|
94
|
+
}
|
|
95
|
+
placeholderNames.push(placeholderName);
|
|
96
|
+
cleanedMessageParts.push(messagePart);
|
|
97
|
+
}
|
|
98
|
+
const messageId = metadata.customId || computeMsgId(messageString, metadata.meaning || '');
|
|
99
|
+
const legacyIds = metadata.legacyIds ? metadata.legacyIds.filter(id => id !== messageId) : [];
|
|
100
|
+
return {
|
|
101
|
+
id: messageId,
|
|
102
|
+
legacyIds,
|
|
103
|
+
substitutions,
|
|
104
|
+
substitutionLocations,
|
|
105
|
+
text: messageString,
|
|
106
|
+
customId: metadata.customId,
|
|
107
|
+
meaning: metadata.meaning || '',
|
|
108
|
+
description: metadata.description || '',
|
|
109
|
+
messageParts: cleanedMessageParts,
|
|
110
|
+
messagePartLocations,
|
|
111
|
+
placeholderNames,
|
|
112
|
+
location,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Parse the given message part (`cooked` + `raw`) to extract the message metadata from the text.
|
|
117
|
+
*
|
|
118
|
+
* If the message part has a metadata block this function will extract the `meaning`,
|
|
119
|
+
* `description`, `customId` and `legacyId` (if provided) from the block. These metadata properties
|
|
120
|
+
* are serialized in the string delimited by `|`, `@@` and `␟` respectively.
|
|
121
|
+
*
|
|
122
|
+
* (Note that `␟` is the `LEGACY_ID_INDICATOR` - see `constants.ts`.)
|
|
123
|
+
*
|
|
124
|
+
* For example:
|
|
125
|
+
*
|
|
126
|
+
* ```ts
|
|
127
|
+
* `:meaning|description@@custom-id`
|
|
128
|
+
* `:meaning|@@custom-id`
|
|
129
|
+
* `:meaning|description`
|
|
130
|
+
* `description@@custom-id`
|
|
131
|
+
* `meaning|`
|
|
132
|
+
* `description`
|
|
133
|
+
* `@@custom-id`
|
|
134
|
+
* `:meaning|description@@custom-id␟legacy-id-1␟legacy-id-2`
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @param cooked The cooked version of the message part to parse.
|
|
138
|
+
* @param raw The raw version of the message part to parse.
|
|
139
|
+
* @returns A object containing any metadata that was parsed from the message part.
|
|
140
|
+
*/
|
|
141
|
+
function parseMetadata(cooked, raw) {
|
|
142
|
+
const { text: messageString, block } = splitBlock(cooked, raw);
|
|
143
|
+
if (block === undefined) {
|
|
144
|
+
return { text: messageString };
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
const [meaningDescAndId, ...legacyIds] = block.split(LEGACY_ID_INDICATOR);
|
|
148
|
+
const [meaningAndDesc, customId] = meaningDescAndId.split(ID_SEPARATOR, 2);
|
|
149
|
+
let [meaning, description] = meaningAndDesc.split(MEANING_SEPARATOR, 2);
|
|
150
|
+
if (description === undefined) {
|
|
151
|
+
description = meaning;
|
|
152
|
+
meaning = undefined;
|
|
153
|
+
}
|
|
154
|
+
if (description === '') {
|
|
155
|
+
description = undefined;
|
|
156
|
+
}
|
|
157
|
+
return { text: messageString, meaning, description, customId, legacyIds };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Split a message part (`cooked` + `raw`) into an optional delimited "block" off the front and the
|
|
162
|
+
* rest of the text of the message part.
|
|
163
|
+
*
|
|
164
|
+
* Blocks appear at the start of message parts. They are delimited by a colon `:` character at the
|
|
165
|
+
* start and end of the block.
|
|
166
|
+
*
|
|
167
|
+
* If the block is in the first message part then it will be metadata about the whole message:
|
|
168
|
+
* meaning, description, id. Otherwise it will be metadata about the immediately preceding
|
|
169
|
+
* substitution: placeholder name.
|
|
170
|
+
*
|
|
171
|
+
* Since blocks are optional, it is possible that the content of a message block actually starts
|
|
172
|
+
* with a block marker. In this case the marker must be escaped `\:`.
|
|
173
|
+
*
|
|
174
|
+
* @param cooked The cooked version of the message part to parse.
|
|
175
|
+
* @param raw The raw version of the message part to parse.
|
|
176
|
+
* @returns An object containing the `text` of the message part and the text of the `block`, if it
|
|
177
|
+
* exists.
|
|
178
|
+
* @throws an error if the `block` is unterminated
|
|
179
|
+
*/
|
|
180
|
+
function splitBlock(cooked, raw) {
|
|
181
|
+
if (raw.charAt(0) !== BLOCK_MARKER) {
|
|
182
|
+
return { text: cooked };
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
const endOfBlock = findEndOfBlock(cooked, raw);
|
|
186
|
+
return {
|
|
187
|
+
block: cooked.substring(1, endOfBlock),
|
|
188
|
+
text: cooked.substring(endOfBlock + 1),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function computePlaceholderName(index) {
|
|
193
|
+
return index === 1 ? 'PH' : `PH_${index - 1}`;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Find the end of a "marked block" indicated by the first non-escaped colon.
|
|
197
|
+
*
|
|
198
|
+
* @param cooked The cooked string (where escaped chars have been processed)
|
|
199
|
+
* @param raw The raw string (where escape sequences are still in place)
|
|
200
|
+
*
|
|
201
|
+
* @returns the index of the end of block marker
|
|
202
|
+
* @throws an error if the block is unterminated
|
|
203
|
+
*/
|
|
204
|
+
function findEndOfBlock(cooked, raw) {
|
|
205
|
+
/************************************************************************************************
|
|
206
|
+
* This function is repeated in `src/localize/src/localize.ts` and the two should be kept in sync.
|
|
207
|
+
* (See that file for more explanation of why.)
|
|
208
|
+
************************************************************************************************/
|
|
209
|
+
for (let cookedIndex = 1, rawIndex = 1; cookedIndex < cooked.length; cookedIndex++, rawIndex++) {
|
|
210
|
+
if (raw[rawIndex] === '\\') {
|
|
211
|
+
rawIndex++;
|
|
212
|
+
}
|
|
213
|
+
else if (cooked[cookedIndex] === BLOCK_MARKER) {
|
|
214
|
+
return cookedIndex;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
throw new Error(`Unterminated $localize metadata block in "${raw}".`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @license
|
|
222
|
+
* Copyright Google LLC All Rights Reserved.
|
|
223
|
+
*
|
|
224
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
225
|
+
* found in the LICENSE file at https://angular.io/license
|
|
226
|
+
*/
|
|
227
|
+
class MissingTranslationError extends Error {
|
|
228
|
+
constructor(parsedMessage) {
|
|
229
|
+
super(`No translation found for ${describeMessage(parsedMessage)}.`);
|
|
230
|
+
this.parsedMessage = parsedMessage;
|
|
231
|
+
this.type = 'MissingTranslationError';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function isMissingTranslationError(e) {
|
|
235
|
+
return e.type === 'MissingTranslationError';
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Translate the text of the `$localize` tagged-string (i.e. `messageParts` and
|
|
239
|
+
* `substitutions`) using the given `translations`.
|
|
240
|
+
*
|
|
241
|
+
* The tagged-string is parsed to extract its `messageId` which is used to find an appropriate
|
|
242
|
+
* `ParsedTranslation`. If this doesn't match and there are legacy ids then try matching a
|
|
243
|
+
* translation using those.
|
|
244
|
+
*
|
|
245
|
+
* If one is found then it is used to translate the message into a new set of `messageParts` and
|
|
246
|
+
* `substitutions`.
|
|
247
|
+
* The translation may reorder (or remove) substitutions as appropriate.
|
|
248
|
+
*
|
|
249
|
+
* If there is no translation with a matching message id then an error is thrown.
|
|
250
|
+
* If a translation contains a placeholder that is not found in the message being translated then an
|
|
251
|
+
* error is thrown.
|
|
252
|
+
*/
|
|
253
|
+
function translate$1(translations, messageParts, substitutions) {
|
|
254
|
+
const message = parseMessage(messageParts, substitutions);
|
|
255
|
+
// Look up the translation using the messageId, and then the legacyId if available.
|
|
256
|
+
let translation = translations[message.id];
|
|
257
|
+
// If the messageId did not match a translation, try matching the legacy ids instead
|
|
258
|
+
if (message.legacyIds !== undefined) {
|
|
259
|
+
for (let i = 0; i < message.legacyIds.length && translation === undefined; i++) {
|
|
260
|
+
translation = translations[message.legacyIds[i]];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (translation === undefined) {
|
|
264
|
+
throw new MissingTranslationError(message);
|
|
265
|
+
}
|
|
266
|
+
return [
|
|
267
|
+
translation.messageParts, translation.placeholderNames.map(placeholder => {
|
|
268
|
+
if (message.substitutions.hasOwnProperty(placeholder)) {
|
|
269
|
+
return message.substitutions[placeholder];
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
throw new Error(`There is a placeholder name mismatch with the translation provided for the message ${describeMessage(message)}.\n` +
|
|
273
|
+
`The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`);
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Parse the `messageParts` and `placeholderNames` out of a target `message`.
|
|
280
|
+
*
|
|
281
|
+
* Used by `loadTranslations()` to convert target message strings into a structure that is more
|
|
282
|
+
* appropriate for doing translation.
|
|
283
|
+
*
|
|
284
|
+
* @param message the message to be parsed.
|
|
285
|
+
*/
|
|
286
|
+
function parseTranslation(messageString) {
|
|
287
|
+
const parts = messageString.split(/{\$([^}]*)}/);
|
|
288
|
+
const messageParts = [parts[0]];
|
|
289
|
+
const placeholderNames = [];
|
|
290
|
+
for (let i = 1; i < parts.length - 1; i += 2) {
|
|
291
|
+
placeholderNames.push(parts[i]);
|
|
292
|
+
messageParts.push(`${parts[i + 1]}`);
|
|
293
|
+
}
|
|
294
|
+
const rawMessageParts = messageParts.map(part => part.charAt(0) === BLOCK_MARKER ? '\\' + part : part);
|
|
295
|
+
return {
|
|
296
|
+
text: messageString,
|
|
297
|
+
messageParts: makeTemplateObject(messageParts, rawMessageParts),
|
|
298
|
+
placeholderNames,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Create a `ParsedTranslation` from a set of `messageParts` and `placeholderNames`.
|
|
303
|
+
*
|
|
304
|
+
* @param messageParts The message parts to appear in the ParsedTranslation.
|
|
305
|
+
* @param placeholderNames The names of the placeholders to intersperse between the `messageParts`.
|
|
306
|
+
*/
|
|
307
|
+
function makeParsedTranslation(messageParts, placeholderNames = []) {
|
|
308
|
+
let messageString = messageParts[0];
|
|
309
|
+
for (let i = 0; i < placeholderNames.length; i++) {
|
|
310
|
+
messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
text: messageString,
|
|
314
|
+
messageParts: makeTemplateObject(messageParts, messageParts),
|
|
315
|
+
placeholderNames
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Create the specialized array that is passed to tagged-string tag functions.
|
|
320
|
+
*
|
|
321
|
+
* @param cooked The message parts with their escape codes processed.
|
|
322
|
+
* @param raw The message parts with their escaped codes as-is.
|
|
323
|
+
*/
|
|
324
|
+
function makeTemplateObject(cooked, raw) {
|
|
325
|
+
Object.defineProperty(cooked, 'raw', { value: raw });
|
|
326
|
+
return cooked;
|
|
327
|
+
}
|
|
328
|
+
function describeMessage(message) {
|
|
329
|
+
const meaningString = message.meaning && ` - "${message.meaning}"`;
|
|
330
|
+
const legacy = message.legacyIds && message.legacyIds.length > 0 ?
|
|
331
|
+
` [${message.legacyIds.map(l => `"${l}"`).join(', ')}]` :
|
|
332
|
+
'';
|
|
333
|
+
return `"${message.id}"${legacy} ("${message.text}"${meaningString})`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @license
|
|
338
|
+
* Copyright Google LLC All Rights Reserved.
|
|
339
|
+
*
|
|
340
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
341
|
+
* found in the LICENSE file at https://angular.io/license
|
|
342
|
+
*/
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Load translations for use by `$localize`, if doing runtime translation.
|
|
346
|
+
*
|
|
347
|
+
* If the `$localize` tagged strings are not going to be replaced at compiled time, it is possible
|
|
348
|
+
* to load a set of translations that will be applied to the `$localize` tagged strings at runtime,
|
|
349
|
+
* in the browser.
|
|
350
|
+
*
|
|
351
|
+
* Loading a new translation will overwrite a previous translation if it has the same `MessageId`.
|
|
352
|
+
*
|
|
353
|
+
* Note that `$localize` messages are only processed once, when the tagged string is first
|
|
354
|
+
* encountered, and does not provide dynamic language changing without refreshing the browser.
|
|
355
|
+
* Loading new translations later in the application life-cycle will not change the translated text
|
|
356
|
+
* of messages that have already been translated.
|
|
357
|
+
*
|
|
358
|
+
* The message IDs and translations are in the same format as that rendered to "simple JSON"
|
|
359
|
+
* translation files when extracting messages. In particular, placeholders in messages are rendered
|
|
360
|
+
* using the `{$PLACEHOLDER_NAME}` syntax. For example the message from the following template:
|
|
361
|
+
*
|
|
362
|
+
* ```html
|
|
363
|
+
* <div i18n>pre<span>inner-pre<b>bold</b>inner-post</span>post</div>
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* would have the following form in the `translations` map:
|
|
367
|
+
*
|
|
368
|
+
* ```ts
|
|
369
|
+
* {
|
|
370
|
+
* "2932901491976224757":
|
|
371
|
+
* "pre{$START_TAG_SPAN}inner-pre{$START_BOLD_TEXT}bold{$CLOSE_BOLD_TEXT}inner-post{$CLOSE_TAG_SPAN}post"
|
|
372
|
+
* }
|
|
373
|
+
* ```
|
|
374
|
+
*
|
|
375
|
+
* @param translations A map from message ID to translated message.
|
|
376
|
+
*
|
|
377
|
+
* These messages are processed and added to a lookup based on their `MessageId`.
|
|
378
|
+
*
|
|
379
|
+
* @see `clearTranslations()` for removing translations loaded using this function.
|
|
380
|
+
* @see `$localize` for tagging messages as needing to be translated.
|
|
381
|
+
* @publicApi
|
|
382
|
+
*/
|
|
383
|
+
function loadTranslations(translations) {
|
|
384
|
+
// Ensure the translate function exists
|
|
385
|
+
if (!$localize.translate) {
|
|
386
|
+
$localize.translate = translate;
|
|
387
|
+
}
|
|
388
|
+
if (!$localize.TRANSLATIONS) {
|
|
389
|
+
$localize.TRANSLATIONS = {};
|
|
390
|
+
}
|
|
391
|
+
Object.keys(translations).forEach(key => {
|
|
392
|
+
$localize.TRANSLATIONS[key] = parseTranslation(translations[key]);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Remove all translations for `$localize`, if doing runtime translation.
|
|
397
|
+
*
|
|
398
|
+
* All translations that had been loading into memory using `loadTranslations()` will be removed.
|
|
399
|
+
*
|
|
400
|
+
* @see `loadTranslations()` for loading translations at runtime.
|
|
401
|
+
* @see `$localize` for tagging messages as needing to be translated.
|
|
402
|
+
*
|
|
403
|
+
* @publicApi
|
|
404
|
+
*/
|
|
405
|
+
function clearTranslations() {
|
|
406
|
+
$localize.translate = undefined;
|
|
407
|
+
$localize.TRANSLATIONS = {};
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Translate the text of the given message, using the loaded translations.
|
|
411
|
+
*
|
|
412
|
+
* This function may reorder (or remove) substitutions as indicated in the matching translation.
|
|
413
|
+
*/
|
|
414
|
+
function translate(messageParts, substitutions) {
|
|
415
|
+
try {
|
|
416
|
+
return translate$1($localize.TRANSLATIONS, messageParts, substitutions);
|
|
417
|
+
}
|
|
418
|
+
catch (e) {
|
|
419
|
+
console.warn(e.message);
|
|
420
|
+
return [messageParts, substitutions];
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @license
|
|
426
|
+
* Copyright Google LLC All Rights Reserved.
|
|
427
|
+
*
|
|
428
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
429
|
+
* found in the LICENSE file at https://angular.io/license
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* @license
|
|
434
|
+
* Copyright Google LLC All Rights Reserved.
|
|
435
|
+
*
|
|
436
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
437
|
+
* found in the LICENSE file at https://angular.io/license
|
|
438
|
+
*/
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @license
|
|
442
|
+
* Copyright Google LLC All Rights Reserved.
|
|
443
|
+
*
|
|
444
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
445
|
+
* found in the LICENSE file at https://angular.io/license
|
|
446
|
+
*/
|
|
447
|
+
|
|
448
|
+
export { clearTranslations, loadTranslations, MissingTranslationError as ɵMissingTranslationError, findEndOfBlock as ɵfindEndOfBlock, isMissingTranslationError as ɵisMissingTranslationError, makeParsedTranslation as ɵmakeParsedTranslation, makeTemplateObject as ɵmakeTemplateObject, parseMessage as ɵparseMessage, parseMetadata as ɵparseMetadata, parseTranslation as ɵparseTranslation, splitBlock as ɵsplitBlock, translate$1 as ɵtranslate };
|
|
449
|
+
//# sourceMappingURL=localize.mjs.map
|