refactor(ivy): i18n - create and use `isLocalize()` helper (#33314)
PR Close #33314
This commit is contained in:
parent
fde8363e0d
commit
f17072c7af
|
@ -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<string, ɵParsedTranslation>,
|
||||
|
@ -20,7 +20,7 @@ export function makeEs2015TranslatePlugin(
|
|||
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) {
|
||||
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,
|
||||
|
|
|
@ -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<string, ɵParsedTranslation>,
|
||||
|
@ -20,7 +20,7 @@ export function makeEs5TranslatePlugin(
|
|||
CallExpression(callPath: NodePath<CallExpression>) {
|
||||
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 =
|
||||
|
|
|
@ -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<t.Identifier> {
|
||||
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<t.Identifier> {
|
||||
|
|
Loading…
Reference in New Issue