MCPcopy
hub / github.com/angular/angular / parseMessage

Function parseMessage

packages/localize/src/utils/src/messages.ts:171–219  ·  view source on GitHub ↗
(
  messageParts: TemplateStringsArray,
  expressions?: readonly any[],
  location?: SourceLocation,
  messagePartLocations?: (SourceLocation | undefined)[],
  expressionLocations: (SourceLocation | undefined)[] = [],
)

Source from the content-addressed store, hash-verified

169 * See `ParsedMessage` for an example.
170 */
171export function parseMessage(
172 messageParts: TemplateStringsArray,
173 expressions?: readonly any[],
174 location?: SourceLocation,
175 messagePartLocations?: (SourceLocation | undefined)[],
176 expressionLocations: (SourceLocation | undefined)[] = [],
177): ParsedMessage {
178 const substitutions: {[placeholderName: string]: any} = {};
179 const substitutionLocations: {[placeholderName: string]: SourceLocation | undefined} = {};
180 const associatedMessageIds: {[placeholderName: string]: MessageId} = {};
181 const metadata = parseMetadata(messageParts[0], messageParts.raw[0]);
182 const cleanedMessageParts: string[] = [metadata.text];
183 const placeholderNames: string[] = [];
184 let messageString = metadata.text;
185 for (let i = 1; i < messageParts.length; i++) {
186 const {
187 messagePart,
188 placeholderName = computePlaceholderName(i),
189 associatedMessageId,
190 } = parsePlaceholder(messageParts[i], messageParts.raw[i]);
191 messageString += `{$${placeholderName}}${messagePart}`;
192 if (expressions !== undefined) {
193 substitutions[placeholderName] = expressions[i - 1];
194 substitutionLocations[placeholderName] = expressionLocations[i - 1];
195 }
196 placeholderNames.push(placeholderName);
197 if (associatedMessageId !== undefined) {
198 associatedMessageIds[placeholderName] = associatedMessageId;
199 }
200 cleanedMessageParts.push(messagePart);
201 }
202 const messageId = metadata.customId || computeMsgId(messageString, metadata.meaning || '');
203 const legacyIds = metadata.legacyIds ? metadata.legacyIds.filter((id) => id !== messageId) : [];
204 return {
205 id: messageId,
206 legacyIds,
207 substitutions,
208 substitutionLocations,
209 text: messageString,
210 customId: metadata.customId,
211 meaning: metadata.meaning || '',
212 description: metadata.description || '',
213 messageParts: cleanedMessageParts,
214 messagePartLocations,
215 placeholderNames,
216 associatedMessageIds,
217 location,
218 };
219}
220
221/**
222 * Parse the given message part (`cooked` + `raw`) to extract the message metadata from the text.

Callers 2

translateFunction · 0.90
messages_spec.tsFile · 0.85

Calls 6

computeMsgIdFunction · 0.90
parseMetadataFunction · 0.85
computePlaceholderNameFunction · 0.85
parsePlaceholderFunction · 0.85
pushMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…