refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
70
packages/compiler/src/i18n/i18n_html_parser.ts
Normal file
70
packages/compiler/src/i18n/i18n_html_parser.ts
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @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 {MissingTranslationStrategy, ɵConsole as Console} from '@angular/core';
|
||||
import {HtmlParser} from '../ml_parser/html_parser';
|
||||
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../ml_parser/interpolation_config';
|
||||
import {ParseTreeResult} from '../ml_parser/parser';
|
||||
import {mergeTranslations} from './extractor_merger';
|
||||
import {Serializer} from './serializers/serializer';
|
||||
import {Xliff} from './serializers/xliff';
|
||||
import {Xmb} from './serializers/xmb';
|
||||
import {Xtb} from './serializers/xtb';
|
||||
import {TranslationBundle} from './translation_bundle';
|
||||
|
||||
export class I18NHtmlParser implements HtmlParser {
|
||||
// @override
|
||||
getTagDefinition: any;
|
||||
|
||||
private _translationBundle: TranslationBundle;
|
||||
|
||||
constructor(
|
||||
private _htmlParser: HtmlParser, translations?: string, translationsFormat?: string,
|
||||
missingTranslation: MissingTranslationStrategy = MissingTranslationStrategy.Warning,
|
||||
console?: Console) {
|
||||
if (translations) {
|
||||
const serializer = createSerializer(translationsFormat);
|
||||
this._translationBundle =
|
||||
TranslationBundle.load(translations, 'i18n', serializer, missingTranslation, console);
|
||||
}
|
||||
}
|
||||
|
||||
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._translationBundle) {
|
||||
// Do not enable i18n when no translation bundle is provided
|
||||
return parseResult;
|
||||
}
|
||||
|
||||
if (parseResult.errors.length) {
|
||||
return new ParseTreeResult(parseResult.rootNodes, parseResult.errors);
|
||||
}
|
||||
|
||||
return mergeTranslations(
|
||||
parseResult.rootNodes, this._translationBundle, interpolationConfig, [], {});
|
||||
}
|
||||
}
|
||||
|
||||
function createSerializer(format?: string): Serializer {
|
||||
format = (format || 'xlf').toLowerCase();
|
||||
|
||||
switch (format) {
|
||||
case 'xmb':
|
||||
return new Xmb();
|
||||
case 'xtb':
|
||||
return new Xtb();
|
||||
case 'xliff':
|
||||
case 'xlf':
|
||||
default:
|
||||
return new Xliff();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user