Skip to content

Commit 658f96b

Browse files
committed
重构: 更新 CheekyChimp 插件以支持国际化
在插件中引入 i18n 服务,更新通知消息以使用国际化文本,替换所有与 Tampermonkey 相关的日志信息为 CheekyChimp,优化脚本注入逻辑,确保在不同环境下的兼容性和可读性。
1 parent cdd2a6c commit 658f96b

File tree

5 files changed

+726
-390
lines changed

5 files changed

+726
-390
lines changed

examples/demo.mp4

10.4 MB
Binary file not shown.

main.js

Lines changed: 117 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ScriptManager } from './services/script-manager';
44
import { ObsidianStorage } from './services/obsidian-storage';
55
import { ScriptInjector } from './services/script-injector';
66
import { UserScript } from './models/script';
7+
import { i18n } from './services/i18n-service';
78

89
export default class CheekyChimpPlugin extends Plugin {
910
settings: CheekyChimpSettings;
@@ -50,9 +51,9 @@ export default class CheekyChimpPlugin extends Plugin {
5051
const activeScripts = this.scriptManager.getAllScripts().filter(s => s.enabled);
5152
if (activeScripts.length > 0) {
5253
const scriptList = activeScripts.map(s => `- ${s.name}`).join('\n');
53-
new Notice(`当前启用的脚本 (${activeScripts.length}):\n${scriptList}`);
54+
new Notice(`${i18n.t('active_scripts')} (${activeScripts.length}):\n${scriptList}`);
5455
} else {
55-
new Notice('当前没有启用的脚本');
56+
new Notice(i18n.t('no_active_scripts'));
5657
}
5758

5859
// 打开设置页面
@@ -117,7 +118,7 @@ export default class CheekyChimpPlugin extends Plugin {
117118
}
118119
} else {
119120
// 如果没有setting API,退回到简单通知
120-
new Notice('无法打开设置,请从Obsidian设置中找到CheekyChimp标签');
121+
new Notice(i18n.t('error_open_settings'));
121122
}
122123
}
123124

@@ -130,7 +131,7 @@ export default class CheekyChimpPlugin extends Plugin {
130131
// 打开设置页面
131132
this.openSettings();
132133
// 通知设置页面打开特定脚本
133-
new Notice(`正在打开脚本 "${script.name}" 进行编辑`);
134+
new Notice(i18n.t('opening_script_editor', { name: script.name }));
134135
}
135136
}
136137

@@ -143,10 +144,10 @@ export default class CheekyChimpPlugin extends Plugin {
143144
// 生成针对当前URL的脚本模板
144145
const domain = new URL(url).hostname;
145146
const scriptTemplate = `// ==UserScript==
146-
// @name 针对 ${domain} 的脚本
147+
// @name Script for ${domain}
147148
// @namespace http://tampermonkey.net/
148149
// @version 0.1
149-
// @description ${domain} 添加功能
150+
// @description Add functionality to ${domain}
150151
// @author You
151152
// @match ${url}
152153
// @grant none
@@ -155,20 +156,20 @@ export default class CheekyChimpPlugin extends Plugin {
155156
(function() {
156157
'use strict';
157158
158-
// 在此处添加您的代码...
159-
console.log('Tampermonkey脚本已启动!');
159+
// Add your code here...
160+
console.log('CheekyChimp script running!');
160161
})();`;
161162

162163
try {
163164
// 添加脚本
164165
const script = this.scriptManager.addScript(scriptTemplate);
165-
new Notice(`已为 ${domain} 创建新脚本`);
166+
new Notice(i18n.t('script_created_for_domain', { domain }));
166167

167168
// 打开编辑器
168169
this.openScriptEditor(script.id);
169170
} catch (error) {
170171
console.error('创建脚本失败:', error);
171-
new Notice('创建脚本失败,请查看控制台');
172+
new Notice(i18n.t('error_creating_script'));
172173
}
173174
}
174175

