Skip to content

Commit

Permalink
fix(localize): relax error to warning for missing target (#41944)
Browse files Browse the repository at this point in the history
Some localization workflows want to use the extracted source translation
files directy back in the project as a target translation file.

The extraction process generates files that only contain "source" messages
and not "target" messages. This is actually valid for most translation formats
but currently the Angular localization process expects target translation files
to always contain target messages and will stop with an error in this case.

Now, instead of an error, the translation file loader will log a warning,
and then try to falback to a source message, only erroring if this is also
missing.

Fixes #21690

PR Close #41944
  • Loading branch information
petebacondarwin authored and mhevery committed May 5, 2021
1 parent fdf2e02 commit 992c70d
Show file tree
Hide file tree
Showing 4 changed files with 1,505 additions and 1,423 deletions.
Expand Up @@ -140,13 +140,23 @@ class XliffTranslationVisitor extends BaseVisitor {
return;
}

// Error if there is no `<target>` child element
const targetMessage = element.children.find(isNamedElement('target'));
let targetMessage = element.children.find(isNamedElement('target'));
if (targetMessage === undefined) {
// Warn if there is no `<target>` child element
addParseDiagnostic(
bundle.diagnostics, element.sourceSpan, 'Missing required <target> element',
ParseErrorLevel.ERROR);
return;
bundle.diagnostics, element.sourceSpan, 'Missing <target> element',
ParseErrorLevel.WARNING);

// Fallback to the `<source>` element if available.
targetMessage = element.children.find(isNamedElement('source'));
if (targetMessage === undefined) {
// Error if there is neither `<target>` nor `<source>`.
addParseDiagnostic(
bundle.diagnostics, element.sourceSpan,
'Missing required element: one of <target> or <source> is required',
ParseErrorLevel.ERROR);
return;
}
}

const {translation, parseErrors, serializeErrors} = serializeTranslationMessage(targetMessage, {
Expand Down
Expand Up @@ -132,12 +132,23 @@ class Xliff2TranslationVisitor extends BaseVisitor {
return;
}

const targetMessage = element.children.find(isNamedElement('target'));
let targetMessage = element.children.find(isNamedElement('target'));
if (targetMessage === undefined) {
// Warn if there is no `<target>` child element
addParseDiagnostic(
bundle.diagnostics, element.sourceSpan, 'Missing required <target> element',
ParseErrorLevel.ERROR);
return;
bundle.diagnostics, element.sourceSpan, 'Missing <target> element',
ParseErrorLevel.WARNING);

// Fallback to the `<source>` element if available.
targetMessage = element.children.find(isNamedElement('source'));
if (targetMessage === undefined) {
// Error if there is neither `<target>` nor `<source>`.
addParseDiagnostic(
bundle.diagnostics, element.sourceSpan,
'Missing required element: one of <target> or <source> is required',
ParseErrorLevel.ERROR);
return;
}
}

const {translation, parseErrors, serializeErrors} = serializeTranslationMessage(targetMessage, {
Expand Down

0 comments on commit 992c70d

Please sign in to comment.