Skip to content

Commit fa546b8

Browse files
committed
注册 i18n
1 parent 761a510 commit fa546b8

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

package.json

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@
106106
"language": "lua"
107107
}
108108
],
109+
"jsonValidation": [
110+
{
111+
"fileMatch": ".emmyrc.json",
112+
"url": "emmyrc-schema://schemas/emmyrc"
113+
},
114+
{
115+
"fileMatch": ".emmyrc.json",
116+
"url": "./syntaxes/schema.json"
117+
}
118+
],
109119
"debuggers": [
110120
{
111121
"type": "emmylua_attach",
@@ -961,7 +971,10 @@
961971
"markdownDescription": "%config.emmylua.colors.local.description%"
962972
},
963973
"emmylua.colors.mutable_underline": {
964-
"type": ["boolean", "null"],
974+
"type": [
975+
"boolean",
976+
"null"
977+
],
965978
"default": null,
966979
"markdownDescription": "%config.emmylua.colors.mutableUnderline.description%",
967980
"markdownDeprecationMessage": "Use `emmylua.colors.mutableUnderline` instead"
@@ -1002,7 +1015,10 @@
10021015
"markdownDescription": "%config.emmylua.ls.executablePath.description%"
10031016
},
10041017
"emmylua.misc.executablePath": {
1005-
"type": ["string", "null"],
1018+
"type": [
1019+
"string",
1020+
"null"
1021+
],
10061022
"default": null,
10071023
"markdownDescription": "%config.emmylua.ls.executablePath.description%",
10081024
"markdownDeprecationMessage": "Use `emmylua.ls.executablePath` instead"
@@ -1013,7 +1029,10 @@
10131029
"markdownDescription": "%config.emmylua.ls.globalConfigPath.description%"
10141030
},
10151031
"emmylua.misc.globalConfigPath": {
1016-
"type": ["string", "null"],
1032+
"type": [
1033+
"string",
1034+
"null"
1035+
],
10171036
"default": null,
10181037
"markdownDescription": "%config.emmylua.ls.globalConfigPath.description%",
10191038
"markdownDeprecationMessage": "Use `emmylua.ls.globalConfigPath` instead"
@@ -1030,7 +1049,10 @@
10301049
"markdownDescription": "%config.lua.trace.server.description%"
10311050
},
10321051
"emmylua.ls.debugPort": {
1033-
"type": ["integer", "null"],
1052+
"type": [
1053+
"integer",
1054+
"null"
1055+
],
10341056
"default": null,
10351057
"markdownDescription": "Look for language server on this port instead of starting it from an executable"
10361058
}
@@ -1047,12 +1069,6 @@
10471069
"editor.wordBasedSuggestions": "off"
10481070
}
10491071
},
1050-
"jsonValidation": [
1051-
{
1052-
"fileMatch": ".emmyrc.json",
1053-
"url": "./syntaxes/schema.json"
1054-
}
1055-
],
10561072
"colors": [],
10571073
"semanticTokenScopes": [
10581074
{
@@ -1201,4 +1217,4 @@
12011217
"vscode-languageclient": "9.0.1",
12021218
"concat-map": "0.0.2"
12031219
}
1204-
}
1220+
}

src/extension.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ let luaRocksTreeProvider: LuaRocksTreeProvider | undefined;
3232
export async function activate(context: vscode.ExtensionContext) {
3333
console.log('EmmyLua extension activated!');
3434

35+
// 提供`.emmyrc.json`的 i18n
36+
context.subscriptions.push(
37+
vscode.workspace.registerTextDocumentContentProvider('emmyrc-schema', new EmmyrcSchemaContentProvider(context))
38+
);
39+
3540
extensionContext = new EmmyContext(
3641
process.env['EMMY_DEV'] === 'true',
3742
context
@@ -433,7 +438,7 @@ async function showPackageInfo(item: PackageTreeItem): Promise<void> {
433438
const quickPick = vscode.window.createQuickPick();
434439
quickPick.title = `Package: ${packageInfo.name}`;
435440
quickPick.placeholder = 'Package Information';
436-
441+
437442
const items: vscode.QuickPickItem[] = [
438443
{
439444
label: `$(package) ${packageInfo.name}`,
@@ -554,3 +559,56 @@ async function checkLuaRocksInstallation(): Promise<void> {
554559
}
555560
}
556561
}
562+
563+
564+
565+
/**
566+
* 提供`.emmyrc.json`的 i18n
567+
*/
568+
class EmmyrcSchemaContentProvider implements vscode.TextDocumentContentProvider {
569+
private readonly schemaBaseDir: string;
570+
571+
constructor(context: vscode.ExtensionContext) {
572+
this.schemaBaseDir = path.join(context.extensionPath, 'syntaxes');
573+
}
574+
575+
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
576+
const schemaIdentifier = path.posix.basename(uri.path);
577+
const locale = vscode.env.language;
578+
let schemaFileName: string;
579+
580+
if (schemaIdentifier === 'emmyrc') {
581+
switch (locale) {
582+
case 'zh-cn':
583+
case 'zh-CN':
584+
case 'zh':
585+
schemaFileName = 'schema.zh-cn.json';
586+
break;
587+
case 'en':
588+
case 'en-US':
589+
case 'en-GB':
590+
default:
591+
schemaFileName = 'schema.json';
592+
break;
593+
}
594+
} else {
595+
return '';
596+
}
597+
598+
// 检查schema文件是否存在, 如果不存在则使用默认的
599+
let schemaFilePath = path.join(this.schemaBaseDir, schemaFileName);
600+
if (!fs.existsSync(schemaFilePath)) {
601+
schemaFilePath = path.join(this.schemaBaseDir, 'schema.json');
602+
}
603+
604+
try {
605+
return await fs.promises.readFile(schemaFilePath, 'utf8');
606+
} catch (error: any) {
607+
return JSON.stringify({
608+
"$schema": "https://json-schema.org/draft/2020-12/schema#",
609+
"title": "Error Loading Schema",
610+
"description": `Could not load schema: ${schemaFileName}. Error: ${error.message}.`
611+
});
612+
}
613+
}
614+
}

0 commit comments

Comments
 (0)