2016-08-11 21:00:35 -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
|
|
|
|
*/
|
|
|
|
|
2016-08-30 18:07:40 -07:00
|
|
|
import {HtmlParser} from '../ml_parser/html_parser';
|
2016-08-11 21:00:35 -07:00
|
|
|
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../ml_parser/interpolation_config';
|
|
|
|
import {ParseTreeResult} from '../ml_parser/parser';
|
|
|
|
|
|
|
|
import {mergeTranslations} from './extractor_merger';
|
2016-08-12 22:13:54 -07:00
|
|
|
import {Serializer} from './serializers/serializer';
|
|
|
|
import {Xliff} from './serializers/xliff';
|
|
|
|
import {Xmb} from './serializers/xmb';
|
2016-08-11 21:00:35 -07:00
|
|
|
import {Xtb} from './serializers/xtb';
|
|
|
|
import {TranslationBundle} from './translation_bundle';
|
|
|
|
|
2016-08-30 18:07:40 -07:00
|
|
|
export class I18NHtmlParser implements HtmlParser {
|
2016-08-11 21:00:35 -07:00
|
|
|
// @override
|
2016-08-12 22:13:54 -07:00
|
|
|
getTagDefinition: any;
|
2016-08-11 21:00:35 -07:00
|
|
|
|
|
|
|
// TODO(vicb): transB.load() should not need a msgB & add transB.resolve(msgB,
|
|
|
|
// interpolationConfig)
|
|
|
|
// TODO(vicb): remove the interpolationConfig from the Xtb serializer
|
2016-08-12 22:13:54 -07:00
|
|
|
constructor(
|
2016-08-30 18:07:40 -07:00
|
|
|
private _htmlParser: HtmlParser, private _translations?: string,
|
2016-08-12 22:13:54 -07:00
|
|
|
private _translationsFormat?: string) {}
|
2016-08-11 21:00:35 -07:00
|
|
|
|
|
|
|
parse(
|
|
|
|
source: string, url: string, parseExpansionForms: boolean = false,
|
|
|
|
interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): ParseTreeResult {
|
|
|
|
const parseResult =
|
|
|
|
this._htmlParser.parse(source, url, parseExpansionForms, interpolationConfig);
|
|
|
|
|
|
|
|
if (!this._translations || this._translations === '') {
|
|
|
|
// Do not enable i18n when no translation bundle is provided
|
|
|
|
return parseResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(vicb): add support for implicit tags / attributes
|
|
|
|
|
2016-11-02 17:40:15 -07:00
|
|
|
if (parseResult.errors.length) {
|
|
|
|
return new ParseTreeResult(parseResult.rootNodes, parseResult.errors);
|
2016-08-11 21:00:35 -07:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:40:15 -07:00
|
|
|
const serializer = this._createSerializer();
|
|
|
|
const translationBundle = TranslationBundle.load(this._translations, url, serializer);
|
2016-08-11 21:00:35 -07:00
|
|
|
|
2016-08-11 15:31:36 -07:00
|
|
|
return mergeTranslations(parseResult.rootNodes, translationBundle, interpolationConfig, [], {});
|
2016-08-11 21:00:35 -07:00
|
|
|
}
|
2016-08-12 22:13:54 -07:00
|
|
|
|
2016-11-02 17:40:15 -07:00
|
|
|
private _createSerializer(): Serializer {
|
2016-08-12 22:13:54 -07:00
|
|
|
const format = (this._translationsFormat || 'xlf').toLowerCase();
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case 'xmb':
|
|
|
|
return new Xmb();
|
|
|
|
case 'xtb':
|
2016-11-02 17:40:15 -07:00
|
|
|
return new Xtb();
|
2016-08-12 22:13:54 -07:00
|
|
|
case 'xliff':
|
|
|
|
case 'xlf':
|
|
|
|
default:
|
2016-11-02 17:40:15 -07:00
|
|
|
return new Xliff();
|
2016-08-12 22:13:54 -07:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 18:07:40 -07:00
|
|
|
}
|