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