-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.js
More file actions
146 lines (123 loc) · 4.45 KB
/
i18n.js
File metadata and controls
146 lines (123 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const translations = {
'zh-CN': {
'title': '验光系统',
// 导航菜单
'nav.calibration': '尺寸校准',
'nav.visualAcuity': 'E视力表',
'nav.duochrome': '红绿精调',
'nav.astigmatism': '散光盘',
'nav.honeycomb': '蜂窝视标',
'nav.cross': '交叉视标',
'nav.langSwitch': 'EN',
// 校准视图
'calibration.title': '物理尺寸校准',
'calibration.testDistance': '测试距离:',
'calibration.meters': '米',
'calibration.custom': '自定义',
'calibration.instruction': '请拖动滑块,使蓝色矩形等同信用卡大小 (85.6mm x 54mm)',
'calibration.cardText': 'CREDIT CARD',
'calibration.saveAndContinue': '保存校准并继续',
// E视力表
'va.startAcuity': '起始视力:',
'va.refresh': '刷新随机方向',
// 散光盘
'astig.diameter': '直径 (cm):',
'astig.dashLength': '虚线段长 (mm):',
'astig.lineWeight': '线条粗细 (mm):',
'astig.fontSize': '字体大小 (px):',
'astig.refresh': '刷新散光盘',
// 红绿精调
'duo.startAcuity': '起始视力:',
'duo.randomize': '随机换向',
// 蜂窝视标
'hc.diameter': '圆形直径 (cm):',
'hc.dotSize': '黑点直径 (mm):',
'hc.refresh': '刷新蜂窝视标',
// 交叉视标
'cross.diameter': '圆形直径 (cm):',
'cross.lineWidth': '线宽 (px):',
'cross.spacing': '线距 (mm):',
'cross.refresh': '刷新交叉视标'
},
'en-US': {
'title': 'Optometry System',
// Navigation
'nav.calibration': 'Calibration',
'nav.visualAcuity': 'E Chart',
'nav.duochrome': 'Duochrome',
'nav.astigmatism': 'Astigmatism',
'nav.honeycomb': 'Honeycomb',
'nav.cross': 'Cross',
'nav.langSwitch': '中文',
// Calibration view
'calibration.title': 'Calibration',
'calibration.testDistance': 'Distance:',
'calibration.meters': 'm',
'calibration.custom': 'Custom',
'calibration.instruction': 'Drag slider to match credit card size (85.6mm x 54mm)',
'calibration.cardText': 'CREDIT CARD',
'calibration.saveAndContinue': 'Save & Continue',
// Visual Acuity
'va.startAcuity': 'Start VA:',
'va.refresh': 'Refresh',
// Astigmatism
'astig.diameter': 'Dia (cm):',
'astig.dashLength': 'Dash (mm):',
'astig.lineWeight': 'Width (mm):',
'astig.fontSize': 'Font (px):',
'astig.refresh': 'Refresh',
// Duochrome
'duo.startAcuity': 'Start VA:',
'duo.randomize': 'Randomize',
// Honeycomb
'hc.diameter': 'Dia (cm):',
'hc.dotSize': 'Dot (mm):',
'hc.refresh': 'Refresh',
// Cross
'cross.diameter': 'Dia (cm):',
'cross.lineWidth': 'Width (px):',
'cross.spacing': 'Gap (mm):',
'cross.refresh': 'Refresh'
}
};
class I18n {
constructor() {
this.currentLang = localStorage.getItem('optometry_lang') || 'zh-CN';
}
t(key) {
return translations[this.currentLang]?.[key] || key;
}
setLanguage(lang) {
this.currentLang = lang;
localStorage.setItem('optometry_lang', lang);
this.updateUI();
}
toggleLanguage() {
const newLang = this.currentLang === 'zh-CN' ? 'en-US' : 'zh-CN';
this.setLanguage(newLang);
}
updateUI() {
// 更新所有带有 data-i18n 属性的元素
document.querySelectorAll('[data-i18n]').forEach(el => {
const key = el.getAttribute('data-i18n');
const translation = this.t(key);
// 根据元素类型更新文本
if (el.tagName === 'INPUT' && el.type === 'button') {
el.value = translation;
} else if (el.tagName === 'BUTTON') {
el.textContent = translation;
} else if (el.hasAttribute('data-i18n-placeholder')) {
el.placeholder = translation;
} else {
el.textContent = translation;
}
});
// 更新语言切换按钮
const langSwitcher = document.getElementById('lang-switcher');
if (langSwitcher) {
langSwitcher.textContent = this.t('nav.langSwitch');
}
}
}
// 创建全局实例
const i18n = new I18n();