51 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-07-15 09:42:33 -07:00
/**
* @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 {HtmlParser} from '../ml_parser/html_parser';
import {InterpolationConfig} from '../ml_parser/interpolation_config';
2016-07-21 13:56:58 -07:00
import {ParseError} from '../parse_util';
2016-07-15 09:42:33 -07:00
2016-08-05 12:08:43 -07:00
import {digestMessage} from './digest';
import {extractMessages} from './extractor_merger';
import {Message} from './i18n_ast';
2016-07-15 09:42:33 -07:00
import {Serializer} from './serializers/serializer';
2016-07-21 13:56:58 -07:00
/**
* A container for message extracted from the templates.
*/
export class MessageBundle {
2016-08-05 12:08:43 -07:00
private _messageMap: {[id: string]: Message} = {};
2016-07-15 09:42:33 -07:00
constructor(
private _htmlParser: HtmlParser, private _implicitTags: string[],
private _implicitAttrs: {[k: string]: string[]}) {}
2016-07-21 13:56:58 -07:00
updateFromTemplate(html: string, url: string, interpolationConfig: InterpolationConfig):
ParseError[] {
2016-07-15 09:42:33 -07:00
const htmlParserResult = this._htmlParser.parse(html, url, true, interpolationConfig);
if (htmlParserResult.errors.length) {
2016-07-21 13:56:58 -07:00
return htmlParserResult.errors;
2016-07-15 09:42:33 -07:00
}
2016-08-05 12:08:43 -07:00
const i18nParserResult = extractMessages(
2016-07-15 09:42:33 -07:00
htmlParserResult.rootNodes, interpolationConfig, this._implicitTags, this._implicitAttrs);
if (i18nParserResult.errors.length) {
return i18nParserResult.errors;
}
i18nParserResult.messages.forEach(
(message) => { this._messageMap[digestMessage(message)] = message; });
2016-07-15 09:42:33 -07:00
}
getMessageMap(): {[id: string]: Message} { return this._messageMap; }
2016-07-21 13:56:58 -07:00
write(serializer: Serializer): string { return serializer.write(this._messageMap); }
2016-07-15 09:42:33 -07:00
}