MCPcopy Index your code
hub / github.com/angular/angular / translate

Function translate

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

Source from the content-addressed store, hash-verified

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…