From 1845faa66b486a0274b17898ad4cb0396ed343ef Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 11 Oct 2019 10:01:08 +0100 Subject: [PATCH] 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 --- .../translate/source_files/source_file_utils.ts | 7 ++++++- .../es2015_translate_plugin_spec.ts | 17 +++++++++++++++++ .../source_files/es5_translate_plugin_spec.ts | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/localize/src/tools/src/translate/source_files/source_file_utils.ts b/packages/localize/src/tools/src/translate/source_files/source_file_utils.ts index d00a6454c0..4314e2a155 100644 --- a/packages/localize/src/tools/src/translate/source_files/source_file_utils.ts +++ b/packages/localize/src/tools/src/translate/source_files/source_file_utils.ts @@ -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]; } } diff --git a/packages/localize/src/tools/test/translate/source_files/es2015_translate_plugin_spec.ts b/packages/localize/src/tools/test/translate/source_files/es2015_translate_plugin_spec.ts index 5a034a5a52..d25e90d830 100644 --- a/packages/localize/src/tools/test/translate/source_files/es2015_translate_plugin_spec.ts +++ b/packages/localize/src/tools/test/translate/source_files/es2015_translate_plugin_spec.ts @@ -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`;'; diff --git a/packages/localize/src/tools/test/translate/source_files/es5_translate_plugin_spec.ts b/packages/localize/src/tools/test/translate/source_files/es5_translate_plugin_spec.ts index ed4e470cad..b06579c081 100644 --- a/packages/localize/src/tools/test/translate/source_files/es5_translate_plugin_spec.ts +++ b/packages/localize/src/tools/test/translate/source_files/es5_translate_plugin_spec.ts @@ -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));';