/
serialize_translation_message.ts
32 lines (29 loc) 路 1.2 KB
/
serialize_translation_message.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
/**
* @license
* Copyright Google LLC 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 {Element, ParseError} from '@angular/compiler';
import {傻ParsedTranslation} from '@angular/localize';
import {MessageSerializer, MessageSerializerConfig} from '../message_serialization/message_serializer';
import {TargetMessageRenderer} from '../message_serialization/target_message_renderer';
import {parseInnerRange} from './translation_utils';
/**
* Serialize the given `element` into a parsed translation using the given `serializer`.
*/
export function serializeTranslationMessage(element: Element, config: MessageSerializerConfig): {
translation: 傻ParsedTranslation|null,
parseErrors: ParseError[],
serializeErrors: ParseError[]
} {
const {rootNodes, errors: parseErrors} = parseInnerRange(element);
try {
const serializer = new MessageSerializer(new TargetMessageRenderer(), config);
const translation = serializer.serialize(rootNodes);
return {translation, parseErrors, serializeErrors: []};
} catch (e) {
return {translation: null, parseErrors, serializeErrors: [e]};
}
}