@angular/localize 21.0.0-next.1 → 21.0.0-next.10
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/fesm2022/_localize-chunk.mjs +257 -0
- package/fesm2022/_localize-chunk.mjs.map +1 -0
- package/fesm2022/init.mjs +3 -4
- package/fesm2022/init.mjs.map +1 -1
- package/fesm2022/localize.mjs +72 -171
- package/fesm2022/localize.mjs.map +1 -1
- package/package.json +7 -7
- package/schematics/ng-add/ng_add_bundle.cjs +1 -1
- package/types/init.d.ts +7 -0
- package/{index.d.ts → types/localize.d.ts} +2 -2
- package/fesm2022/localize2.mjs +0 -496
- package/fesm2022/localize2.mjs.map +0 -1
- package/init/index.d.ts +0 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localize.mjs","sources":["../../../../../k8-fastbuild-ST-199a4f3c4e20/bin/packages/localize/src/utils/src/translations.ts","../../../../../k8-fastbuild-ST-199a4f3c4e20/bin/packages/localize/src/translate.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.dev/license\n */\nimport {BLOCK_MARKER} from './constants';\nimport {MessageId, MessageMetadata, ParsedMessage, parseMessage, TargetMessage} from './messages';\n\n/**\n * A translation message that has been processed to extract the message parts and placeholders.\n */\nexport interface ParsedTranslation extends MessageMetadata {\n messageParts: TemplateStringsArray;\n placeholderNames: string[];\n}\n\n/**\n * The internal structure used by the runtime localization to translate messages.\n */\nexport type ParsedTranslations = Record<MessageId, ParsedTranslation>;\n\nexport class MissingTranslationError extends Error {\n private readonly type = 'MissingTranslationError';\n constructor(readonly parsedMessage: ParsedMessage) {\n super(`No translation found for ${describeMessage(parsedMessage)}.`);\n }\n}\n\nexport function isMissingTranslationError(e: any): e is MissingTranslationError {\n return e.type === 'MissingTranslationError';\n}\n\n/**\n * Translate the text of the `$localize` tagged-string (i.e. `messageParts` and\n * `substitutions`) using the given `translations`.\n *\n * The tagged-string is parsed to extract its `messageId` which is used to find an appropriate\n * `ParsedTranslation`. If this doesn't match and there are legacy ids then try matching a\n * translation using those.\n *\n * If one is found then it is used to translate the message into a new set of `messageParts` and\n * `substitutions`.\n * The translation may reorder (or remove) substitutions as appropriate.\n *\n * If there is no translation with a matching message id then an error is thrown.\n * If a translation contains a placeholder that is not found in the message being translated then an\n * error is thrown.\n */\nexport function translate(\n translations: Record<string, ParsedTranslation>,\n messageParts: TemplateStringsArray,\n substitutions: readonly any[],\n): [TemplateStringsArray, readonly any[]] {\n const message = parseMessage(messageParts, substitutions);\n // Look up the translation using the messageId, and then the legacyId if available.\n let translation = translations[message.id];\n // If the messageId did not match a translation, try matching the legacy ids instead\n if (message.legacyIds !== undefined) {\n for (let i = 0; i < message.legacyIds.length && translation === undefined; i++) {\n translation = translations[message.legacyIds[i]];\n }\n }\n if (translation === undefined) {\n throw new MissingTranslationError(message);\n }\n return [\n translation.messageParts,\n translation.placeholderNames.map((placeholder) => {\n if (message.substitutions.hasOwnProperty(placeholder)) {\n return message.substitutions[placeholder];\n } else {\n throw new Error(\n `There is a placeholder name mismatch with the translation provided for the message ${describeMessage(\n message,\n )}.\\n` +\n `The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`,\n );\n }\n }),\n ];\n}\n\n/**\n * Parse the `messageParts` and `placeholderNames` out of a target `message`.\n *\n * Used by `loadTranslations()` to convert target message strings into a structure that is more\n * appropriate for doing translation.\n *\n * @param message the message to be parsed.\n */\nexport function parseTranslation(messageString: TargetMessage): ParsedTranslation {\n const parts = messageString.split(/{\\$([^}]*)}/);\n const messageParts = [parts[0]];\n const placeholderNames: string[] = [];\n for (let i = 1; i < parts.length - 1; i += 2) {\n placeholderNames.push(parts[i]);\n messageParts.push(`${parts[i + 1]}`);\n }\n const rawMessageParts = messageParts.map((part) =>\n part.charAt(0) === BLOCK_MARKER ? '\\\\' + part : part,\n );\n return {\n text: messageString,\n messageParts: makeTemplateObject(messageParts, rawMessageParts),\n placeholderNames,\n };\n}\n\n/**\n * Create a `ParsedTranslation` from a set of `messageParts` and `placeholderNames`.\n *\n * @param messageParts The message parts to appear in the ParsedTranslation.\n * @param placeholderNames The names of the placeholders to intersperse between the `messageParts`.\n */\nexport function makeParsedTranslation(\n messageParts: string[],\n placeholderNames: string[] = [],\n): ParsedTranslation {\n let messageString = messageParts[0];\n for (let i = 0; i < placeholderNames.length; i++) {\n messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;\n }\n return {\n text: messageString,\n messageParts: makeTemplateObject(messageParts, messageParts),\n placeholderNames,\n };\n}\n\n/**\n * Create the specialized array that is passed to tagged-string tag functions.\n *\n * @param cooked The message parts with their escape codes processed.\n * @param raw The message parts with their escaped codes as-is.\n */\nexport function makeTemplateObject(cooked: string[], raw: string[]): TemplateStringsArray {\n Object.defineProperty(cooked, 'raw', {value: raw});\n return cooked as any;\n}\n\nfunction describeMessage(message: ParsedMessage): string {\n const meaningString = message.meaning && ` - \"${message.meaning}\"`;\n const legacy =\n message.legacyIds && message.legacyIds.length > 0\n ? ` [${message.legacyIds.map((l) => `\"${l}\"`).join(', ')}]`\n : '';\n return `\"${message.id}\"${legacy} (\"${message.text}\"${meaningString})`;\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.dev/license\n */\nimport {LocalizeFn} from './localize';\nimport {\n MessageId,\n ParsedTranslation,\n parseTranslation,\n TargetMessage,\n translate as _translate,\n} from './utils';\n\n/**\n * We augment the `$localize` object to also store the translations.\n *\n * Note that because the TRANSLATIONS are attached to a global object, they will be shared between\n * all applications that are running in a single page of the browser.\n */\ndeclare const $localize: LocalizeFn & {TRANSLATIONS: Record<MessageId, ParsedTranslation>};\n\n/**\n * Load translations for use by `$localize`, if doing runtime translation.\n *\n * If the `$localize` tagged strings are not going to be replaced at compiled time, it is possible\n * to load a set of translations that will be applied to the `$localize` tagged strings at runtime,\n * in the browser.\n *\n * Loading a new translation will overwrite a previous translation if it has the same `MessageId`.\n *\n * Note that `$localize` messages are only processed once, when the tagged string is first\n * encountered, and does not provide dynamic language changing without refreshing the browser.\n * Loading new translations later in the application life-cycle will not change the translated text\n * of messages that have already been translated.\n *\n * The message IDs and translations are in the same format as that rendered to \"simple JSON\"\n * translation files when extracting messages. In particular, placeholders in messages are rendered\n * using the `{$PLACEHOLDER_NAME}` syntax. For example the message from the following template:\n *\n * ```html\n * <div i18n>pre<span>inner-pre<b>bold</b>inner-post</span>post</div>\n * ```\n *\n * would have the following form in the `translations` map:\n *\n * ```ts\n * {\n * \"2932901491976224757\":\n * \"pre{$START_TAG_SPAN}inner-pre{$START_BOLD_TEXT}bold{$CLOSE_BOLD_TEXT}inner-post{$CLOSE_TAG_SPAN}post\"\n * }\n * ```\n *\n * @param translations A map from message ID to translated message.\n *\n * These messages are processed and added to a lookup based on their `MessageId`.\n *\n * @see {@link clearTranslations} for removing translations loaded using this function.\n * @see {@link /api/localize/init/$localize $localize} for tagging messages as needing to be translated.\n * @publicApi\n */\nexport function loadTranslations(translations: Record<MessageId, TargetMessage>) {\n // Ensure the translate function exists\n if (!$localize.translate) {\n $localize.translate = translate;\n }\n if (!$localize.TRANSLATIONS) {\n $localize.TRANSLATIONS = {};\n }\n Object.keys(translations).forEach((key) => {\n $localize.TRANSLATIONS[key] = parseTranslation(translations[key]);\n });\n}\n\n/**\n * Remove all translations for `$localize`, if doing runtime translation.\n *\n * All translations that had been loading into memory using `loadTranslations()` will be removed.\n *\n * @see {@link loadTranslations} for loading translations at runtime.\n * @see {@link /api/localize/init/$localize $localize} for tagging messages as needing to be translated.\n *\n * @publicApi\n */\nexport function clearTranslations() {\n $localize.translate = undefined;\n $localize.TRANSLATIONS = {};\n}\n\n/**\n * Translate the text of the given message, using the loaded translations.\n *\n * This function may reorder (or remove) substitutions as indicated in the matching translation.\n */\nexport function translate(\n messageParts: TemplateStringsArray,\n substitutions: readonly any[],\n): [TemplateStringsArray, readonly any[]] {\n try {\n return _translate($localize.TRANSLATIONS, messageParts, substitutions);\n } catch (e) {\n console.warn((e as Error).message);\n return [messageParts, substitutions];\n }\n}\n"],"names":["translate","_translate"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"localize.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-199a4f3c4e20/bin/packages/localize/src/utils/src/translations.ts","../../../../../darwin_arm64-fastbuild-ST-199a4f3c4e20/bin/packages/localize/src/translate.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.dev/license\n */\nimport {BLOCK_MARKER} from './constants';\nimport {MessageId, MessageMetadata, ParsedMessage, parseMessage, TargetMessage} from './messages';\n\n/**\n * A translation message that has been processed to extract the message parts and placeholders.\n */\nexport interface ParsedTranslation extends MessageMetadata {\n messageParts: TemplateStringsArray;\n placeholderNames: string[];\n}\n\n/**\n * The internal structure used by the runtime localization to translate messages.\n */\nexport type ParsedTranslations = Record<MessageId, ParsedTranslation>;\n\nexport class MissingTranslationError extends Error {\n private readonly type = 'MissingTranslationError';\n constructor(readonly parsedMessage: ParsedMessage) {\n super(`No translation found for ${describeMessage(parsedMessage)}.`);\n }\n}\n\nexport function isMissingTranslationError(e: any): e is MissingTranslationError {\n return e.type === 'MissingTranslationError';\n}\n\n/**\n * Translate the text of the `$localize` tagged-string (i.e. `messageParts` and\n * `substitutions`) using the given `translations`.\n *\n * The tagged-string is parsed to extract its `messageId` which is used to find an appropriate\n * `ParsedTranslation`. If this doesn't match and there are legacy ids then try matching a\n * translation using those.\n *\n * If one is found then it is used to translate the message into a new set of `messageParts` and\n * `substitutions`.\n * The translation may reorder (or remove) substitutions as appropriate.\n *\n * If there is no translation with a matching message id then an error is thrown.\n * If a translation contains a placeholder that is not found in the message being translated then an\n * error is thrown.\n */\nexport function translate(\n translations: Record<string, ParsedTranslation>,\n messageParts: TemplateStringsArray,\n substitutions: readonly any[],\n): [TemplateStringsArray, readonly any[]] {\n const message = parseMessage(messageParts, substitutions);\n // Look up the translation using the messageId, and then the legacyId if available.\n let translation = translations[message.id];\n // If the messageId did not match a translation, try matching the legacy ids instead\n if (message.legacyIds !== undefined) {\n for (let i = 0; i < message.legacyIds.length && translation === undefined; i++) {\n translation = translations[message.legacyIds[i]];\n }\n }\n if (translation === undefined) {\n throw new MissingTranslationError(message);\n }\n return [\n translation.messageParts,\n translation.placeholderNames.map((placeholder) => {\n if (message.substitutions.hasOwnProperty(placeholder)) {\n return message.substitutions[placeholder];\n } else {\n throw new Error(\n `There is a placeholder name mismatch with the translation provided for the message ${describeMessage(\n message,\n )}.\\n` +\n `The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`,\n );\n }\n }),\n ];\n}\n\n/**\n * Parse the `messageParts` and `placeholderNames` out of a target `message`.\n *\n * Used by `loadTranslations()` to convert target message strings into a structure that is more\n * appropriate for doing translation.\n *\n * @param message the message to be parsed.\n */\nexport function parseTranslation(messageString: TargetMessage): ParsedTranslation {\n const parts = messageString.split(/{\\$([^}]*)}/);\n const messageParts = [parts[0]];\n const placeholderNames: string[] = [];\n for (let i = 1; i < parts.length - 1; i += 2) {\n placeholderNames.push(parts[i]);\n messageParts.push(`${parts[i + 1]}`);\n }\n const rawMessageParts = messageParts.map((part) =>\n part.charAt(0) === BLOCK_MARKER ? '\\\\' + part : part,\n );\n return {\n text: messageString,\n messageParts: makeTemplateObject(messageParts, rawMessageParts),\n placeholderNames,\n };\n}\n\n/**\n * Create a `ParsedTranslation` from a set of `messageParts` and `placeholderNames`.\n *\n * @param messageParts The message parts to appear in the ParsedTranslation.\n * @param placeholderNames The names of the placeholders to intersperse between the `messageParts`.\n */\nexport function makeParsedTranslation(\n messageParts: string[],\n placeholderNames: string[] = [],\n): ParsedTranslation {\n let messageString = messageParts[0];\n for (let i = 0; i < placeholderNames.length; i++) {\n messageString += `{$${placeholderNames[i]}}${messageParts[i + 1]}`;\n }\n return {\n text: messageString,\n messageParts: makeTemplateObject(messageParts, messageParts),\n placeholderNames,\n };\n}\n\n/**\n * Create the specialized array that is passed to tagged-string tag functions.\n *\n * @param cooked The message parts with their escape codes processed.\n * @param raw The message parts with their escaped codes as-is.\n */\nexport function makeTemplateObject(cooked: string[], raw: string[]): TemplateStringsArray {\n Object.defineProperty(cooked, 'raw', {value: raw});\n return cooked as any;\n}\n\nfunction describeMessage(message: ParsedMessage): string {\n const meaningString = message.meaning && ` - \"${message.meaning}\"`;\n const legacy =\n message.legacyIds && message.legacyIds.length > 0\n ? ` [${message.legacyIds.map((l) => `\"${l}\"`).join(', ')}]`\n : '';\n return `\"${message.id}\"${legacy} (\"${message.text}\"${meaningString})`;\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.dev/license\n */\nimport {LocalizeFn} from './localize';\nimport {\n MessageId,\n ParsedTranslation,\n parseTranslation,\n TargetMessage,\n translate as _translate,\n} from './utils';\n\n/**\n * We augment the `$localize` object to also store the translations.\n *\n * Note that because the TRANSLATIONS are attached to a global object, they will be shared between\n * all applications that are running in a single page of the browser.\n */\ndeclare const $localize: LocalizeFn & {TRANSLATIONS: Record<MessageId, ParsedTranslation>};\n\n/**\n * Load translations for use by `$localize`, if doing runtime translation.\n *\n * If the `$localize` tagged strings are not going to be replaced at compiled time, it is possible\n * to load a set of translations that will be applied to the `$localize` tagged strings at runtime,\n * in the browser.\n *\n * Loading a new translation will overwrite a previous translation if it has the same `MessageId`.\n *\n * Note that `$localize` messages are only processed once, when the tagged string is first\n * encountered, and does not provide dynamic language changing without refreshing the browser.\n * Loading new translations later in the application life-cycle will not change the translated text\n * of messages that have already been translated.\n *\n * The message IDs and translations are in the same format as that rendered to \"simple JSON\"\n * translation files when extracting messages. In particular, placeholders in messages are rendered\n * using the `{$PLACEHOLDER_NAME}` syntax. For example the message from the following template:\n *\n * ```html\n * <div i18n>pre<span>inner-pre<b>bold</b>inner-post</span>post</div>\n * ```\n *\n * would have the following form in the `translations` map:\n *\n * ```ts\n * {\n * \"2932901491976224757\":\n * \"pre{$START_TAG_SPAN}inner-pre{$START_BOLD_TEXT}bold{$CLOSE_BOLD_TEXT}inner-post{$CLOSE_TAG_SPAN}post\"\n * }\n * ```\n *\n * @param translations A map from message ID to translated message.\n *\n * These messages are processed and added to a lookup based on their `MessageId`.\n *\n * @see {@link clearTranslations} for removing translations loaded using this function.\n * @see {@link /api/localize/init/$localize $localize} for tagging messages as needing to be translated.\n * @publicApi\n */\nexport function loadTranslations(translations: Record<MessageId, TargetMessage>) {\n // Ensure the translate function exists\n if (!$localize.translate) {\n $localize.translate = translate;\n }\n if (!$localize.TRANSLATIONS) {\n $localize.TRANSLATIONS = {};\n }\n Object.keys(translations).forEach((key) => {\n $localize.TRANSLATIONS[key] = parseTranslation(translations[key]);\n });\n}\n\n/**\n * Remove all translations for `$localize`, if doing runtime translation.\n *\n * All translations that had been loading into memory using `loadTranslations()` will be removed.\n *\n * @see {@link loadTranslations} for loading translations at runtime.\n * @see {@link /api/localize/init/$localize $localize} for tagging messages as needing to be translated.\n *\n * @publicApi\n */\nexport function clearTranslations() {\n $localize.translate = undefined;\n $localize.TRANSLATIONS = {};\n}\n\n/**\n * Translate the text of the given message, using the loaded translations.\n *\n * This function may reorder (or remove) substitutions as indicated in the matching translation.\n */\nexport function translate(\n messageParts: TemplateStringsArray,\n substitutions: readonly any[],\n): [TemplateStringsArray, readonly any[]] {\n try {\n return _translate($localize.TRANSLATIONS, messageParts, substitutions);\n } catch (e) {\n console.warn((e as Error).message);\n return [messageParts, substitutions];\n }\n}\n"],"names":["parsedMessage","message","legacyIds","translation","placeholderNames","map","placeholder","substitutions","hasOwnProperty","Error","describeMessage","messageParts","push","parts","i","rawMessageParts","part","charAt","BLOCK_MARKER","text","messageString","loadTranslations","translations","$localize","translate","Object","keys","forEach","key","TRANSLATIONS","parseTranslation"],"mappings":";;;;;;;;;;;;;;QAyBuB,CAAAA,aAAa,GAAAA;;;;;;;;;;;gCAsD9B,CAAAC,OACF,CAAAC;AAIJ;;;;;oCAOGC,WAAA,CAAAC,gBAAA,CAAAC,GAAA,CAAAC,WAAA,IAAA;AACG,IAAA,IAAAL,OAAA,CAAAM,aAAA,CAAAC,cAAA,CAAAF,WAAA,CAAA,EAAA;AACE,MAAA,cAAqB,CAAAC;;AAGtB,MAAA,MAAA,IAAAE,KAAA,CAAAC,CAAAA,mFAAAA,EAAAA,eAAA,CAAAT,OAAA,CAAA,CAAA,GAAA,CAAA,GACa;AAChB;;;;;;;;;AA4BAU,IAAAA,YAAA,CAAAC,IAAA,CAAAC,CAAAA,EAAAA,KAAA,CAAAC,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;QAEDC,eAAA,GAAAJ,YAAA,CAAAN,GAAA,CAAAW,IAAA,IAAAA,IAAA,CAAAC,MAAA,CAAAC,CAAAA,CAAAA,KAAAA,YAAA,GAAAF,IAAAA,GAAAA,IAAA,GAAAA,IAAA,CAAA;EACH,OAAA;AAEAG,IAAAA,IAAA,EAAAC,aAAA;;;;;;EAiBE,IAAAA,aAAA,GAAAT,YAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;AC7DA,SAAUU,gBAAYA,CAAAC,YAAU,EAAA;AAEjC,EAAA,IAAA,CAAAC,SAAA,CAAAC,SAAA,EAAA;IAEDD,SAAA,CAAAC,SAAA,GAAAA,SAAA;;;;AAIG;EACHC,MAAgB,CAAAC,IAAA,CAAAJ,YACd,CAAA,CAAAK,OAAA,CAAAC,GACA,IAA6B;AAE7BL,IAAAA,SAAK,CAAAM,YAAA,CAAAD,GAAA,CAAA,GAAAE,gBAAA,CAAAR,YAAA,CAAAM,GAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angular/localize",
|
|
3
|
-
"version": "21.0.0-next.
|
|
3
|
+
"version": "21.0.0-next.10",
|
|
4
4
|
"description": "Angular - library for localizing messages",
|
|
5
5
|
"bin": {
|
|
6
6
|
"localize-translate": "./tools/bundles/src/translate/cli.js",
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"default": "./package.json"
|
|
17
17
|
},
|
|
18
18
|
".": {
|
|
19
|
-
"types": "./
|
|
19
|
+
"types": "./types/localize.d.ts",
|
|
20
20
|
"default": "./fesm2022/localize.mjs"
|
|
21
21
|
},
|
|
22
22
|
"./init": {
|
|
23
|
-
"types": "./init
|
|
23
|
+
"types": "./types/init.d.ts",
|
|
24
24
|
"default": "./fesm2022/init.mjs"
|
|
25
25
|
}
|
|
26
26
|
},
|
|
@@ -61,16 +61,16 @@
|
|
|
61
61
|
"./fesm2022/init.mjs"
|
|
62
62
|
],
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@babel/core": "7.28.
|
|
64
|
+
"@babel/core": "7.28.4",
|
|
65
65
|
"@types/babel__core": "7.20.5",
|
|
66
66
|
"tinyglobby": "^0.2.12",
|
|
67
67
|
"yargs": "^18.0.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@angular/compiler": "21.0.0-next.
|
|
71
|
-
"@angular/compiler-cli": "21.0.0-next.
|
|
70
|
+
"@angular/compiler": "21.0.0-next.10",
|
|
71
|
+
"@angular/compiler-cli": "21.0.0-next.10"
|
|
72
72
|
},
|
|
73
73
|
"module": "./fesm2022/localize.mjs",
|
|
74
|
-
"typings": "./
|
|
74
|
+
"typings": "./types/localize.d.ts",
|
|
75
75
|
"type": "module"
|
|
76
76
|
}
|
|
@@ -131,7 +131,7 @@ function moveToDependencies(host) {
|
|
|
131
131
|
return;
|
|
132
132
|
}
|
|
133
133
|
(0, import_dependencies.removePackageJsonDependency)(host, "@angular/localize");
|
|
134
|
-
return (0, import_utility.addDependency)("@angular/localize", `~21.0.0-next.
|
|
134
|
+
return (0, import_utility.addDependency)("@angular/localize", `~21.0.0-next.10`);
|
|
135
135
|
}
|
|
136
136
|
function ng_add_default(options) {
|
|
137
137
|
const projectName = options.project;
|
package/types/init.d.ts
ADDED
package/fesm2022/localize2.mjs
DELETED
|
@@ -1,496 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Angular v21.0.0-next.1
|
|
3
|
-
* (c) 2010-2025 Google LLC. https://angular.io/
|
|
4
|
-
* License: MIT
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The character used to mark the start and end of a "block" in a `$localize` tagged string.
|
|
9
|
-
* A block can indicate metadata about the message or specify a name of a placeholder for a
|
|
10
|
-
* substitution expressions.
|
|
11
|
-
*
|
|
12
|
-
* For example:
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* $localize`Hello, ${title}:title:!`;
|
|
16
|
-
* $localize`:meaning|description@@id:source message text`;
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
const BLOCK_MARKER$1 = ':';
|
|
20
|
-
/**
|
|
21
|
-
* The marker used to separate a message's "meaning" from its "description" in a metadata block.
|
|
22
|
-
*
|
|
23
|
-
* For example:
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* $localize `:correct|Indicates that the user got the answer correct: Right!`;
|
|
27
|
-
* $localize `:movement|Button label for moving to the right: Right!`;
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
const MEANING_SEPARATOR = '|';
|
|
31
|
-
/**
|
|
32
|
-
* The marker used to separate a message's custom "id" from its "description" in a metadata block.
|
|
33
|
-
*
|
|
34
|
-
* For example:
|
|
35
|
-
*
|
|
36
|
-
* ```ts
|
|
37
|
-
* $localize `:A welcome message on the home page@@myApp-homepage-welcome: Welcome!`;
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
const ID_SEPARATOR = '@@';
|
|
41
|
-
/**
|
|
42
|
-
* The marker used to separate legacy message ids from the rest of a metadata block.
|
|
43
|
-
*
|
|
44
|
-
* For example:
|
|
45
|
-
*
|
|
46
|
-
* ```ts
|
|
47
|
-
* $localize `:@@custom-id␟2df64767cd895a8fabe3e18b94b5b6b6f9e2e3f0: Welcome!`;
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* Note that this character is the "symbol for the unit separator" (␟) not the "unit separator
|
|
51
|
-
* character" itself, since that has no visual representation. See https://graphemica.com/%E2%90%9F.
|
|
52
|
-
*
|
|
53
|
-
* Here is some background for the original "unit separator character":
|
|
54
|
-
* https://stackoverflow.com/questions/8695118/whats-the-file-group-record-unit-separator-control-characters-and-its-usage
|
|
55
|
-
*/
|
|
56
|
-
const LEGACY_ID_INDICATOR = '\u241F';
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* A lazily created TextEncoder instance for converting strings into UTF-8 bytes
|
|
60
|
-
*/
|
|
61
|
-
let textEncoder;
|
|
62
|
-
/**
|
|
63
|
-
* Compute the fingerprint of the given string
|
|
64
|
-
*
|
|
65
|
-
* The output is 64 bit number encoded as a decimal string
|
|
66
|
-
*
|
|
67
|
-
* based on:
|
|
68
|
-
* https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
|
|
69
|
-
*/
|
|
70
|
-
function fingerprint(str) {
|
|
71
|
-
textEncoder ??= new TextEncoder();
|
|
72
|
-
const utf8 = textEncoder.encode(str);
|
|
73
|
-
const view = new DataView(utf8.buffer, utf8.byteOffset, utf8.byteLength);
|
|
74
|
-
let hi = hash32(view, utf8.length, 0);
|
|
75
|
-
let lo = hash32(view, utf8.length, 102072);
|
|
76
|
-
if (hi == 0 && (lo == 0 || lo == 1)) {
|
|
77
|
-
hi = hi ^ 0x130f9bef;
|
|
78
|
-
lo = lo ^ -0x6b5f56d8;
|
|
79
|
-
}
|
|
80
|
-
return (BigInt.asUintN(32, BigInt(hi)) << BigInt(32)) | BigInt.asUintN(32, BigInt(lo));
|
|
81
|
-
}
|
|
82
|
-
function computeMsgId(msg, meaning = '') {
|
|
83
|
-
let msgFingerprint = fingerprint(msg);
|
|
84
|
-
if (meaning) {
|
|
85
|
-
// Rotate the 64-bit message fingerprint one bit to the left and then add the meaning
|
|
86
|
-
// fingerprint.
|
|
87
|
-
msgFingerprint =
|
|
88
|
-
BigInt.asUintN(64, msgFingerprint << BigInt(1)) |
|
|
89
|
-
((msgFingerprint >> BigInt(63)) & BigInt(1));
|
|
90
|
-
msgFingerprint += fingerprint(meaning);
|
|
91
|
-
}
|
|
92
|
-
return BigInt.asUintN(63, msgFingerprint).toString();
|
|
93
|
-
}
|
|
94
|
-
function hash32(view, length, c) {
|
|
95
|
-
let a = 0x9e3779b9, b = 0x9e3779b9;
|
|
96
|
-
let index = 0;
|
|
97
|
-
const end = length - 12;
|
|
98
|
-
for (; index <= end; index += 12) {
|
|
99
|
-
a += view.getUint32(index, true);
|
|
100
|
-
b += view.getUint32(index + 4, true);
|
|
101
|
-
c += view.getUint32(index + 8, true);
|
|
102
|
-
const res = mix(a, b, c);
|
|
103
|
-
(a = res[0]), (b = res[1]), (c = res[2]);
|
|
104
|
-
}
|
|
105
|
-
const remainder = length - index;
|
|
106
|
-
// the first byte of c is reserved for the length
|
|
107
|
-
c += length;
|
|
108
|
-
if (remainder >= 4) {
|
|
109
|
-
a += view.getUint32(index, true);
|
|
110
|
-
index += 4;
|
|
111
|
-
if (remainder >= 8) {
|
|
112
|
-
b += view.getUint32(index, true);
|
|
113
|
-
index += 4;
|
|
114
|
-
// Partial 32-bit word for c
|
|
115
|
-
if (remainder >= 9) {
|
|
116
|
-
c += view.getUint8(index++) << 8;
|
|
117
|
-
}
|
|
118
|
-
if (remainder >= 10) {
|
|
119
|
-
c += view.getUint8(index++) << 16;
|
|
120
|
-
}
|
|
121
|
-
if (remainder === 11) {
|
|
122
|
-
c += view.getUint8(index++) << 24;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
// Partial 32-bit word for b
|
|
127
|
-
if (remainder >= 5) {
|
|
128
|
-
b += view.getUint8(index++);
|
|
129
|
-
}
|
|
130
|
-
if (remainder >= 6) {
|
|
131
|
-
b += view.getUint8(index++) << 8;
|
|
132
|
-
}
|
|
133
|
-
if (remainder === 7) {
|
|
134
|
-
b += view.getUint8(index++) << 16;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
// Partial 32-bit word for a
|
|
140
|
-
if (remainder >= 1) {
|
|
141
|
-
a += view.getUint8(index++);
|
|
142
|
-
}
|
|
143
|
-
if (remainder >= 2) {
|
|
144
|
-
a += view.getUint8(index++) << 8;
|
|
145
|
-
}
|
|
146
|
-
if (remainder === 3) {
|
|
147
|
-
a += view.getUint8(index++) << 16;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return mix(a, b, c)[2];
|
|
151
|
-
}
|
|
152
|
-
function mix(a, b, c) {
|
|
153
|
-
a -= b;
|
|
154
|
-
a -= c;
|
|
155
|
-
a ^= c >>> 13;
|
|
156
|
-
b -= c;
|
|
157
|
-
b -= a;
|
|
158
|
-
b ^= a << 8;
|
|
159
|
-
c -= a;
|
|
160
|
-
c -= b;
|
|
161
|
-
c ^= b >>> 13;
|
|
162
|
-
a -= b;
|
|
163
|
-
a -= c;
|
|
164
|
-
a ^= c >>> 12;
|
|
165
|
-
b -= c;
|
|
166
|
-
b -= a;
|
|
167
|
-
b ^= a << 16;
|
|
168
|
-
c -= a;
|
|
169
|
-
c -= b;
|
|
170
|
-
c ^= b >>> 5;
|
|
171
|
-
a -= b;
|
|
172
|
-
a -= c;
|
|
173
|
-
a ^= c >>> 3;
|
|
174
|
-
b -= c;
|
|
175
|
-
b -= a;
|
|
176
|
-
b ^= a << 10;
|
|
177
|
-
c -= a;
|
|
178
|
-
c -= b;
|
|
179
|
-
c ^= b >>> 15;
|
|
180
|
-
return [a, b, c];
|
|
181
|
-
}
|
|
182
|
-
// Utils
|
|
183
|
-
var Endian;
|
|
184
|
-
(function (Endian) {
|
|
185
|
-
Endian[Endian["Little"] = 0] = "Little";
|
|
186
|
-
Endian[Endian["Big"] = 1] = "Big";
|
|
187
|
-
})(Endian || (Endian = {}));
|
|
188
|
-
|
|
189
|
-
// This module specifier is intentionally a relative path to allow bundling the code directly
|
|
190
|
-
// into the package.
|
|
191
|
-
// @ng_package: ignore-cross-repo-import
|
|
192
|
-
/**
|
|
193
|
-
* Parse a `$localize` tagged string into a structure that can be used for translation or
|
|
194
|
-
* extraction.
|
|
195
|
-
*
|
|
196
|
-
* See `ParsedMessage` for an example.
|
|
197
|
-
*/
|
|
198
|
-
function parseMessage(messageParts, expressions, location, messagePartLocations, expressionLocations = []) {
|
|
199
|
-
const substitutions = {};
|
|
200
|
-
const substitutionLocations = {};
|
|
201
|
-
const associatedMessageIds = {};
|
|
202
|
-
const metadata = parseMetadata(messageParts[0], messageParts.raw[0]);
|
|
203
|
-
const cleanedMessageParts = [metadata.text];
|
|
204
|
-
const placeholderNames = [];
|
|
205
|
-
let messageString = metadata.text;
|
|
206
|
-
for (let i = 1; i < messageParts.length; i++) {
|
|
207
|
-
const { messagePart, placeholderName = computePlaceholderName(i), associatedMessageId, } = parsePlaceholder(messageParts[i], messageParts.raw[i]);
|
|
208
|
-
messageString += `{$${placeholderName}}${messagePart}`;
|
|
209
|
-
if (expressions !== undefined) {
|
|
210
|
-
substitutions[placeholderName] = expressions[i - 1];
|
|
211
|
-
substitutionLocations[placeholderName] = expressionLocations[i - 1];
|
|
212
|
-
}
|
|
213
|
-
placeholderNames.push(placeholderName);
|
|
214
|
-
if (associatedMessageId !== undefined) {
|
|
215
|
-
associatedMessageIds[placeholderName] = associatedMessageId;
|
|
216
|
-
}
|
|
217
|
-
cleanedMessageParts.push(messagePart);
|
|
218
|
-
}
|
|
219
|
-
const messageId = metadata.customId || computeMsgId(messageString, metadata.meaning || '');
|
|
220
|
-
const legacyIds = metadata.legacyIds ? metadata.legacyIds.filter((id) => id !== messageId) : [];
|
|
221
|
-
return {
|
|
222
|
-
id: messageId,
|
|
223
|
-
legacyIds,
|
|
224
|
-
substitutions,
|
|
225
|
-
substitutionLocations,
|
|
226
|
-
text: messageString,
|
|
227
|
-
customId: metadata.customId,
|
|
228
|
-
meaning: metadata.meaning || '',
|
|
229
|
-
description: metadata.description || '',
|
|
230
|
-
messageParts: cleanedMessageParts,
|
|
231
|
-
messagePartLocations,
|
|
232
|
-
placeholderNames,
|
|
233
|
-
associatedMessageIds,
|
|
234
|
-
location,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Parse the given message part (`cooked` + `raw`) to extract the message metadata from the text.
|
|
239
|
-
*
|
|
240
|
-
* If the message part has a metadata block this function will extract the `meaning`,
|
|
241
|
-
* `description`, `customId` and `legacyId` (if provided) from the block. These metadata properties
|
|
242
|
-
* are serialized in the string delimited by `|`, `@@` and `␟` respectively.
|
|
243
|
-
*
|
|
244
|
-
* (Note that `␟` is the `LEGACY_ID_INDICATOR` - see `constants.ts`.)
|
|
245
|
-
*
|
|
246
|
-
* For example:
|
|
247
|
-
*
|
|
248
|
-
* ```ts
|
|
249
|
-
* `:meaning|description@@custom-id:`
|
|
250
|
-
* `:meaning|@@custom-id:`
|
|
251
|
-
* `:meaning|description:`
|
|
252
|
-
* `:description@@custom-id:`
|
|
253
|
-
* `:meaning|:`
|
|
254
|
-
* `:description:`
|
|
255
|
-
* `:@@custom-id:`
|
|
256
|
-
* `:meaning|description@@custom-id␟legacy-id-1␟legacy-id-2:`
|
|
257
|
-
* ```
|
|
258
|
-
*
|
|
259
|
-
* @param cooked The cooked version of the message part to parse.
|
|
260
|
-
* @param raw The raw version of the message part to parse.
|
|
261
|
-
* @returns A object containing any metadata that was parsed from the message part.
|
|
262
|
-
*/
|
|
263
|
-
function parseMetadata(cooked, raw) {
|
|
264
|
-
const { text: messageString, block } = splitBlock(cooked, raw);
|
|
265
|
-
if (block === undefined) {
|
|
266
|
-
return { text: messageString };
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
const [meaningDescAndId, ...legacyIds] = block.split(LEGACY_ID_INDICATOR);
|
|
270
|
-
const [meaningAndDesc, customId] = meaningDescAndId.split(ID_SEPARATOR, 2);
|
|
271
|
-
let [meaning, description] = meaningAndDesc.split(MEANING_SEPARATOR, 2);
|
|
272
|
-
if (description === undefined) {
|
|
273
|
-
description = meaning;
|
|
274
|
-
meaning = undefined;
|
|
275
|
-
}
|
|
276
|
-
if (description === '') {
|
|
277
|
-
description = undefined;
|
|
278
|
-
}
|
|
279
|
-
return { text: messageString, meaning, description, customId, legacyIds };
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Parse the given message part (`cooked` + `raw`) to extract any placeholder metadata from the
|
|
284
|
-
* text.
|
|
285
|
-
*
|
|
286
|
-
* If the message part has a metadata block this function will extract the `placeholderName` and
|
|
287
|
-
* `associatedMessageId` (if provided) from the block.
|
|
288
|
-
*
|
|
289
|
-
* These metadata properties are serialized in the string delimited by `@@`.
|
|
290
|
-
*
|
|
291
|
-
* For example:
|
|
292
|
-
*
|
|
293
|
-
* ```ts
|
|
294
|
-
* `:placeholder-name@@associated-id:`
|
|
295
|
-
* ```
|
|
296
|
-
*
|
|
297
|
-
* @param cooked The cooked version of the message part to parse.
|
|
298
|
-
* @param raw The raw version of the message part to parse.
|
|
299
|
-
* @returns A object containing the metadata (`placeholderName` and `associatedMessageId`) of the
|
|
300
|
-
* preceding placeholder, along with the static text that follows.
|
|
301
|
-
*/
|
|
302
|
-
function parsePlaceholder(cooked, raw) {
|
|
303
|
-
const { text: messagePart, block } = splitBlock(cooked, raw);
|
|
304
|
-
if (block === undefined) {
|
|
305
|
-
return { messagePart };
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
const [placeholderName, associatedMessageId] = block.split(ID_SEPARATOR);
|
|
309
|
-
return { messagePart, placeholderName, associatedMessageId };
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Split a message part (`cooked` + `raw`) into an optional delimited "block" off the front and the
|
|
314
|
-
* rest of the text of the message part.
|
|
315
|
-
*
|
|
316
|
-
* Blocks appear at the start of message parts. They are delimited by a colon `:` character at the
|
|
317
|
-
* start and end of the block.
|
|
318
|
-
*
|
|
319
|
-
* If the block is in the first message part then it will be metadata about the whole message:
|
|
320
|
-
* meaning, description, id. Otherwise it will be metadata about the immediately preceding
|
|
321
|
-
* substitution: placeholder name.
|
|
322
|
-
*
|
|
323
|
-
* Since blocks are optional, it is possible that the content of a message block actually starts
|
|
324
|
-
* with a block marker. In this case the marker must be escaped `\:`.
|
|
325
|
-
*
|
|
326
|
-
* @param cooked The cooked version of the message part to parse.
|
|
327
|
-
* @param raw The raw version of the message part to parse.
|
|
328
|
-
* @returns An object containing the `text` of the message part and the text of the `block`, if it
|
|
329
|
-
* exists.
|
|
330
|
-
* @throws an error if the `block` is unterminated
|
|
331
|
-
*/
|
|
332
|
-
function splitBlock(cooked, raw) {
|
|
333
|
-
if (raw.charAt(0) !== BLOCK_MARKER$1) {
|
|
334
|
-
return { text: cooked };
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
const endOfBlock = findEndOfBlock(cooked, raw);
|
|
338
|
-
return {
|
|
339
|
-
block: cooked.substring(1, endOfBlock),
|
|
340
|
-
text: cooked.substring(endOfBlock + 1),
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
function computePlaceholderName(index) {
|
|
345
|
-
return index === 1 ? 'PH' : `PH_${index - 1}`;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Find the end of a "marked block" indicated by the first non-escaped colon.
|
|
349
|
-
*
|
|
350
|
-
* @param cooked The cooked string (where escaped chars have been processed)
|
|
351
|
-
* @param raw The raw string (where escape sequences are still in place)
|
|
352
|
-
*
|
|
353
|
-
* @returns the index of the end of block marker
|
|
354
|
-
* @throws an error if the block is unterminated
|
|
355
|
-
*/
|
|
356
|
-
function findEndOfBlock(cooked, raw) {
|
|
357
|
-
for (let cookedIndex = 1, rawIndex = 1; cookedIndex < cooked.length; cookedIndex++, rawIndex++) {
|
|
358
|
-
if (raw[rawIndex] === '\\') {
|
|
359
|
-
rawIndex++;
|
|
360
|
-
}
|
|
361
|
-
else if (cooked[cookedIndex] === BLOCK_MARKER$1) {
|
|
362
|
-
return cookedIndex;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
throw new Error(`Unterminated $localize metadata block in "${raw}".`);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Tag a template literal string for localization.
|
|
370
|
-
*
|
|
371
|
-
* For example:
|
|
372
|
-
*
|
|
373
|
-
* ```ts
|
|
374
|
-
* $localize `some string to localize`
|
|
375
|
-
* ```
|
|
376
|
-
*
|
|
377
|
-
* **Providing meaning, description and id**
|
|
378
|
-
*
|
|
379
|
-
* You can optionally specify one or more of `meaning`, `description` and `id` for a localized
|
|
380
|
-
* string by pre-pending it with a colon delimited block of the form:
|
|
381
|
-
*
|
|
382
|
-
* ```ts
|
|
383
|
-
* $localize`:meaning|description@@id:source message text`;
|
|
384
|
-
*
|
|
385
|
-
* $localize`:meaning|:source message text`;
|
|
386
|
-
* $localize`:description:source message text`;
|
|
387
|
-
* $localize`:@@id:source message text`;
|
|
388
|
-
* ```
|
|
389
|
-
*
|
|
390
|
-
* This format is the same as that used for `i18n` markers in Angular templates. See the
|
|
391
|
-
* [Angular i18n guide](guide/i18n/prepare#mark-text-in-component-template).
|
|
392
|
-
*
|
|
393
|
-
* **Naming placeholders**
|
|
394
|
-
*
|
|
395
|
-
* If the template literal string contains expressions, then the expressions will be automatically
|
|
396
|
-
* associated with placeholder names for you.
|
|
397
|
-
*
|
|
398
|
-
* For example:
|
|
399
|
-
*
|
|
400
|
-
* ```ts
|
|
401
|
-
* $localize `Hi ${name}! There are ${items.length} items.`;
|
|
402
|
-
* ```
|
|
403
|
-
*
|
|
404
|
-
* will generate a message-source of `Hi {$PH}! There are {$PH_1} items`.
|
|
405
|
-
*
|
|
406
|
-
* The recommended practice is to name the placeholder associated with each expression though.
|
|
407
|
-
*
|
|
408
|
-
* Do this by providing the placeholder name wrapped in `:` characters directly after the
|
|
409
|
-
* expression. These placeholder names are stripped out of the rendered localized string.
|
|
410
|
-
*
|
|
411
|
-
* For example, to name the `items.length` expression placeholder `itemCount` you write:
|
|
412
|
-
*
|
|
413
|
-
* ```ts
|
|
414
|
-
* $localize `There are ${items.length}:itemCount: items`;
|
|
415
|
-
* ```
|
|
416
|
-
*
|
|
417
|
-
* **Escaping colon markers**
|
|
418
|
-
*
|
|
419
|
-
* If you need to use a `:` character directly at the start of a tagged string that has no
|
|
420
|
-
* metadata block, or directly after a substitution expression that has no name you must escape
|
|
421
|
-
* the `:` by preceding it with a backslash:
|
|
422
|
-
*
|
|
423
|
-
* For example:
|
|
424
|
-
*
|
|
425
|
-
* ```ts
|
|
426
|
-
* // message has a metadata block so no need to escape colon
|
|
427
|
-
* $localize `:some description::this message starts with a colon (:)`;
|
|
428
|
-
* // no metadata block so the colon must be escaped
|
|
429
|
-
* $localize `\:this message starts with a colon (:)`;
|
|
430
|
-
* ```
|
|
431
|
-
*
|
|
432
|
-
* ```ts
|
|
433
|
-
* // named substitution so no need to escape colon
|
|
434
|
-
* $localize `${label}:label:: ${}`
|
|
435
|
-
* // anonymous substitution so colon must be escaped
|
|
436
|
-
* $localize `${label}\: ${}`
|
|
437
|
-
* ```
|
|
438
|
-
*
|
|
439
|
-
* **Processing localized strings:**
|
|
440
|
-
*
|
|
441
|
-
* There are three scenarios:
|
|
442
|
-
*
|
|
443
|
-
* * **compile-time inlining**: the `$localize` tag is transformed at compile time by a
|
|
444
|
-
* transpiler, removing the tag and replacing the template literal string with a translated
|
|
445
|
-
* literal string from a collection of translations provided to the transpilation tool.
|
|
446
|
-
*
|
|
447
|
-
* * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and
|
|
448
|
-
* reorders the parts (static strings and expressions) of the template literal string with strings
|
|
449
|
-
* from a collection of translations loaded at run-time.
|
|
450
|
-
*
|
|
451
|
-
* * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates
|
|
452
|
-
* the original template literal string without applying any translations to the parts. This
|
|
453
|
-
* version is used during development or where there is no need to translate the localized
|
|
454
|
-
* template literals.
|
|
455
|
-
*
|
|
456
|
-
* @param messageParts a collection of the static parts of the template string.
|
|
457
|
-
* @param expressions a collection of the values of each placeholder in the template string.
|
|
458
|
-
* @returns the translated string, with the `messageParts` and `expressions` interleaved together.
|
|
459
|
-
*
|
|
460
|
-
* @publicApi
|
|
461
|
-
*/
|
|
462
|
-
const $localize = function (messageParts, ...expressions) {
|
|
463
|
-
if ($localize.translate) {
|
|
464
|
-
// Don't use array expansion here to avoid the compiler adding `__read()` helper unnecessarily.
|
|
465
|
-
const translation = $localize.translate(messageParts, expressions);
|
|
466
|
-
messageParts = translation[0];
|
|
467
|
-
expressions = translation[1];
|
|
468
|
-
}
|
|
469
|
-
let message = stripBlock(messageParts[0], messageParts.raw[0]);
|
|
470
|
-
for (let i = 1; i < messageParts.length; i++) {
|
|
471
|
-
message += expressions[i - 1] + stripBlock(messageParts[i], messageParts.raw[i]);
|
|
472
|
-
}
|
|
473
|
-
return message;
|
|
474
|
-
};
|
|
475
|
-
const BLOCK_MARKER = ':';
|
|
476
|
-
/**
|
|
477
|
-
* Strip a delimited "block" from the start of the `messagePart`, if it is found.
|
|
478
|
-
*
|
|
479
|
-
* If a marker character (:) actually appears in the content at the start of a tagged string or
|
|
480
|
-
* after a substitution expression, where a block has not been provided the character must be
|
|
481
|
-
* escaped with a backslash, `\:`. This function checks for this by looking at the `raw`
|
|
482
|
-
* messagePart, which should still contain the backslash.
|
|
483
|
-
*
|
|
484
|
-
* @param messagePart The cooked message part to process.
|
|
485
|
-
* @param rawMessagePart The raw message part to check.
|
|
486
|
-
* @returns the message part with the placeholder name stripped, if found.
|
|
487
|
-
* @throws an error if the block is unterminated
|
|
488
|
-
*/
|
|
489
|
-
function stripBlock(messagePart, rawMessagePart) {
|
|
490
|
-
return rawMessagePart.charAt(0) === BLOCK_MARKER
|
|
491
|
-
? messagePart.substring(findEndOfBlock(messagePart, rawMessagePart) + 1)
|
|
492
|
-
: messagePart;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
export { $localize, BLOCK_MARKER$1 as BLOCK_MARKER, computeMsgId, findEndOfBlock, parseMessage, parseMetadata, splitBlock };
|
|
496
|
-
//# sourceMappingURL=localize2.mjs.map
|