MCPcopy Create free account
hub / github.com/angular/angular / translate

Function translate

packages/localize/src/utils/src/translations.ts:55–87  ·  view source on GitHub ↗
(
  translations: Record<string, ParsedTranslation>,
  messageParts: TemplateStringsArray,
  substitutions: readonly unknown[],
)

Source from the content-addressed store, hash-verified

53 * error is thrown.
54 */
55export function translate(
56 translations: Record<string, ParsedTranslation>,
57 messageParts: TemplateStringsArray,
58 substitutions: readonly unknown[],
59): [TemplateStringsArray, readonly unknown[]] {
60 const message = parseMessage(messageParts, substitutions);
61 // Look up the translation using the messageId, and then the legacyId if available.
62 let translation = translations[message.id];
63 // If the messageId did not match a translation, try matching the legacy ids instead
64 if (message.legacyIds !== undefined) {
65 for (let i = 0; i < message.legacyIds.length && translation === undefined; i++) {
66 translation = translations[message.legacyIds[i]];
67 }
68 }
69 if (translation === undefined) {
70 throw new MissingTranslationError(message);
71 }
72 return [
73 translation.messageParts,
74 translation.placeholderNames.map((placeholder) => {
75 if (Object.hasOwn(message.substitutions, placeholder)) {
76 return message.substitutions[placeholder];
77 } else {
78 throw new Error(
79 `There is a placeholder name mismatch with the translation provided for the message ${describeMessage(
80 message,
81 )}.\n` +
82 `The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`,
83 );
84 }
85 }),
86 ];
87}
88
89/**
90 * Parse the `messageParts` and `placeholderNames` out of a target `message`.

Callers 1

doTranslateFunction · 0.50

Calls 3

parseMessageFunction · 0.90
describeMessageFunction · 0.85
mapMethod · 0.80

Tested by

no test coverage detected