From f17072c7af2ff576eefd86c0501eef3726c7f62c Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 23 Oct 2019 22:49:02 +0100 Subject: [PATCH] refactor(ivy): i18n - create and use `isLocalize()` helper (#33314) PR Close #33314 --- .../source_files/es2015_translate_plugin.ts | 4 ++-- .../source_files/es5_translate_plugin.ts | 4 ++-- .../translate/source_files/source_file_utils.ts | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/localize/src/tools/src/translate/source_files/es2015_translate_plugin.ts b/packages/localize/src/tools/src/translate/source_files/es2015_translate_plugin.ts index 52cdd795f0..6f88e945a9 100644 --- a/packages/localize/src/tools/src/translate/source_files/es2015_translate_plugin.ts +++ b/packages/localize/src/tools/src/translate/source_files/es2015_translate_plugin.ts @@ -9,7 +9,7 @@ import {ɵParsedTranslation} from '@angular/localize'; import {NodePath, PluginObj} from '@babel/core'; import {TaggedTemplateExpression} from '@babel/types'; import {Diagnostics} from '../../diagnostics'; -import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isGlobalIdentifier, isNamedIdentifier, translate, unwrapMessagePartsFromTemplateLiteral} from './source_file_utils'; +import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, unwrapMessagePartsFromTemplateLiteral} from './source_file_utils'; export function makeEs2015TranslatePlugin( diagnostics: Diagnostics, translations: Record, @@ -20,7 +20,7 @@ export function makeEs2015TranslatePlugin( TaggedTemplateExpression(path: NodePath) { try { const tag = path.get('tag'); - if (isNamedIdentifier(tag, localizeName) && isGlobalIdentifier(tag)) { + if (isLocalize(tag, localizeName)) { const messageParts = unwrapMessagePartsFromTemplateLiteral(path.node.quasi.quasis); const translated = translate( diagnostics, translations, messageParts, path.node.quasi.expressions, diff --git a/packages/localize/src/tools/src/translate/source_files/es5_translate_plugin.ts b/packages/localize/src/tools/src/translate/source_files/es5_translate_plugin.ts index bcbfd51021..b96002b34a 100644 --- a/packages/localize/src/tools/src/translate/source_files/es5_translate_plugin.ts +++ b/packages/localize/src/tools/src/translate/source_files/es5_translate_plugin.ts @@ -9,7 +9,7 @@ import {ɵParsedTranslation} from '@angular/localize'; import {NodePath, PluginObj} from '@babel/core'; import {CallExpression} from '@babel/types'; import {Diagnostics} from '../../diagnostics'; -import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isGlobalIdentifier, isNamedIdentifier, translate, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from './source_file_utils'; +import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from './source_file_utils'; export function makeEs5TranslatePlugin( diagnostics: Diagnostics, translations: Record, @@ -20,7 +20,7 @@ export function makeEs5TranslatePlugin( CallExpression(callPath: NodePath) { try { const calleePath = callPath.get('callee'); - if (isNamedIdentifier(calleePath, localizeName) && isGlobalIdentifier(calleePath)) { + if (isLocalize(calleePath, localizeName)) { const messageParts = unwrapMessagePartsFromLocalizeCall(callPath); const expressions = unwrapSubstitutionsFromLocalizeCall(callPath.node); const translated = 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 7ae3e04d1a..1a6c81f275 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 @@ -11,8 +11,21 @@ import * as t from '@babel/types'; import {Diagnostics} from '../../diagnostics'; /** - * Is the given `expression` an identifier with the correct name + * Is the given `expression` the global `$localize` identifier? + * * @param expression The expression to check. + * @param localizeName The configured name of `$localize`. + */ +export function isLocalize( + expression: NodePath, localizeName: string): expression is NodePath { + return isNamedIdentifier(expression, localizeName) && isGlobalIdentifier(expression); +} + +/** + * Is the given `expression` an identifier with the correct `name`? + * + * @param expression The expression to check. + * @param name The name of the identifier we are looking for. */ export function isNamedIdentifier( expression: NodePath, name: string): expression is NodePath {