refactor(compiler): i18n - render legacy i18n message ids (#34135)

Now that `@angular/localize` can interpret multiple legacy message ids in the
metablock of a `$localize` tagged template string, this commit adds those
ids to each i18n message extracted from component templates, but only if
the `enableI18nLegacyMessageIdFormat` is not `false`.

PR Close #34135
This commit is contained in:
Pete Bacon Darwin
2019-12-03 08:36:38 +00:00
committed by Miško Hevery
parent 8e96b450e2
commit e524322c43
12 changed files with 100 additions and 124 deletions

View File

@ -18,7 +18,7 @@ import {I18N_ATTR, I18N_ATTR_PREFIX, hasI18nAttrs, icuFromI18nMessage} from './u
export type I18nMeta = {
id?: string,
customId?: string,
legacyId?: string,
legacyIds?: string[],
description?: string,
meaning?: string
};
@ -52,7 +52,7 @@ export class I18nMetaVisitor implements html.Visitor {
constructor(
private interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG,
private keepI18nAttrs: boolean = false, private i18nLegacyMessageIdFormat: string = '') {}
private keepI18nAttrs = false, private enableI18nLegacyMessageIdFormat = false) {}
private _generateI18nMessage(
nodes: html.Node[], meta: string|i18n.I18nMeta = '',
@ -60,7 +60,7 @@ export class I18nMetaVisitor implements html.Visitor {
const {meaning, description, customId} = this._parseMetadata(meta);
const message = this._createI18nMessage(nodes, meaning, description, customId, visitNodeFn);
this._setMessageId(message, meta);
this._setLegacyId(message, meta);
this._setLegacyIds(message, meta);
return message;
}
@ -171,13 +171,9 @@ export class I18nMetaVisitor implements html.Visitor {
* @param message the message whose legacy id should be set
* @param meta information about the message being processed
*/
private _setLegacyId(message: i18n.Message, meta: string|i18n.I18nMeta): void {
if (this.i18nLegacyMessageIdFormat === 'xlf' || this.i18nLegacyMessageIdFormat === 'xliff') {
message.legacyId = computeDigest(message);
} else if (
this.i18nLegacyMessageIdFormat === 'xlf2' || this.i18nLegacyMessageIdFormat === 'xliff2' ||
this.i18nLegacyMessageIdFormat === 'xmb') {
message.legacyId = computeDecimalDigest(message);
private _setLegacyIds(message: i18n.Message, meta: string|i18n.I18nMeta): void {
if (this.enableI18nLegacyMessageIdFormat) {
message.legacyIds = [computeDigest(message), computeDecimalDigest(message)];
} else if (typeof meta !== 'string') {
// This occurs if we are doing the 2nd pass after whitespace removal (see `parseTemplate()` in
// `packages/compiler/src/render3/view/template.ts`).
@ -186,7 +182,7 @@ export class I18nMetaVisitor implements html.Visitor {
const previousMessage = meta instanceof i18n.Message ?
meta :
meta instanceof i18n.IcuPlaceholder ? meta.previousMessage : undefined;
message.legacyId = previousMessage && previousMessage.legacyId;
message.legacyIds = previousMessage ? previousMessage.legacyIds : [];
}
}
}
@ -195,7 +191,7 @@ export function metaFromI18nMessage(message: i18n.Message, id: string | null = n
return {
id: typeof id === 'string' ? id : message.id || '',
customId: message.customId,
legacyId: message.legacyId,
legacyIds: message.legacyIds,
meaning: message.meaning || '',
description: message.description || ''
};

View File

@ -1945,17 +1945,14 @@ export interface ParseTemplateOptions {
leadingTriviaChars?: string[];
/**
* Render `$localize` message ids with the specified legacy format (xlf, xlf2 or xmb).
* Render `$localize` message ids with additional legacy message ids.
*
* Use this option when use are using the `$localize` based localization messages but
* have not migrated the translation files to use the new `$localize` message id format.
* This option defaults to `true` but in the future the defaul will be flipped.
*
* @deprecated
* `i18nLegacyMessageIdFormat` should only be used while migrating from legacy message id
* formatted translation files and will be removed at the same time as ViewEngine support is
* removed.
* For now set this option to false if you have migrated the translation files to use the new
* `$localize` message id format and you are not using compile time translation merging.
*/
i18nLegacyMessageIdFormat?: string;
enableI18nLegacyMessageIdFormat?: boolean;
}
/**
@ -1968,7 +1965,7 @@ export interface ParseTemplateOptions {
export function parseTemplate(
template: string, templateUrl: string, options: ParseTemplateOptions = {}):
{errors?: ParseError[], nodes: t.Node[], styleUrls: string[], styles: string[]} {
const {interpolationConfig, preserveWhitespaces, i18nLegacyMessageIdFormat} = options;
const {interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat} = options;
const bindingParser = makeBindingParser(interpolationConfig);
const htmlParser = new HtmlParser();
const parseResult = htmlParser.parse(
@ -1986,7 +1983,8 @@ export function parseTemplate(
// extraction process (ng xi18n) relies on a raw content to generate
// message ids
const i18nMetaVisitor = new I18nMetaVisitor(
interpolationConfig, /* keepI18nAttrs */ !preserveWhitespaces, i18nLegacyMessageIdFormat);
interpolationConfig, /* keepI18nAttrs */ !preserveWhitespaces,
enableI18nLegacyMessageIdFormat);
rootNodes = html.visitAll(i18nMetaVisitor, rootNodes);
if (!preserveWhitespaces) {