feat:vscode环境下根据设置语言自动选择l10n文件

This commit is contained in:
li1553770945 2025-06-29 22:55:50 +08:00
parent 318cf64ace
commit 7ff6f3b48e
2 changed files with 65 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { setRunningCWD, setVscodeWorkspace } from '../openmcp-sdk/service/index.js'; import { setRunningCWD, setVscodeWorkspace } from '../openmcp-sdk/service/index.js';
import { launch } from './common/entry.js'; import { launch } from './common/entry.js';
import { initialiseI18n } from './i18n/index.js'; import { initialiseI18n, getAvailableKeys } from './i18n/index.js';
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
console.log('activate openmcp'); console.log('activate openmcp');
@ -13,6 +13,11 @@ export function activate(context: vscode.ExtensionContext) {
setVscodeWorkspace(workspace); setVscodeWorkspace(workspace);
setRunningCWD(context.extensionPath); setRunningCWD(context.extensionPath);
initialiseI18n(context); initialiseI18n(context);
// 添加i18n调试信息
console.log('Current language:', vscode.env.language);
console.log('Available i18n keys:', getAvailableKeys().length);
launch(context); launch(context);
} }

View File

@ -1,26 +1,81 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
const defaultBundle: Record<string, string> = {} const defaultBundle: Record<string, string> = {}
export function initialiseI18n(context: vscode.ExtensionContext) { export function initialiseI18n(context: vscode.ExtensionContext) {
if (vscode.l10n.bundle === undefined) { if (vscode.l10n.bundle === undefined) {
const bundlePath = context.asAbsolutePath('l10n/bundle.l10n.en.json'); // 获取用户的语言设置
const bundle = JSON.parse(fs.readFileSync(bundlePath, { encoding: 'utf-8' })) as Record<string, string>; const userLanguage = vscode.env.language;
Object.assign(defaultBundle, bundle);
// 尝试加载用户语言对应的语言包
let bundlePath = context.asAbsolutePath(`l10n/bundle.l10n.${userLanguage}.json`);
// 如果用户语言的语言包不存在,回退到英语
if (!fs.existsSync(bundlePath)) {
bundlePath = context.asAbsolutePath('l10n/bundle.l10n.en.json');
}
try {
const bundle = JSON.parse(fs.readFileSync(bundlePath, { encoding: 'utf-8' })) as Record<string, string>;
Object.assign(defaultBundle, bundle);
} catch (error) {
console.error('Failed to load i18n bundle:', error);
// 如果加载失败,尝试加载英语包作为最后的回退
try {
const fallbackPath = context.asAbsolutePath('l10n/bundle.l10n.en.json');
const fallbackBundle = JSON.parse(fs.readFileSync(fallbackPath, { encoding: 'utf-8' })) as Record<string, string>;
Object.assign(defaultBundle, fallbackBundle);
} catch (fallbackError) {
console.error('Failed to load fallback i18n bundle:', fallbackError);
}
}
} }
} }
export function t(message: string, ...args: string[]): string { export function t(message: string, ...args: string[]): string {
if (vscode.l10n.bundle === undefined) { if (vscode.l10n.bundle === undefined) {
// 使用自定义的语言包
let translateMessage = defaultBundle[message] || message; let translateMessage = defaultBundle[message] || message;
for (let i = 0; i < args.length; ++ i) { // 替换占位符 {0}, {1}, {2} 等
for (let i = 0; i < args.length; i++) {
translateMessage = translateMessage.replace(`{${i}}`, args[i]); translateMessage = translateMessage.replace(`{${i}}`, args[i]);
} }
return translateMessage; return translateMessage;
} else { } else {
// 使用 VS Code 的 l10n API
return vscode.l10n.t(message, ...args); return vscode.l10n.t(message, ...args);
} }
} }
/**
* 使
*/
export function getCurrentLanguage(): string {
return vscode.env.language;
}
/**
*
*/
export function getAvailableKeys(): string[] {
if (vscode.l10n.bundle === undefined) {
return Object.keys(defaultBundle);
}
return [];
}
/**
*
*/
export function hasTranslation(key: string): boolean {
if (vscode.l10n.bundle === undefined) {
return key in defaultBundle;
}
// VS Code 的 l10n API 没有直接的方法检查,我们尝试翻译并比较
const translated = vscode.l10n.t(key);
return translated !== key;
}