From 559689f66f6fc5a93d6f02a8b6f1d5888de59ad0 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 3 Oct 2017 17:18:12 -0700 Subject: [PATCH 1/3] Localize more Extract Function/Constant strings Fixes #18899 --- src/compiler/diagnosticMessages.json | 10 ++++++++++ src/services/refactors/extractSymbol.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index ee5ada47382..7d4d1441968 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3733,5 +3733,15 @@ "Extract constant": { "category": "Message", "code": 95006 + }, + + "enclosing scope": { + "category": "Message", + "code": 95007 + }, + + "{0} scope": { + "category": "Message", + "code": 95008 } } diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 92c36fd1c99..b6570dda2bb 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -557,7 +557,7 @@ namespace ts.refactor.extractSymbol { description: getDescriptionForConstantInScope(scope), errors: constantErrorsPerScope[i], scopeDescription: (i === 0 && !isClassLike(scope)) - ? "enclosing scope" // Like "global scope" and "module scope", this is not localized. + ? Diagnostics.enclosing_scope.message : scopeDescription, }, }; @@ -630,7 +630,7 @@ namespace ts.refactor.extractSymbol { function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string { return scope.kind === SyntaxKind.ModuleBlock ? `namespace '${scope.parent.name.getText()}'` - : scope.externalModuleIndicator ? "module scope" : "global scope"; + : formatStringFromArgs(Diagnostics._0_scope.message, [scope.externalModuleIndicator ? "module" : "global"]); } function getUniqueName(baseName: string, fileText: string): string { From bcdfdd276fa20602162afcc8f895c9eae6f06cd2 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 3 Oct 2017 18:10:58 -0700 Subject: [PATCH 2/3] Call getLocaleSpecificMessage --- src/services/refactors/extractSymbol.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index b6570dda2bb..4171d61805b 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -5,7 +5,7 @@ namespace ts.refactor.extractSymbol { const extractSymbol: Refactor = { name: "Extract Symbol", - description: Diagnostics.Extract_symbol.message, + description: getLocaleSpecificMessage(Diagnostics.Extract_symbol), getAvailableActions, getEditsForAction, }; @@ -43,7 +43,7 @@ namespace ts.refactor.extractSymbol { // Don't issue refactorings with duplicated names. // Scopes come back in "innermost first" order, so extractions will // preferentially go into nearer scopes - const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [functionExtraction.description, functionExtraction.scopeDescription]); + const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [functionExtraction.description, functionExtraction.scopeDescription]); if (!usedFunctionNames.has(description)) { usedFunctionNames.set(description, true); functionActions.push({ @@ -58,7 +58,7 @@ namespace ts.refactor.extractSymbol { // Don't issue refactorings with duplicated names. // Scopes come back in "innermost first" order, so extractions will // preferentially go into nearer scopes - const description = formatStringFromArgs(Diagnostics.Extract_to_0_in_1.message, [constantExtraction.description, constantExtraction.scopeDescription]); + const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [constantExtraction.description, constantExtraction.scopeDescription]); if (!usedConstantNames.has(description)) { usedConstantNames.set(description, true); constantActions.push({ @@ -78,7 +78,7 @@ namespace ts.refactor.extractSymbol { if (functionActions.length) { infos.push({ name: extractSymbol.name, - description: Diagnostics.Extract_function.message, + description: getLocaleSpecificMessage(Diagnostics.Extract_function), actions: functionActions }); } @@ -86,7 +86,7 @@ namespace ts.refactor.extractSymbol { if (constantActions.length) { infos.push({ name: extractSymbol.name, - description: Diagnostics.Extract_constant.message, + description: getLocaleSpecificMessage(Diagnostics.Extract_constant), actions: constantActions }); } @@ -557,7 +557,7 @@ namespace ts.refactor.extractSymbol { description: getDescriptionForConstantInScope(scope), errors: constantErrorsPerScope[i], scopeDescription: (i === 0 && !isClassLike(scope)) - ? Diagnostics.enclosing_scope.message + ? getLocaleSpecificMessage(Diagnostics.enclosing_scope) : scopeDescription, }, }; @@ -630,7 +630,7 @@ namespace ts.refactor.extractSymbol { function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string { return scope.kind === SyntaxKind.ModuleBlock ? `namespace '${scope.parent.name.getText()}'` - : formatStringFromArgs(Diagnostics._0_scope.message, [scope.externalModuleIndicator ? "module" : "global"]); + : formatStringFromArgs(getLocaleSpecificMessage(Diagnostics._0_scope), [scope.externalModuleIndicator ? "module" : "global"]); } function getUniqueName(baseName: string, fileText: string): string { From 02f2a29ca24dd4aeb9cb26b6ac2fb667e02b05e3 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 4 Oct 2017 12:33:48 -0700 Subject: [PATCH 3/3] Stop combining already-translated strings --- src/compiler/diagnosticMessages.json | 4 +-- src/services/refactors/extractSymbol.ts | 46 +++++++++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7d4d1441968..3b32ba174f1 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3735,12 +3735,12 @@ "code": 95006 }, - "enclosing scope": { + "Extract to {0} in enclosing scope": { "category": "Message", "code": 95007 }, - "{0} scope": { + "Extract to {0} in {1} scope": { "category": "Message", "code": 95008 } diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 4171d61805b..d99f1bbda91 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -43,7 +43,7 @@ namespace ts.refactor.extractSymbol { // Don't issue refactorings with duplicated names. // Scopes come back in "innermost first" order, so extractions will // preferentially go into nearer scopes - const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [functionExtraction.description, functionExtraction.scopeDescription]); + const description = functionExtraction.description; if (!usedFunctionNames.has(description)) { usedFunctionNames.set(description, true); functionActions.push({ @@ -58,7 +58,7 @@ namespace ts.refactor.extractSymbol { // Don't issue refactorings with duplicated names. // Scopes come back in "innermost first" order, so extractions will // preferentially go into nearer scopes - const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [constantExtraction.description, constantExtraction.scopeDescription]); + const description = constantExtraction.description; if (!usedConstantNames.has(description)) { usedConstantNames.set(description, true); constantActions.push({ @@ -524,7 +524,6 @@ namespace ts.refactor.extractSymbol { interface Extraction { readonly description: string; - readonly scopeDescription: string; readonly errors: ReadonlyArray; } @@ -542,23 +541,43 @@ namespace ts.refactor.extractSymbol { const { scopes, readsAndWrites: { functionErrorsPerScope, constantErrorsPerScope } } = getPossibleExtractionsWorker(targetRange, context); // Need the inner type annotation to avoid https://github.com/Microsoft/TypeScript/issues/7547 const extractions = scopes.map((scope, i): ScopeExtractions => { + const functionDescriptionPart = getDescriptionForFunctionInScope(scope); + const constantDescriptionPart = getDescriptionForConstantInScope(scope); + const scopeDescription = isFunctionLikeDeclaration(scope) ? getDescriptionForFunctionLikeDeclaration(scope) : isClassLike(scope) ? getDescriptionForClassLikeDeclaration(scope) : getDescriptionForModuleLikeDeclaration(scope); + + let functionDescription: string; + let constantDescription: string; + if (scopeDescription === SpecialScope.Global) { + functionDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1_scope), [functionDescriptionPart, "global"]); + constantDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1_scope), [constantDescriptionPart, "global"]); + } + else if (scopeDescription === SpecialScope.Module) { + functionDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1_scope), [functionDescriptionPart, "module"]); + constantDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1_scope), [constantDescriptionPart, "module"]); + } + else { + functionDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [functionDescriptionPart, scopeDescription]); + constantDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_1), [constantDescriptionPart, scopeDescription]); + } + + // Customize the phrasing for the innermost scope to increase clarity. + if (i === 0 && !isClassLike(scope)) { + constantDescription = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Extract_to_0_in_enclosing_scope), [constantDescriptionPart]); + } + return { functionExtraction: { - description: getDescriptionForFunctionInScope(scope), + description: functionDescription, errors: functionErrorsPerScope[i], - scopeDescription, }, constantExtraction: { - description: getDescriptionForConstantInScope(scope), + description: constantDescription, errors: constantErrorsPerScope[i], - scopeDescription: (i === 0 && !isClassLike(scope)) - ? getLocaleSpecificMessage(Diagnostics.enclosing_scope) - : scopeDescription, }, }; }); @@ -627,10 +646,15 @@ namespace ts.refactor.extractSymbol { ? `class '${scope.name.text}'` : scope.name ? `class expression '${scope.name.text}'` : "anonymous class expression"; } - function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string { + function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string | SpecialScope { return scope.kind === SyntaxKind.ModuleBlock ? `namespace '${scope.parent.name.getText()}'` - : formatStringFromArgs(getLocaleSpecificMessage(Diagnostics._0_scope), [scope.externalModuleIndicator ? "module" : "global"]); + : scope.externalModuleIndicator ? SpecialScope.Module : SpecialScope.Global; + } + + const enum SpecialScope { + Module, + Global, } function getUniqueName(baseName: string, fileText: string): string {