UNPKG

8.32 kBTypeScriptView Raw
1/**
2 * Re-export this helper function so that users of `@angular/localize` don't need to actively import
3 * from `@angular/compiler`.
4 */
5export { computeMsgId } from '@angular/compiler';
6/**
7 * A string containing a translation source message.
8 *
9 * I.E. the message that indicates what will be translated from.
10 *
11 * Uses `{$placeholder-name}` to indicate a placeholder.
12 */
13export declare type SourceMessage = string;
14/**
15 * A string containing a translation target message.
16 *
17 * I.E. the message that indicates what will be translated to.
18 *
19 * Uses `{$placeholder-name}` to indicate a placeholder.
20 */
21export declare type TargetMessage = string;
22/**
23 * A string that uniquely identifies a message, to be used for matching translations.
24 */
25export declare type MessageId = string;
26/**
27 * Declares a copy of the `AbsoluteFsPath` branded type in `@angular/compiler-cli` to avoid an
28 * import into `@angular/compiler-cli`. The compiler-cli's declaration files are not necessarily
29 * compatible with web environments that use `@angular/localize`, and would inadvertently include
30 * `typescript` declaration files in any compilation unit that uses `@angular/localize` (which
31 * increases parsing time and memory usage during builds) using a default import that only
32 * type-checks when `allowSyntheticDefaultImports` is enabled.
33 *
34 * @see https://github.com/angular/angular/issues/45179
35 */
36declare type AbsoluteFsPathLocalizeCopy = string & {
37 _brand: 'AbsoluteFsPath';
38};
39/**
40 * The location of the message in the source file.
41 *
42 * The `line` and `column` values for the `start` and `end` properties are zero-based.
43 */
44export interface SourceLocation {
45 start: {
46 line: number;
47 column: number;
48 };
49 end: {
50 line: number;
51 column: number;
52 };
53 file: AbsoluteFsPathLocalizeCopy;
54 text?: string;
55}
56/**
57 * Additional information that can be associated with a message.
58 */
59export interface MessageMetadata {
60 /**
61 * A human readable rendering of the message
62 */
63 text: string;
64 /**
65 * Legacy message ids, if provided.
66 *
67 * In legacy message formats the message id can only be computed directly from the original
68 * template source.
69 *
70 * Since this information is not available in `$localize` calls, the legacy message ids may be
71 * attached by the compiler to the `$localize` metablock so it can be used if needed at the point
72 * of translation if the translations are encoded using the legacy message id.
73 */
74 legacyIds?: string[];
75 /**
76 * The id of the `message` if a custom one was specified explicitly.
77 *
78 * This id overrides any computed or legacy ids.
79 */
80 customId?: string;
81 /**
82 * The meaning of the `message`, used to distinguish identical `messageString`s.
83 */
84 meaning?: string;
85 /**
86 * The description of the `message`, used to aid translation.
87 */
88 description?: string;
89 /**
90 * The location of the message in the source.
91 */
92 location?: SourceLocation;
93}
94/**
95 * Information parsed from a `$localize` tagged string that is used to translate it.
96 *
97 * For example:
98 *
99 * ```
100 * const name = 'Jo Bloggs';
101 * $localize`Hello ${name}:title@@ID:!`;
102 * ```
103 *
104 * May be parsed into:
105 *
106 * ```
107 * {
108 * id: '6998194507597730591',
109 * substitutions: { title: 'Jo Bloggs' },
110 * messageString: 'Hello {$title}!',
111 * placeholderNames: ['title'],
112 * associatedMessageIds: { title: 'ID' },
113 * }
114 * ```
115 */
116export interface ParsedMessage extends MessageMetadata {
117 /**
118 * The key used to look up the appropriate translation target.
119 */
120 id: MessageId;
121 /**
122 * A mapping of placeholder names to substitution values.
123 */
124 substitutions: Record<string, any>;
125 /**
126 * An optional mapping of placeholder names to associated MessageIds.
127 * This can be used to match ICU placeholders to the message that contains the ICU.
128 */
129 associatedMessageIds?: Record<string, MessageId>;
130 /**
131 * An optional mapping of placeholder names to source locations
132 */
133 substitutionLocations?: Record<string, SourceLocation | undefined>;
134 /**
135 * The static parts of the message.
136 */
137 messageParts: string[];
138 /**
139 * An optional mapping of message parts to source locations
140 */
141 messagePartLocations?: (SourceLocation | undefined)[];
142 /**
143 * The names of the placeholders that will be replaced with substitutions.
144 */
145 placeholderNames: string[];
146}
147/**
148 * Parse a `$localize` tagged string into a structure that can be used for translation or
149 * extraction.
150 *
151 * See `ParsedMessage` for an example.
152 */
153export declare function parseMessage(messageParts: TemplateStringsArray, expressions?: readonly any[], location?: SourceLocation, messagePartLocations?: (SourceLocation | undefined)[], expressionLocations?: (SourceLocation | undefined)[]): ParsedMessage;
154/**
155 * Parse the given message part (`cooked` + `raw`) to extract the message metadata from the text.
156 *
157 * If the message part has a metadata block this function will extract the `meaning`,
158 * `description`, `customId` and `legacyId` (if provided) from the block. These metadata properties
159 * are serialized in the string delimited by `|`, `@@` and `␟` respectively.
160 *
161 * (Note that `␟` is the `LEGACY_ID_INDICATOR` - see `constants.ts`.)
162 *
163 * For example:
164 *
165 * ```ts
166 * `:meaning|description@@custom-id:`
167 * `:meaning|@@custom-id:`
168 * `:meaning|description:`
169 * `:description@@custom-id:`
170 * `:meaning|:`
171 * `:description:`
172 * `:@@custom-id:`
173 * `:meaning|description@@custom-id␟legacy-id-1␟legacy-id-2:`
174 * ```
175 *
176 * @param cooked The cooked version of the message part to parse.
177 * @param raw The raw version of the message part to parse.
178 * @returns A object containing any metadata that was parsed from the message part.
179 */
180export declare function parseMetadata(cooked: string, raw: string): MessageMetadata;
181/**
182 * Parse the given message part (`cooked` + `raw`) to extract any placeholder metadata from the
183 * text.
184 *
185 * If the message part has a metadata block this function will extract the `placeholderName` and
186 * `associatedMessageId` (if provided) from the block.
187 *
188 * These metadata properties are serialized in the string delimited by `@@`.
189 *
190 * For example:
191 *
192 * ```ts
193 * `:placeholder-name@@associated-id:`
194 * ```
195 *
196 * @param cooked The cooked version of the message part to parse.
197 * @param raw The raw version of the message part to parse.
198 * @returns A object containing the metadata (`placeholderName` and `associatedMesssageId`) of the
199 * preceding placeholder, along with the static text that follows.
200 */
201export declare function parsePlaceholder(cooked: string, raw: string): {
202 messagePart: string;
203 placeholderName?: string;
204 associatedMessageId?: string;
205};
206/**
207 * Split a message part (`cooked` + `raw`) into an optional delimited "block" off the front and the
208 * rest of the text of the message part.
209 *
210 * Blocks appear at the start of message parts. They are delimited by a colon `:` character at the
211 * start and end of the block.
212 *
213 * If the block is in the first message part then it will be metadata about the whole message:
214 * meaning, description, id. Otherwise it will be metadata about the immediately preceding
215 * substitution: placeholder name.
216 *
217 * Since blocks are optional, it is possible that the content of a message block actually starts
218 * with a block marker. In this case the marker must be escaped `\:`.
219 *
220 * @param cooked The cooked version of the message part to parse.
221 * @param raw The raw version of the message part to parse.
222 * @returns An object containing the `text` of the message part and the text of the `block`, if it
223 * exists.
224 * @throws an error if the `block` is unterminated
225 */
226export declare function splitBlock(cooked: string, raw: string): {
227 text: string;
228 block?: string;
229};
230/**
231 * Find the end of a "marked block" indicated by the first non-escaped colon.
232 *
233 * @param cooked The cooked string (where escaped chars have been processed)
234 * @param raw The raw string (where escape sequences are still in place)
235 *
236 * @returns the index of the end of block marker
237 * @throws an error if the block is unterminated
238 */
239export declare function findEndOfBlock(cooked: string, raw: string): number;