ai-localize-vscode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/extension.js +116 -0
- package/package.json +68 -0
- package/src/extension.ts +84 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/extension.ts
|
|
31
|
+
var extension_exports = {};
|
|
32
|
+
__export(extension_exports, {
|
|
33
|
+
activate: () => activate,
|
|
34
|
+
deactivate: () => deactivate
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(extension_exports);
|
|
37
|
+
var vscode = __toESM(require("vscode"));
|
|
38
|
+
var path = __toESM(require("path"));
|
|
39
|
+
var diagnosticCollection;
|
|
40
|
+
function activate(context) {
|
|
41
|
+
diagnosticCollection = vscode.languages.createDiagnosticCollection("ai-localize");
|
|
42
|
+
context.subscriptions.push(diagnosticCollection);
|
|
43
|
+
context.subscriptions.push(
|
|
44
|
+
vscode.commands.registerCommand("aiLocalize.scan", runScan),
|
|
45
|
+
vscode.commands.registerCommand("aiLocalize.extract", runExtract),
|
|
46
|
+
vscode.commands.registerCommand("aiLocalize.validate", runValidate),
|
|
47
|
+
vscode.commands.registerCommand("aiLocalize.jumpToLocale", jumpToLocale)
|
|
48
|
+
);
|
|
49
|
+
context.subscriptions.push(
|
|
50
|
+
vscode.workspace.onDidSaveTextDocument(async (doc) => {
|
|
51
|
+
const config = vscode.workspace.getConfiguration("aiLocalize");
|
|
52
|
+
if (!config.get("enabled", true)) return;
|
|
53
|
+
await scanDocument(doc);
|
|
54
|
+
})
|
|
55
|
+
);
|
|
56
|
+
vscode.window.showInformationMessage("AI Localize extension activated");
|
|
57
|
+
}
|
|
58
|
+
function runTerminalCommand(cmd) {
|
|
59
|
+
const folder = vscode.workspace.workspaceFolders?.[0];
|
|
60
|
+
if (!folder) return;
|
|
61
|
+
const terminal = vscode.window.createTerminal("AI Localize");
|
|
62
|
+
terminal.sendText(`cd "${folder.uri.fsPath}" && ${cmd}`);
|
|
63
|
+
terminal.show();
|
|
64
|
+
}
|
|
65
|
+
function runScan() {
|
|
66
|
+
runTerminalCommand("npx ai-localize scan");
|
|
67
|
+
}
|
|
68
|
+
function runExtract() {
|
|
69
|
+
runTerminalCommand("npx ai-localize extract");
|
|
70
|
+
}
|
|
71
|
+
function runValidate() {
|
|
72
|
+
runTerminalCommand("npx ai-localize validate");
|
|
73
|
+
}
|
|
74
|
+
async function jumpToLocale() {
|
|
75
|
+
const folder = vscode.workspace.workspaceFolders?.[0];
|
|
76
|
+
if (!folder) return;
|
|
77
|
+
const localesPath = path.join(folder.uri.fsPath, "locales", "en", "translation.json");
|
|
78
|
+
try {
|
|
79
|
+
const doc = await vscode.workspace.openTextDocument(localesPath);
|
|
80
|
+
await vscode.window.showTextDocument(doc);
|
|
81
|
+
} catch {
|
|
82
|
+
vscode.window.showWarningMessage("Locale file not found. Run: ai-localize extract first.");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function scanDocument(doc) {
|
|
86
|
+
const supportedLangs = ["typescript", "typescriptreact", "javascript", "javascriptreact", "vue"];
|
|
87
|
+
if (!supportedLangs.includes(doc.languageId)) return;
|
|
88
|
+
const diagnostics = [];
|
|
89
|
+
const text = doc.getText();
|
|
90
|
+
const jsxTextRegex = />([^<>{}\n]{3,80})</g;
|
|
91
|
+
let match;
|
|
92
|
+
while ((match = jsxTextRegex.exec(text)) !== null) {
|
|
93
|
+
const str = match[1].trim();
|
|
94
|
+
if (!str || !/[a-zA-Z]/.test(str)) continue;
|
|
95
|
+
if (/^[a-z][a-z0-9_.]+$/.test(str)) continue;
|
|
96
|
+
const startPos = doc.positionAt(match.index + 1);
|
|
97
|
+
const endPos = doc.positionAt(match.index + match[0].length - 1);
|
|
98
|
+
const range = new vscode.Range(startPos, endPos);
|
|
99
|
+
const diag = new vscode.Diagnostic(
|
|
100
|
+
range,
|
|
101
|
+
`Hardcoded text: "${str}". Use AI Localize: Extract Locale Keys to fix.`,
|
|
102
|
+
vscode.DiagnosticSeverity.Warning
|
|
103
|
+
);
|
|
104
|
+
diag.source = "ai-localize";
|
|
105
|
+
diagnostics.push(diag);
|
|
106
|
+
}
|
|
107
|
+
diagnosticCollection.set(doc.uri, diagnostics);
|
|
108
|
+
}
|
|
109
|
+
function deactivate() {
|
|
110
|
+
diagnosticCollection?.dispose();
|
|
111
|
+
}
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
activate,
|
|
115
|
+
deactivate
|
|
116
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-localize-vscode",
|
|
3
|
+
"displayName": "AI Localize",
|
|
4
|
+
"description": "Highlight hardcoded text, extract locale keys, and validate translations",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"engines": {
|
|
7
|
+
"vscode": "^1.85.0"
|
|
8
|
+
},
|
|
9
|
+
"categories": [
|
|
10
|
+
"Other",
|
|
11
|
+
"Linters"
|
|
12
|
+
],
|
|
13
|
+
"activationEvents": [
|
|
14
|
+
"onLanguage:typescript",
|
|
15
|
+
"onLanguage:typescriptreact",
|
|
16
|
+
"onLanguage:javascript",
|
|
17
|
+
"onLanguage:vue"
|
|
18
|
+
],
|
|
19
|
+
"main": "./dist/extension.js",
|
|
20
|
+
"contributes": {
|
|
21
|
+
"commands": [
|
|
22
|
+
{
|
|
23
|
+
"command": "aiLocalize.scan",
|
|
24
|
+
"title": "AI Localize: Scan for Hardcoded Text"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"command": "aiLocalize.extract",
|
|
28
|
+
"title": "AI Localize: Extract Locale Keys"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"command": "aiLocalize.validate",
|
|
32
|
+
"title": "AI Localize: Validate Locale Files"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"command": "aiLocalize.jumpToLocale",
|
|
36
|
+
"title": "AI Localize: Jump to Locale File"
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
"configuration": {
|
|
40
|
+
"title": "AI Localize",
|
|
41
|
+
"properties": {
|
|
42
|
+
"aiLocalize.enabled": {
|
|
43
|
+
"type": "boolean",
|
|
44
|
+
"default": true
|
|
45
|
+
},
|
|
46
|
+
"aiLocalize.configPath": {
|
|
47
|
+
"type": "string",
|
|
48
|
+
"default": ""
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"ai-localize-shared": "1.0.0",
|
|
55
|
+
"ai-localize-config": "1.0.0",
|
|
56
|
+
"ai-localize-scanner": "1.0.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/vscode": "^1.85.0",
|
|
60
|
+
"tsup": "^8.0.1",
|
|
61
|
+
"typescript": "^5.3.3"
|
|
62
|
+
},
|
|
63
|
+
"license": "MIT",
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsup src/extension.ts --format cjs --external vscode",
|
|
66
|
+
"watch": "tsup src/extension.ts --format cjs --external vscode --watch"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as vscode from 'vscode';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
|
|
4
|
+
let diagnosticCollection: vscode.DiagnosticCollection;
|
|
5
|
+
|
|
6
|
+
export function activate(context: vscode.ExtensionContext): void {
|
|
7
|
+
diagnosticCollection = vscode.languages.createDiagnosticCollection('ai-localize');
|
|
8
|
+
context.subscriptions.push(diagnosticCollection);
|
|
9
|
+
|
|
10
|
+
context.subscriptions.push(
|
|
11
|
+
vscode.commands.registerCommand('aiLocalize.scan', runScan),
|
|
12
|
+
vscode.commands.registerCommand('aiLocalize.extract', runExtract),
|
|
13
|
+
vscode.commands.registerCommand('aiLocalize.validate', runValidate),
|
|
14
|
+
vscode.commands.registerCommand('aiLocalize.jumpToLocale', jumpToLocale)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
context.subscriptions.push(
|
|
18
|
+
vscode.workspace.onDidSaveTextDocument(async (doc) => {
|
|
19
|
+
const config = vscode.workspace.getConfiguration('aiLocalize');
|
|
20
|
+
if (!config.get('enabled', true)) return;
|
|
21
|
+
await scanDocument(doc);
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
vscode.window.showInformationMessage('AI Localize extension activated');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function runTerminalCommand(cmd: string): void {
|
|
29
|
+
const folder = vscode.workspace.workspaceFolders?.[0];
|
|
30
|
+
if (!folder) return;
|
|
31
|
+
const terminal = vscode.window.createTerminal('AI Localize');
|
|
32
|
+
terminal.sendText(`cd "${folder.uri.fsPath}" && ${cmd}`);
|
|
33
|
+
terminal.show();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function runScan(): void { runTerminalCommand('npx ai-localize scan'); }
|
|
37
|
+
function runExtract(): void { runTerminalCommand('npx ai-localize extract'); }
|
|
38
|
+
function runValidate(): void { runTerminalCommand('npx ai-localize validate'); }
|
|
39
|
+
|
|
40
|
+
async function jumpToLocale(): Promise<void> {
|
|
41
|
+
const folder = vscode.workspace.workspaceFolders?.[0];
|
|
42
|
+
if (!folder) return;
|
|
43
|
+
const localesPath = path.join(folder.uri.fsPath, 'locales', 'en', 'translation.json');
|
|
44
|
+
try {
|
|
45
|
+
const doc = await vscode.workspace.openTextDocument(localesPath);
|
|
46
|
+
await vscode.window.showTextDocument(doc);
|
|
47
|
+
} catch {
|
|
48
|
+
vscode.window.showWarningMessage('Locale file not found. Run: ai-localize extract first.');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function scanDocument(doc: vscode.TextDocument): Promise<void> {
|
|
53
|
+
const supportedLangs = ['typescript', 'typescriptreact', 'javascript', 'javascriptreact', 'vue'];
|
|
54
|
+
if (!supportedLangs.includes(doc.languageId)) return;
|
|
55
|
+
|
|
56
|
+
const diagnostics: vscode.Diagnostic[] = [];
|
|
57
|
+
const text = doc.getText();
|
|
58
|
+
const jsxTextRegex = />([^<>{}\n]{3,80})</g;
|
|
59
|
+
let match: RegExpExecArray | null;
|
|
60
|
+
|
|
61
|
+
while ((match = jsxTextRegex.exec(text)) !== null) {
|
|
62
|
+
const str = match[1].trim();
|
|
63
|
+
if (!str || !/[a-zA-Z]/.test(str)) continue;
|
|
64
|
+
if (/^[a-z][a-z0-9_.]+$/.test(str)) continue;
|
|
65
|
+
|
|
66
|
+
const startPos = doc.positionAt(match.index + 1);
|
|
67
|
+
const endPos = doc.positionAt(match.index + match[0].length - 1);
|
|
68
|
+
const range = new vscode.Range(startPos, endPos);
|
|
69
|
+
|
|
70
|
+
const diag = new vscode.Diagnostic(
|
|
71
|
+
range,
|
|
72
|
+
`Hardcoded text: "${str}". Use AI Localize: Extract Locale Keys to fix.`,
|
|
73
|
+
vscode.DiagnosticSeverity.Warning
|
|
74
|
+
);
|
|
75
|
+
diag.source = 'ai-localize';
|
|
76
|
+
diagnostics.push(diag);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
diagnosticCollection.set(doc.uri, diagnostics);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function deactivate(): void {
|
|
83
|
+
diagnosticCollection?.dispose();
|
|
84
|
+
}
|