Skip to content

Commit fc630f7

Browse files
fix bug that all the TS file localization string don't work (#80)
##### Objective I want to fix the regression introduced by this commit: #37 It breaks all the localization strings in TS files. ##### Abstractions For vscode extension localization we referenced the vscode sample project like https://github.com/microsoft/vscode-extension-samples/blob/master/i18n-sample/src/extension.ts#L8 And it uses the `GULP` library to inject and load the resource json files. So for each TS files it has codes like: ``` import * as nls from 'vscode-nls'; const localize = nls.config({ messageFormat: nls.MessageFormat.file })(); ``` This does ALL the good stuff, it inject lots of javascript codes in the background, let the gulp to load the "correct" json files when loading. But in the PR #37 we move the codes to context.ts file, so gulp only take cares the gulp.json, it will not search and load the other TS file's resource string. ##### Tests performed - passed build - try to verify some features in the Chinese display language setting ##### Screen shot ![image](https://user-images.githubusercontent.com/68938334/106983474-ec311500-67a0-11eb-87e2-33fb5eee26b7.png)
1 parent bbc53c2 commit fc630f7

31 files changed

+240
-175
lines changed

extension/src/commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as vscode from "vscode";
2+
import * as nls from 'vscode-nls';
23
import { AutoLispExt } from './extension';
34
import { openWebHelp } from './help/openWebHelp';
45
import { showErrorMessage } from './project/projectCommands';
56
import { AutolispDefinitionProvider } from './providers/gotoProvider';
67

8+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
79

