fix(ivy): i18n - strip meta blocks from untranslated messages (#33097)

If a message has no translation then we should still strip the
meta blocks from the message parts before adding back to the
AST.

PR Close #33097
This commit is contained in:
Pete Bacon Darwin 2019-10-11 10:01:08 +01:00 committed by Miško Hevery
parent d617373a76
commit 1845faa66b
3 changed files with 39 additions and 1 deletions

View File

@ -200,10 +200,15 @@ export function translate(
} else if (missingTranslation === 'warning') {
diagnostics.warn(e.message);
}
// Return the parsed message because this will have the meta blocks stripped
return [
ɵmakeTemplateObject(e.parsedMessage.messageParts, e.parsedMessage.messageParts),
substitutions
];
} else {
diagnostics.error(e.message);
return [messageParts, substitutions];
}
return [messageParts, substitutions];
}
}

View File

@ -21,6 +21,23 @@ describe('makeEs2015Plugin', () => {
expect(output.code).toEqual('const b = 10;\n"try\\n" + (40 + b) + "\\n me";');
});
it('should strip meta blocks', () => {
const diagnostics = new Diagnostics();
const input = 'const b = 10;\n$localize `:description:try\\n${40 + b}\\n me`;';
const output =
transformSync(input, {plugins: [makeEs2015TranslatePlugin(diagnostics, {})]}) !;
expect(output.code).toEqual('const b = 10;\n"try\\n" + (40 + b) + "\\n me";');
});
it('should not strip escaped meta blocks', () => {
const diagnostics = new Diagnostics();
const input = 'const b = 10;\n$localize `\\:description:try\\n${40 + b}\\n me`;';
const output =
transformSync(input, {plugins: [makeEs2015TranslatePlugin(diagnostics, {})]}) !;
expect(output.code).toEqual('const b = 10;\n":description:try\\n" + (40 + b) + "\\n me";');
});
it('should transform nested `$localize` tags', () => {
const diagnostics = new Diagnostics();
const input = '$localize`a${1}b${$localize`x${5}y${6}z`}c`;';

View File

@ -20,6 +20,22 @@ describe('makeEs5Plugin', () => {
expect(output.code).toEqual('const b = 10;\n"try\\n" + (40 + b) + "\\n me";');
});
it('should strip meta blocks', () => {
const diagnostics = new Diagnostics();
const input =
'const b = 10;\n$localize([":description:try\\n", ":placeholder:\\n me"], 40 + b);';
const output = transformSync(input, {plugins: [makeEs5TranslatePlugin(diagnostics, {})]}) !;
expect(output.code).toEqual('const b = 10;\n"try\\n" + (40 + b) + "\\n me";');
});
it('should not strip escaped meta blocks', () => {
const diagnostics = new Diagnostics();
const input =
`$localize(__makeTemplateObject([':desc:try', 'me'], ['\\\\\\:desc:try', 'me']), 40 + 2);`;
const output = transformSync(input, {plugins: [makeEs5TranslatePlugin(diagnostics, {})]}) !;
expect(output.code).toEqual('":desc:try" + (40 + 2) + "me";');
});
it('should transform nested `$localize` calls', () => {
const diagnostics = new Diagnostics();
const input = '$localize(["a", "b", "c"], 1, $localize(["x", "y", "z"], 5, 6));';