refactor(ivy): i18n - rename I18nError
to TranslationParserError
(#32881)
This closer reflects what caused the error. PR Close #32881
This commit is contained in:
parent
2cdb3a079d
commit
90855f331f
@ -8,10 +8,9 @@
|
|||||||
import {ParseErrorLevel, ParseSourceSpan} from '@angular/compiler';
|
import {ParseErrorLevel, ParseSourceSpan} from '@angular/compiler';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instances of this class are thrown when there is an error in the source code that is being
|
* This error is thrown when there is a problem parsing a translation file.
|
||||||
* translated.
|
|
||||||
*/
|
*/
|
||||||
export class I18nError extends Error {
|
export class TranslationParseError extends Error {
|
||||||
constructor(
|
constructor(
|
||||||
public span: ParseSourceSpan, public msg: string,
|
public span: ParseSourceSpan, public msg: string,
|
||||||
public level: ParseErrorLevel = ParseErrorLevel.ERROR) {
|
public level: ParseErrorLevel = ParseErrorLevel.ERROR) {
|
@ -6,12 +6,13 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import {Element, LexerRange, Node, XmlParser} from '@angular/compiler';
|
import {Element, LexerRange, Node, XmlParser} from '@angular/compiler';
|
||||||
import {I18nError} from './i18n_error';
|
import {TranslationParseError} from './translation_parse_error';
|
||||||
|
|
||||||
export function getAttrOrThrow(element: Element, attrName: string): string {
|
export function getAttrOrThrow(element: Element, attrName: string): string {
|
||||||
const attrValue = getAttribute(element, attrName);
|
const attrValue = getAttribute(element, attrName);
|
||||||
if (attrValue === undefined) {
|
if (attrValue === undefined) {
|
||||||
throw new I18nError(element.sourceSpan, `Missing required "${attrName}" attribute:`);
|
throw new TranslationParseError(
|
||||||
|
element.sourceSpan, `Missing required "${attrName}" attribute:`);
|
||||||
}
|
}
|
||||||
return attrValue;
|
return attrValue;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
|
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
|
||||||
import {MessageRenderer} from '../../../message_renderers/message_renderer';
|
import {MessageRenderer} from '../../../message_renderers/message_renderer';
|
||||||
import {BaseVisitor} from '../base_visitor';
|
import {BaseVisitor} from '../base_visitor';
|
||||||
import {I18nError} from '../i18n_error';
|
import {TranslationParseError} from '../translation_parse_error';
|
||||||
import {getAttrOrThrow} from '../translation_utils';
|
import {getAttrOrThrow} from '../translation_utils';
|
||||||
|
|
||||||
const INLINE_ELEMENTS = ['g', 'bx', 'ex', 'bpt', 'ept', 'ph', 'it', 'mrk'];
|
const INLINE_ELEMENTS = ['g', 'bx', 'ex', 'bpt', 'ept', 'ph', 'it', 'mrk'];
|
||||||
@ -29,7 +29,7 @@ export class Xliff1MessageSerializer<T> extends BaseVisitor {
|
|||||||
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
|
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
|
||||||
visitAll(this, element.children);
|
visitAll(this, element.children);
|
||||||
} else {
|
} else {
|
||||||
throw new I18nError(element.sourceSpan, `Invalid element found in message.`);
|
throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import {extname} from 'path';
|
|||||||
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
|
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
|
||||||
import {TranslationBundle} from '../../../translator';
|
import {TranslationBundle} from '../../../translator';
|
||||||
import {BaseVisitor} from '../base_visitor';
|
import {BaseVisitor} from '../base_visitor';
|
||||||
import {I18nError} from '../i18n_error';
|
import {TranslationParseError} from '../translation_parse_error';
|
||||||
import {TranslationParser} from '../translation_parser';
|
import {TranslationParser} from '../translation_parser';
|
||||||
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
|
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
|
||||||
import {Xliff1MessageSerializer} from './xliff1_message_serializer';
|
import {Xliff1MessageSerializer} from './xliff1_message_serializer';
|
||||||
@ -75,12 +75,13 @@ class XliffTranslationVisitor extends BaseVisitor {
|
|||||||
if (element.name === 'trans-unit') {
|
if (element.name === 'trans-unit') {
|
||||||
const id = getAttrOrThrow(element, 'id');
|
const id = getAttrOrThrow(element, 'id');
|
||||||
if (this.translations[id] !== undefined) {
|
if (this.translations[id] !== undefined) {
|
||||||
throw new I18nError(element.sourceSpan, `Duplicated translations for message "${id}"`);
|
throw new TranslationParseError(
|
||||||
|
element.sourceSpan, `Duplicated translations for message "${id}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetMessage = element.children.find(isTargetElement);
|
const targetMessage = element.children.find(isTargetElement);
|
||||||
if (targetMessage === undefined) {
|
if (targetMessage === undefined) {
|
||||||
throw new I18nError(element.sourceSpan, 'Missing required <target> element');
|
throw new TranslationParseError(element.sourceSpan, 'Missing required <target> element');
|
||||||
}
|
}
|
||||||
this.translations[id] = serializeTargetMessage(targetMessage);
|
this.translations[id] = serializeTargetMessage(targetMessage);
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
|
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
|
||||||
import {MessageRenderer} from '../../../message_renderers/message_renderer';
|
import {MessageRenderer} from '../../../message_renderers/message_renderer';
|
||||||
import {BaseVisitor} from '../base_visitor';
|
import {BaseVisitor} from '../base_visitor';
|
||||||
import {I18nError} from '../i18n_error';
|
import {TranslationParseError} from '../translation_parse_error';
|
||||||
import {getAttrOrThrow, getAttribute} from '../translation_utils';
|
import {getAttrOrThrow, getAttribute} from '../translation_utils';
|
||||||
|
|
||||||
const INLINE_ELEMENTS = ['cp', 'sc', 'ec', 'mrk', 'sm', 'em'];
|
const INLINE_ELEMENTS = ['cp', 'sc', 'ec', 'mrk', 'sm', 'em'];
|
||||||
@ -33,7 +33,7 @@ export class Xliff2MessageSerializer<T> extends BaseVisitor {
|
|||||||
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
|
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
|
||||||
visitAll(this, element.children);
|
visitAll(this, element.children);
|
||||||
} else {
|
} else {
|
||||||
throw new I18nError(element.sourceSpan, `Invalid element found in message.`);
|
throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import {extname} from 'path';
|
|||||||
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
|
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
|
||||||
import {TranslationBundle} from '../../../translator';
|
import {TranslationBundle} from '../../../translator';
|
||||||
import {BaseVisitor} from '../base_visitor';
|
import {BaseVisitor} from '../base_visitor';
|
||||||
import {I18nError} from '../i18n_error';
|
import {TranslationParseError} from '../translation_parse_error';
|
||||||
import {TranslationParser} from '../translation_parser';
|
import {TranslationParser} from '../translation_parser';
|
||||||
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
|
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
|
||||||
import {Xliff2MessageSerializer} from './xliff2_message_serializer';
|
import {Xliff2MessageSerializer} from './xliff2_message_serializer';
|
||||||
@ -78,7 +78,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
|
|||||||
if (element.name === 'unit') {
|
if (element.name === 'unit') {
|
||||||
const externalId = getAttrOrThrow(element, 'id');
|
const externalId = getAttrOrThrow(element, 'id');
|
||||||
if (this.translations[externalId] !== undefined) {
|
if (this.translations[externalId] !== undefined) {
|
||||||
throw new I18nError(
|
throw new TranslationParseError(
|
||||||
element.sourceSpan, `Duplicated translations for message "${externalId}"`);
|
element.sourceSpan, `Duplicated translations for message "${externalId}"`);
|
||||||
}
|
}
|
||||||
visitAll(this, element.children, {unit: externalId});
|
visitAll(this, element.children, {unit: externalId});
|
||||||
@ -86,7 +86,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
|
|||||||
assertTranslationUnit(element, context);
|
assertTranslationUnit(element, context);
|
||||||
const targetMessage = element.children.find(isTargetElement);
|
const targetMessage = element.children.find(isTargetElement);
|
||||||
if (targetMessage === undefined) {
|
if (targetMessage === undefined) {
|
||||||
throw new I18nError(element.sourceSpan, 'Missing required <target> element');
|
throw new TranslationParseError(element.sourceSpan, 'Missing required <target> element');
|
||||||
}
|
}
|
||||||
this.translations[context.unit] = serializeTargetMessage(targetMessage);
|
this.translations[context.unit] = serializeTargetMessage(targetMessage);
|
||||||
} else {
|
} else {
|
||||||
@ -97,7 +97,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
|
|||||||
|
|
||||||
function assertTranslationUnit(segment: Element, context: any) {
|
function assertTranslationUnit(segment: Element, context: any) {
|
||||||
if (context === undefined || context.unit === undefined) {
|
if (context === undefined || context.unit === undefined) {
|
||||||
throw new I18nError(
|
throw new TranslationParseError(
|
||||||
segment.sourceSpan, 'Invalid <segment> element: should be a child of a <unit> element.');
|
segment.sourceSpan, 'Invalid <segment> element: should be a child of a <unit> element.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user