-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
target_message_renderer.ts
56 lines (53 loc) 路 1.71 KB
/
target_message_renderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {傻ParsedTranslation, 傻makeParsedTranslation} from '@angular/localize';
import {MessageRenderer} from './message_renderer';
/**
* A message renderer that outputs `傻ParsedTranslation` objects.
*/
export class TargetMessageRenderer implements MessageRenderer<傻ParsedTranslation> {
private current: MessageInfo = {messageParts: [], placeholderNames: [], text: ''};
private icuDepth = 0;
get message(): 傻ParsedTranslation {
const {messageParts, placeholderNames} = this.current;
return 傻makeParsedTranslation(messageParts, placeholderNames);
}
startRender(): void {}
endRender(): void { this.storeMessagePart(); }
text(text: string): void { this.current.text += text; }
placeholder(name: string, body: string|undefined): void { this.renderPlaceholder(name); }
startPlaceholder(name: string): void { this.renderPlaceholder(name); }
closePlaceholder(name: string): void { this.renderPlaceholder(name); }
startContainer(): void {}
closeContainer(): void {}
startIcu(): void {
this.icuDepth++;
this.text('{');
}
endIcu(): void {
this.icuDepth--;
this.text('}');
}
private renderPlaceholder(name: string) {
if (this.icuDepth > 0) {
this.text(`{${name}}`);
} else {
this.storeMessagePart();
this.current.placeholderNames.push(name);
}
}
private storeMessagePart() {
this.current.messageParts.push(this.current.text);
this.current.text = '';
}
}
interface MessageInfo {
messageParts: string[];
placeholderNames: string[];
text: string;
}