810
export function registerCommands(context: vscode.ExtensionContext){
911

@@ -14,7 +16,7 @@ export function registerCommands(context: vscode.ExtensionContext){
1416
}
1517
catch (err) {
1618
if (err){
17-
let msg = AutoLispExt.localize("autolispext.help.commands.openWebHelp", "Failed to load the webHelpAbstraction.json file");
19+
let msg = localize("autolispext.help.commands.openWebHelp", "Failed to load the webHelpAbstraction.json file");
1820
showErrorMessage(msg, err);
1921
}
2022
}
@@ -30,7 +32,7 @@ export function registerCommands(context: vscode.ExtensionContext){
3032
}
3133
catch (err) {
3234
if (err){
33-
let msg = AutoLispExt.localize("autolispext.commands.addFoldingRegion", "Failed to insert snippet");
35+
let msg = localize("autolispext.commands.addFoldingRegion", "Failed to insert snippet");
3436
showErrorMessage(msg, err);
3537
}
3638
}

extension/src/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import * as Resources from "./resources";
44
import { Disposable } from 'vscode-languageclient';
55
import { DocumentManager } from './documents';
66

7-
// This is a singleton class
7+
88
export class ContextManager{
99
private _ctx: vscode.ExtensionContext;
1010
private _docManager: DocumentManager;
1111

12-
localize: nls.LocalizeFunc = nls.config({ messageFormat: nls.MessageFormat.file })();
13-
12+
localize = nls.config({ messageFormat: nls.MessageFormat.file })();
13+
1414
get Context(): vscode.ExtensionContext {
1515
return this._ctx;
1616
}

extension/src/debug.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import * as vscode from 'vscode';
22
import * as Net from 'net';
33
import * as os from 'os';
4-
import { AutoLispExt } from './extension';
4+
5+
import * as nls from 'vscode-nls';
6+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
57

68
import { pickProcess } from './process/acadPicker';
79
import { calculateABSPathForDAP } from './platform';
810
import { existsSync } from 'fs';
911
import { ProcessPathCache } from './process/processCache';
1012
import { DiagnosticsCtrl } from './diagnosticsCtrl';
1113

12-
let strNoADPerr: string = AutoLispExt.localize("autolispext.debug.nodap", "doesn’t exist. Verify that the file exists in the same folder as that for the product specified in the launch.json file.");
13-
let strNoACADerr: string = AutoLispExt.localize("autolispext.debug.noacad", "doesn’t exist. Verify and correct the folder path to the product executable.");
14+
let strNoADPerr: string = localize("autolispext.debug.nodap", "doesn’t exist. Verify that the file exists in the same folder as that for the product specified in the launch.json file.");
15+
let strNoACADerr: string = localize("autolispext.debug.noacad", "doesn’t exist. Verify and correct the folder path to the product executable.");
1416
let acadPid2Attach = -1;
1517

1618
const attachCfgName = 'AutoLISP Debug: Attach';
@@ -95,7 +97,7 @@ export function registerLispDebugProviders(context: vscode.ExtensionContext) {
9597
setDefaultAcadPid(-1);
9698
}
9799
else if (event.event === "acadnosupport") {
98-
let msg = AutoLispExt.localize("autolispext.debug.acad.nosupport",
100+
let msg = localize("autolispext.debug.acad.nosupport",
99101
"This instance of AutoCAD doesn’t support debugging AutoLISP files, use a release later than AutoCAD 2020.");
100102
vscode.window.showErrorMessage(msg);
101103
}
@@ -133,24 +135,24 @@ class LispLaunchConfigurationProvider implements vscode.DebugConfigurationProvid
133135
let productPath = getExtensionSettingString(LAUNCH_PROC);
134136

135137
if (!productPath) {
136-
let info = AutoLispExt.localize("autolispext.debug.launchjson.path",
138+
let info = localize("autolispext.debug.launchjson.path",
137139
"Specify the absolute path to the product with the Path attribute of the launch.json file.");
138140
vscode.window.showInformationMessage(info);
139141
let platform = os.type();
140142
if (platform === 'Windows_NT') {
141-
let msg = AutoLispExt.localize("autolispext.debug.prod.path.win",
143+
let msg = localize("autolispext.debug.prod.path.win",
142144
"Specify the absolute path for the product. For example, C://Program Files//Autodesk//AutoCAD//acad.exe.");
143145
productPath = await vscode.window.showInputBox({ placeHolder: msg });
144146
rememberLaunchPath(productPath);
145147
}
146148
else if (platform === 'Darwin') {
147-
let msg = AutoLispExt.localize("autolispext.debug.prod.path.osx",
149+
let msg = localize("autolispext.debug.prod.path.osx",
148150
"Specify the absolute path for the product. For example, /Applications/Autodesk/AutoCAD.app/Contents/MacOS/AutoCAD.");
149151
productPath = await vscode.window.showInputBox({ placeHolder: msg });
150152
rememberLaunchPath(productPath);
151153
}
152154
else {
153-
let msg = AutoLispExt.localize("autolispext.debug.prod.path.other", "Specify the absolute path for the product.");
155+
let msg = localize("autolispext.debug.prod.path.other", "Specify the absolute path for the product.");
154156
productPath = await vscode.window.showInputBox({ placeHolder: msg });
155157
rememberLaunchPath(productPath);
156158
}
@@ -212,7 +214,7 @@ class LispAttachConfigurationProvider implements vscode.DebugConfigurationProvid
212214
ProcessPathCache.clearProductProcessPathArr();
213215
let processId = await pickProcess(false, acadPid2Attach);
214216
if (!processId) {
215-
let msg = AutoLispExt.localize("autolispext.debug.noprocess.eror", "No process for which to attach could be found.");
217+
let msg = localize("autolispext.debug.noprocess.eror", "No process for which to attach could be found.");
216218
return vscode.window.showInformationMessage(msg).then(_ => {
217219
return undefined; // abort attach
218220
});

extension/src/extension.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11

22
'use strict';
33
import * as vscode from 'vscode';
4-
import { LanguageClient } from 'vscode-languageclient';
5-
import { ContextManager } from "./context";
6-
// moved singleton AutoLispExt constructor ahead of 'our' other imports to create nls.localize() before they cause a null reference error
7-
export const AutoLispExt: ContextManager = new ContextManager();
4+
5+
import {
6+
LanguageClient
7+
} from 'vscode-languageclient';
8+
89

910
import * as Diagnostics from './diagnosticsCtrl';
1011
import { onUriRequested } from './uriHandler';
12+
1113
import * as formatProviders from './format/formatProviders';
1214
import * as autoCompletionProvider from "./completion/autocompletionProvider";
1315
import * as statusBar from "./statusbar";
1416
import * as autoIndent from './format/autoIndent';
17+
import { ContextManager } from "./context";
1518
import * as DebugProviders from "./debug";
1619
import { registerProjectCommands } from "./project/projectCommands";
1720
import { registerCommands } from "./commands";
1821
import { loadAllResources } from "./resources";
22+
import * as nls from 'vscode-nls';
1923

24+
// The example uses the file message format.
25+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
26+
export const AutoLispExt: ContextManager = new ContextManager();
2027
let client: LanguageClient;
2128

2229
loadAllResources();
2330

2431
export function activate(context: vscode.ExtensionContext) {
25-
AutoLispExt.initialize(context);
32+
AutoLispExt.initialize(context);
2633

2734
//-----------------------------------------------------------
2835
//1. lisp autoformat

extension/src/format/formatProviders.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as vscode from 'vscode';
2-
import { LispFormatter } from './formatter';
3-
import * as utils from "../utils";
4-
import { AutoLispExt } from '../extension';
5-
2+
import { LispFormatter } from './formatter'
3+
import * as utils from "../utils"
4+
import * as nls from 'vscode-nls';
5+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
66

77
export function registerDocumentFormatter() {
88
vscode.languages.registerDocumentFormattingEditProvider(['autolisp', 'lisp'], {
@@ -13,7 +13,7 @@ export function registerDocumentFormatter() {
1313
let currentLSPDoc = activeTextEditor.document.fileName;
1414
let ext = currentLSPDoc.substring(currentLSPDoc.length - 4, currentLSPDoc.length).toUpperCase();
1515
if (ext === ".DCL") {
16-
let msg = AutoLispExt.localize("autolispext.format.notsupport.dcl", "Command doesn't support DCL files.");
16+
let msg = localize("autolispext.format.notsupport.dcl", "Command doesn't support DCL files.");
1717
vscode.window.showInformationMessage(msg);
1818
return [];
1919
}
@@ -33,12 +33,12 @@ export function registeSelectionFormatter() {
3333
let currentLSPDoc = activeTextEditor.document.fileName;
3434
let ext = currentLSPDoc.substring(currentLSPDoc.length - 4, currentLSPDoc.length).toUpperCase();
3535
if (ext === ".DCL") {
36-
let msg = AutoLispExt.localize("autolispext.format.notsupport.dcl", "Command doesn't support DCL files.");
36+
let msg = localize("autolispext.format.notsupport.dcl", "Command doesn't support DCL files.");
3737
vscode.window.showInformationMessage(msg);
3838
return [];
3939
}
4040
if (activeTextEditor.selection.isEmpty) {
41-
let msg = AutoLispExt.localize("autolispext.format.selectionlines", "First, select the lines of code to format.");
41+
let msg = localize("autolispext.format.selectionlines", "First, select the lines of code to format.");
4242
vscode.window.showInformationMessage(msg);
4343
}
4444

extension/src/format/formatter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as vscode from 'vscode';
2+
23
import { Sexpression } from "./sexpression";
34
import { LispParser } from "./parser";
4-
import { AutoLispExt } from '../extension';
5+
import * as nls from 'vscode-nls';
6+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
57

68
export class LispFormatter {
79

@@ -58,7 +60,7 @@ export class LispFormatter {
5860

5961
let formatstr = lispLists.formatting(startColumn, linefeed);
6062
if (formatstr.length == 0) {
61-
let msg = AutoLispExt.localize("autolispext.formatter.errors", "It meets some errors when formatting");
63+
let msg = localize("autolispext.formatter.errors", "It meets some errors when formatting");
6264
throw new Error(msg);
6365
}
6466

extension/src/process/acadPicker.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import { basename } from 'path';
1313
import { getProcesses } from './processTree';
1414
import {ProcessPathCache} from "./processCache";
1515
import { calculateACADProcessName } from '../platform';
16-
import { activeDocHasValidLanguageId } from '../utils';
17-
import { AutoLispExt } from '../extension';
16+
import { acitiveDocHasValidLanguageId } from '../utils';
1817

18+
19+
import * as nls from 'vscode-nls';
20+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
1921
interface ProcessItem extends vscode.QuickPickItem{
2022
pidOrPort:string;
2123
sortKey:number;
@@ -24,11 +26,11 @@ interface ProcessItem extends vscode.QuickPickItem{
2426
function getProcesspickerPlaceHolderStr(){
2527
let platform = os.type();
2628
if(platform === 'Windows_NT'){
27-
return AutoLispExt.localize('autolispext.pickprocess.acad.win', "Pick the process to attach. Make sure AutoCAD, or one of the specialized toolsets, is running. Type acad and select it from the list.");
29+
return localize('autolispext.pickprocess.acad.win', "Pick the process to attach. Make sure AutoCAD, or one of the specialized toolsets, is running. Type acad and select it from the list.");
2830
}else if(platform === 'Darwin'){
29-
return AutoLispExt.localize('autolispext.pickprocess.acad.osx', "Pick the process to attach. Make sure AutoCAD is running. Type AutoCAD and select it from the list.");
31+
return localize('autolispext.pickprocess.acad.osx', "Pick the process to attach. Make sure AutoCAD is running. Type AutoCAD and select it from the list.");
3032
}else{
31-
return AutoLispExt.localize('autolispext.pickprocess.acad.other', "Pick the process to attach");
33+
return localize('autolispext.pickprocess.acad.other', "Pick the process to attach");
3234
}
3335
}
3436

@@ -54,7 +56,7 @@ export function pickProcess(ports:any, defaultPid: number): Promise<string | nul
5456
let choosedItem = vscode.window.showQuickPick(items, options).then(item => item ? item.pidOrPort : null);
5557
return choosedItem;
5658
}).catch(err => {
57-
let chooseItem = vscode.window.showErrorMessage(AutoLispExt.localize('autolispext.pickprocess.pickfailed', "Process picker failed ({0})", err.message), { modal: true }).then(_ => null);
59+
let chooseItem = vscode.window.showErrorMessage(localize('autolispext.pickprocess.pickfailed', "Process picker failed ({0})", err.message), { modal: true }).then(_ => null);
5860
return chooseItem;
5961
});
6062
}
@@ -74,7 +76,7 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {
7476
if(ProcessPathCache.globalAcadNameInUserAttachConfig){
7577
processName = ProcessPathCache.globalAcadNameInUserAttachConfig;
7678
}
77-
else if (vscode.window.activeTextEditor && activeDocHasValidLanguageId()) {
79+
else if (vscode.window.activeTextEditor && acitiveDocHasValidLanguageId()) {
7880
//read attach configuration from launch.json
7981
let configurations:[] = vscode.workspace.getConfiguration("launch", vscode.window.activeTextEditor.document.uri).get("configurations");
8082
let attachLispConfig;
@@ -113,14 +115,14 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {
113115

114116
if (usePort) {
115117
if (protocol === 'inspector') {
116-
description = AutoLispExt.localize('autolispext.pickprocess.process.id.port', "process id: {0}, debug port: {1}", pid, port);
118+
description = localize('autolispext.pickprocess.process.id.port', "process id: {0}, debug port: {1}", pid, port);
117119
} else {
118-
description = AutoLispExt.localize('autolispext.pickprocess.process.id.legacy', "process id: {0}, debug port: {1} (legacy protocol)", pid, port);
120+
description = localize('autolispext.pickprocess.process.id.legacy', "process id: {0}, debug port: {1} (legacy protocol)", pid, port);
119121
}
120122
pidOrPort = `${protocol}${port}`;
121123
} else {
122124
if (protocol && port > 0) {
123-
description = AutoLispExt.localize('autolispext.pickprocess.process.port.singal', "process id: {0}, debug port: {1} ({2})", pid, port, 'SIGUSR1');
125+
description = localize('autolispext.pickprocess.process.port.singal', "process id: {0}, debug port: {1} ({2})", pid, port, 'SIGUSR1');
124126
pidOrPort = `${pid}${protocol}${port}`;
125127
} else {
126128
// no port given
@@ -136,7 +138,7 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {
136138

137139
if(addintolist){
138140
ProcessPathCache.addGlobalProductProcessPathArr(executablePath, pid);
139-
description = AutoLispExt.localize('autolispext.pickprocess.process.id.singal', "process id: {0} ({1})", pid, 'SIGUSR1');
141+
description = localize('autolispext.pickprocess.process.id.singal', "process id: {0} ({1})", pid, 'SIGUSR1');
140142
pidOrPort = pid.toString();
141143
}
142144
}

extension/src/project/addFile2Project.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import * as vscode from 'vscode';
1+
import * as vscode from 'vscode'
22
import { ProjectTreeProvider, isFileAlreadyInProject, hasFileWithSameName } from './projectTree';
3-
import { AutoLispExt } from '../extension';
4-
import * as path from 'path';
3+
4+
import * as nls from 'vscode-nls';
5+
const localize = nls.config({ messageFormat: nls.MessageFormat.file })();
6+
7+
import * as path from 'path'
58

69
export async function AddFile2Project(fileList?: vscode.Uri[]) {
710
try {
811
if (ProjectTreeProvider.hasProjectOpened() == false) {
9-
let msg = AutoLispExt.localize("autolispext.project.addfile.openproject", "A project must be open before you can add a file.");
12+
let msg = localize("autolispext.project.addfile.openproject", "A project must be open before you can add a file.");
1013
return Promise.reject(msg);
1114
}
1215

@@ -18,7 +21,7 @@ export async function AddFile2Project(fileList?: vscode.Uri[]) {
1821
for (let file of selectedFiles) {
1922
let fileUpper = file.fsPath.toUpperCase();
2023
if (fileUpper.endsWith(".LSP") == false) {
21-
let msg = AutoLispExt.localize("autolispext.project.addfile.onlylspallowed", "Only LSP files are allowed.");
24+
let msg = localize("autolispext.project.addfile.onlylspallowed", "Only LSP files are allowed.");
2225
return Promise.reject(msg);
2326
}
2427

@@ -27,19 +30,19 @@ export async function AddFile2Project(fileList?: vscode.Uri[]) {
2730
//Legacy IDE doesn't allow user to add hello.lsp.lsp into a project, but if there happen to be a file
2831
// named hello.lsp, it will add hello.lsp into project, and this is wrong.
2932
//To keep consistency with legacy IDE, we have to reject files of this kind.
30-
let msg = AutoLispExt.localize("autolispext.project.addfile.onlylspallowed", "Only LSP files are allowed.");
33+
let msg = localize("autolispext.project.addfile.onlylspallowed", "Only LSP files are allowed.");
3134
return Promise.reject(msg);
3235
}
3336

3437
if (isFileAlreadyInProject(file.fsPath, ProjectTreeProvider.instance().projectNode)) {
35-
let msg = AutoLispExt.localize("autolispext.project.addfile.filealreadyexist", "File already exists in this project: ");
38+
let msg = localize("autolispext.project.addfile.filealreadyexist", "File already exists in this project: ");
3639
vscode.window.showInformationMessage(msg + file.fsPath);
3740

3841
continue;
3942
}
4043

4144
if(hasFileWithSameName(file.fsPath, ProjectTreeProvider.instance().projectNode)) {
42-
let msg = AutoLispExt.localize("autolispext.project.addfile.samenameexist", "File with the same name already exists in this project: ");
45+
let msg = localize("autolispext.project.addfile.samenameexist", "File with the same name already exists in this project: ");
4346
vscode.window.showInformationMessage(msg + path.basename(file.fsPath));
4447

4548
continue;
@@ -76,7 +79,7 @@ function hasMultipleExtensions(filePath:string):boolean {
7679
}
7780

7881
async function SelectLspFiles() {
79-
let label = AutoLispExt.localize("autolispext.project.addfile.openlabel", "Add to Project");
82+
let label = localize("autolispext.project.addfile.openlabel", "Add to Project");
8083
const options: vscode.OpenDialogOptions = {
8184
//TBD: globalize
8285
canSelectMany: true,

0 commit comments

Comments
 (0)