src/services/i18n-service.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* 国际化服务,用于支持多语言
3+
*/
4+
export class I18nService {
5+
private translations: Record<string, Record<string, string>> = {
6+
'zh': {
7+
// 通用
8+
'plugin_name': 'CheekyChimp',
9+
'plugin_description': 'Obsidian用户脚本管理器',
10+
11+
// 设置
12+
'settings': '设置',
13+
'scripts_manager': '脚本管理器',
14+
'add_script': '添加脚本',
15+
'import_script': '导入脚本',
16+
'create_script': '创建脚本',
17+
'edit_script': '编辑脚本',
18+
'delete_script': '删除脚本',
19+
'enable_script': '启用脚本',
20+
'disable_script': '禁用脚本',
21+
'script_settings': '脚本设置',
22+
'active_scripts': '已启用的脚本',
23+
'no_active_scripts': '当前没有启用的脚本',
24+
25+
// 通知
26+
'script_added': '脚本已添加',
27+
'script_updated': '脚本已更新',
28+
'script_deleted': '脚本已删除',
29+
'script_enabled': '脚本已启用',
30+
'script_disabled': '脚本已禁用',
31+
'script_error': '脚本错误',
32+
'script_load_error': '加载脚本失败',
33+
'script_execute_error': '执行脚本失败',
34+
35+
// 错误
36+
'error_script_exists': '已存在同名脚本',
37+
'error_invalid_script': '无效的脚本',
38+
'error_script_not_found': '未找到脚本',
39+
'error_cross_origin': '跨域请求失败',
40+
'error_injection_failed': '注入脚本失败',
41+
42+
// 确认对话框
43+
'confirm_delete': '确定要删除此脚本吗?此操作不可撤销。',
44+
'confirm_yes': '是',
45+
'confirm_no': '否',
46+
'confirm_cancel': '取消',
47+
48+
// 日志
49+
'log_injecting': '正在注入脚本',
50+
'log_injection_success': '脚本注入成功',
51+
'log_injection_failure': '脚本注入失败'
52+
},
53+
'en': {
54+
// General
55+
'plugin_name': 'CheekyChimp',
56+
'plugin_description': 'UserScript Manager for Obsidian',
57+
58+
// Settings
59+
'settings': 'Settings',
60+
'scripts_manager': 'Script Manager',
61+
'add_script': 'Add Script',
62+
'import_script': 'Import Script',
63+
'create_script': 'Create Script',
64+
'edit_script': 'Edit Script',
65+
'delete_script': 'Delete Script',
66+
'enable_script': 'Enable Script',
67+
'disable_script': 'Disable Script',
68+
'script_settings': 'Script Settings',
69+
'active_scripts': 'Active Scripts',
70+
'no_active_scripts': 'No active scripts',
71+
72+
// Notifications
73+
'script_added': 'Script added',
74+
'script_updated': 'Script updated',
75+
'script_deleted': 'Script deleted',
76+
'script_enabled': 'Script enabled',
77+
'script_disabled': 'Script disabled',
78+
'script_error': 'Script error',
79+
'script_load_error': 'Failed to load script',
80+
'script_execute_error': 'Failed to execute script',
81+
82+
// Errors
83+
'error_script_exists': 'Script with the same name already exists',
84+
'error_invalid_script': 'Invalid script',
85+
'error_script_not_found': 'Script not found',
86+
'error_cross_origin': 'Cross-origin request failed',
87+
'error_injection_failed': 'Script injection failed',
88+
89+
// Confirmation dialogs
90+
'confirm_delete': 'Are you sure you want to delete this script? This cannot be undone.',
91+
'confirm_yes': 'Yes',
92+
'confirm_no': 'No',
93+
'confirm_cancel': 'Cancel',
94+
95+
// Logs
96+
'log_injecting': 'Injecting script',
97+
'log_injection_success': 'Script injection successful',
98+
'log_injection_failure': 'Script injection failed'
99+
}
100+
};
101+
102+
private currentLanguage: string = 'zh';
103+
104+
constructor() {
105+
// 尝试检测系统语言
106+
this.detectLanguage();
107+
}
108+
109+
/**
110+
* 检测系统语言
111+
*/
112+
private detectLanguage(): void {
113+
try {
114+
const lang = window.navigator.language;
115+
if (lang.startsWith('zh')) {
116+
this.currentLanguage = 'zh';
117+
} else {
118+
this.currentLanguage = 'en';
119+
}
120+
console.log(`CheekyChimp: 检测到系统语言: ${lang}, 使用: ${this.currentLanguage}`);
121+
} catch (e) {
122+
console.warn('CheekyChimp: 无法检测系统语言,使用默认语言(zh)');
123+
this.currentLanguage = 'zh';
124+
}
125+
}
126+
127+
/**
128+
* 设置当前语言
129+
*/
130+
setLanguage(lang: 'zh' | 'en'): void {
131+
if (this.translations[lang]) {
132+
this.currentLanguage = lang;
133+
} else {
134+
console.warn(`CheekyChimp: 不支持的语言 ${lang}, 使用默认语言(zh)`);
135+
this.currentLanguage = 'zh';
136+
}
137+
}
138+
139+
/**
140+
* 获取当前语言
141+
*/
142+
getLanguage(): string {
143+
return this.currentLanguage;
144+
}
145+
146+
/**
147+
* 获取翻译文本
148+
*/
149+
t(key: string, params?: Record<string, string>): string {
150+
const translations = this.translations[this.currentLanguage] || this.translations['zh'];
151+
let text = translations[key] || key;
152+
153+
// 替换参数
154+
if (params) {
155+
Object.keys(params).forEach(paramKey => {
156+
text = text.replace(`{${paramKey}}`, params[paramKey]);
157+
});
158+
}
159+
160+
return text;
161+
}
162+
}
163+
164+
// 创建单例实例
165+
export const i18n = new I18nService();

0 commit comments

Comments
 (0)