-
Notifications
You must be signed in to change notification settings - Fork 118
/
comparator.html
787 lines (706 loc) · 21.9 KB
/
comparator.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<!DOCTYPE html>
<!-- 用于对比当前已有中文json和新版本的英文json,快速得到新版本英文json中增加的内容,这样就可以快速进行翻译 -->
<html lang="zh-CN">
<head>
<title>自动翻译脚本</title>
<!-- ====Meta==== -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- ====Script Libraries==== -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.0.0/crypto-js.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- ====Others Libraries==== -->
<link rel="icon" type="images/x-icon" href="https://upload.wikimedia.org/wikipedia/commons/d/db/Google_Translate_Icon.png?20160129085523">
<!-- ====Style==== -->
<style>
/* ====Global==== */
li{
list-style: none;
}
fieldset{
padding: 0.5em;
margin: 1px;
}
button:hover{
cursor: pointer;
}
/* ====Specific==== */
header{
height: 32px;
}
.header-anchor{
display: inline-block;
padding: 0;
margin: 0;
height: 100%;
}
.header-anchor-logo{
display: inline-block;
padding: 1px;
height: 30px;
}
.APIConfig{
display: flex;
flex-direction: column;
gap: 5px;
}
.input-group{
display: flex;
align-items: center;
}
.input-group label{
width: 200px;
margin-right: 10px;
}
.APIConfig button{
width: 200px;
}
.introduction{
/* style */
background-color: #F5F5F5;
}
.introduction-list,
.introduction-list-li{
/* box */
padding: 0;
margin: 0 0 0 4px;
/* style */
list-style: decimal;
}
.operation{
/* layout */
display: grid;
grid-template-columns: 5fr 1fr;
}
.console{
/* layout */
display: grid;
grid-template-rows: 2em 2em 2em 1fr auto;
}
.console-btns{
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1em;
justify-items: center;
height: 2em;
width: 100%;
}
.console-btns button,
.console-btns2 button{
width: 100%;
height: 100%;
}
.console-btns2{
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 1em;
justify-items: center;
height: 2em;
width: 100%;
}
#textJson{
width: 100%;
height: 95%;
overflow: auto;
margin-top: 10px;
resize: none;
}
.clickable {
cursor: pointer;
position: relative;
}
.clickable::after {
content: '';
display: inline-block;
margin-left: 5px;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid black;
transform: rotate(0deg);
transition: transform 0.2s ease;
}
.clickable-active::after {
transform: rotate(180deg);
}
#result{
display: flex;
flex-direction: row;
width: 100%;
}
.sp{
width: 30%
}
/* ====Media Query==== */
@media (max-width: 1001px){
.operation{
display: grid;
grid-template-columns: 2fr 1fr;
}
}
</style>
</head>
<body>
<header class="header">
<a class="header-anchor" href="https://github.com/yk47g/gitkraken-chinese" target="_blank">
<img class="header-anchor-logo" src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png" alt="关于我们的GitHub项目">
</a>
</header>
<div id="app">
<form @submit.prevent="saveKeys">
<fieldset class="APIConfig">
<legend>API 配置</legend>
<!-- 下拉菜单选择API -->
<div class="input-group">
<label for="apiSelector">选择翻译 API:</label>
<select id="apiSelector" v-model="selectedApi">
<option value="youdao">有道API</option>
<option value="openai">OpenAI API</option>
</select>
</div>
<!-- 有道 API 配置 -->
<div v-if="selectedApi === 'youdao'">
<div class="input-group">
<label for="appKey">有道AppKey:</label>
<input type="text" id="appKey" v-model="youdao.appKey" placeholder="也就是应用ID">
</div>
<div class="input-group">
<label for="appSecret">有道AppSecret:</label>
<input type="text" id="appSecret" v-model="youdao.appSecret" placeholder="也就是应用秘钥">
</div>
</div>
<!-- OpenAI API 配置 -->
<div v-if="selectedApi === 'openai'">
<div class="input-group">
<label for="openaiApiKey">OpenAI API 密钥:</label>
<input type="text" id="openaiApiKey" v-model="openai.apiKey" placeholder="通常以 sk- 开头">
</div>
<div class='input-group'>
<label for='modelSelector'>选择模型:</label>
<select id='modelSelector' v-model='openai.model'>
<option value='gpt-4o-mini'>gpt-4o-mini</option>
<option value='gpt-3.5-turbo'>gpt-3.5-turbo</option>
<option value='gpt-4'>gpt-4</option>
</select>
</div>
</div>
<button type="submit">保存</button>
</fieldset>
</form>
<div class="operation">
<fieldset class="console">
<legend>控制台</legend>
<div class="console-btns">
<button @click="doCompare">对比</button>
<button @click="autoGen">自动生成json</button>
<button @click="autoTranslate">自动翻译</button>
</div>
<div class="input-group">
<label for="newFile">上传新版文件:</label>
<input type="file" id="newFile" @change="loadFile('en')">
</div>
<div class="input-group">
<label for="oldFile">上传旧版翻译文件:</label>
<input type="file" id="oldFile" @change="loadFile('zh')">
</div>
<div class="console-textarea">
<label for="textJson"></label>
<textarea id="textJson"></textarea>
</div>
<div class="console-btns2">
<button @click="exportJson">导出json文件</button>
<button @click="copyToClipboard">复制到剪切板</button>
</div>
</fieldset>
<fieldset class="introduction">
<legend>使用说明</legend>
<b>对于有提供支持的API的用户:</b>
<ol class="introduction-list">
<li class="introduction-list-li">
填写API配置并保存
</li>
<li class="introduction-list-li">
上传新版本文件和旧版翻译文件并点击 [对比] 按钮得到差异内容
</li>
<li class="introduction-list-li">
点击 [自动翻译] 按钮翻译并等待弹出"翻译完成"提示框
</li>
<li class="introduction-list-li">
导出并覆盖 strings.json 文件
</li>
<li class="introduction-list-li">
重启 GitKraken
</li>
</ol>
<b>对于没有API的用户</b>
<ol class="introduction-list">
<li class="introduction-list-li">
上传新版本文件和旧版翻译文件并点击 [对比] 按钮得到差异内容
</li>
<li class="introduction-list-li">
点击 [自动生成json] 按钮生成整合后的 json 文件
</li>
<li class="introduction-list-li">
复制并翻译文本框内的文件
</li>
<li class="introduction-list-li">
将翻译好的内容使用
<a href="https://www.bejson.com/" target="_blank">
格式化工具
</a>
整理
</li>
<li class="introduction-list-li">
重启 GitKraken
</li>
</ol>
<b>可能出现的问题和解决建议</b>
<a href="https://github.com/yk47g/gitkraken-chinese/issues">
报告问题
</a>
<ol class="introduction-list">
<li class="introduction-list-li">
使用API翻译后并没有插入新增内容
<ul>
<li style="list-style: circle">
确认使用了VS Code 的 Live Server
</li>
<li style="list-style: circle">
检查API是否正确配置(有效性,模型访问权限)
</li>
</ul>
</li>
<li class="introduction-list-li">
电脑变得非常卡
<ul>
<li style="list-style: circle">
由于JSON文件较大,卡顿是正常的
</li>
</ul>
</li>
<li class="introduction-list-li">
按下[自动翻译]没反应
<ul>
<li style="list-style: circle">
请检查"中文 共 xx 个"条目下的数字有无更新,如果有则是正常运行
</li>
</ul>
</li>
</ol>
<b :class="{'clickable': true, 'clickable-active': showYoudao}" @click="toggleYoudao">有道API的申请步骤</b>
<div v-if="showYoudao">
<ol class="introduction-list">
<li class="introduction-list-li">
进入 <a href="https://ai.youdao.com/console/#/service-singleton/text-translation" target="_blank">有道智云控制台</a>
</li>
<li class="introduction-list-li">
注册或登录
</li>
<li class="introduction-list-li">
在侧边栏选择 "自然语言翻译服务/文本翻译"
</li>
<li class="introduction-list-li">
找到 "文本翻译/应用概览/创建应用"
</li>
<li class="introduction-list-li">
创建应用并复制 AppKey 和 AppSecret
</li>
<li class="introduction-list-li">
在 comparator.html 文件中填写相关信息
</li>
</ol>
<i>
<a href="https://ai.youdao.com/price-center.s#servicename=fanyi-text" target="_blank">收费标准</a>
</i>
</div>
<br>
<b :class="{'clickable': true, 'clickable-active': showOpenAI}" @click="toggleOpenAI">OpenAI API的申请步骤</b>
<div v-if="showOpenAI">
<ol class="introduction-list">
<li class="introduction-list-li">
进入 <a href="https://platform.openai.com/api-keys" target="_blank">OpenAI 官方网站</a>
</li>
<li class="introduction-list-li">
注册或登录
</li>
<li class="introduction-list-li">
在顶栏左上角新建Project(可选)或使用用户密钥(可选,不推荐),之后在顶栏右上角选择 "Dashboard", 随后侧边栏选择 "API Keys"
</li>
<li class="introduction-list-li">
通过 "+ Create New secret key" 按照提示新建一个密钥
</li>
<li class="introduction-list-li">
<b style="color: #FF0000">请保管好你的密钥,平台将不会再次展示你的密钥,忘记则意味着你需要重新创建密钥并删除旧的密钥</b>
</li>
<li class="introduction-list-li">
在 comparator.html 文件中填写相关信息, 建议使用 "gpt-4o-mini" 模型, 足以保证翻译效果(而且免费限额高,token还便宜)
</li>
</ol>
<i>
<a href="https://platform.openai.com/docs/models/gpt-4o-mini" target="_blank">收费标准</a>
</i>
</div>
</fieldset>
</div>
<div id="result">
<div id="en" class="sp">
<div class="title">
英文 共 {{menuStringsEN.length}} 个
</div>
<ul>
<li v-for="(item, index) in menuStringsEN" :key="index">{{index + 1}}:{{item.key}}</li>
</ul>
</div>
<div id="zh" class="sp">
<div class="title">
中文 共 {{menuStringsZH.length}} 个
</div>
<ul>
<li v-for="(item, index) in menuStringsZH" :key="index">{{index + 1}}:{{item.key}}</li>
</ul>
</div>
<div id="cy" class="sp">
<div class="title">
中文没有的 共 {{menuStringsCY.length}} 个
</div>
<ul>
<li v-for="(item, index) in menuStringsCY" :key="index">{{index + 1}}:{{item.key}}</li>
</ul>
</div>
</div>
</div>
</body>
</html>
<script>
const app = new Vue({
el: '#app',
data() {
return {
showYoudao: false,
showOpenAI: false,
menuStringsEN: [],
menuStringsZH: [],
menuStringsCY: [],
sourceEN: {},
// 默认 API
selectedApi: 'youdao',
// 有道 API 配置
youdao: {
appKey: '',
appSecret: '',
salt: (new Date).getTime(),
from: 'en',
to: 'zh-CHS',
},
// OpenAI API 配置
openai: {
apiKey: '',
// 默认模型
model: 'gpt-4o-mini',
temperature: 0.7,
}
}
},
methods: {
/**
* 导出JSON文件
*/
exportJson() {
const content = document.getElementById("textJson").value;
if (!content) {
alert("没有可以导出的内容");
return;
}
const blob = new Blob([content], { type: "application/json" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "strings.json";
link.click();
URL.revokeObjectURL(link.href);
},
/**
* 复制到剪切板
*
* @returns {Promise<void>} 标明方法类型,不用管
*/
async copyToClipboard() {
const content = document.getElementById("textJson").value;
if (!content) {
alert("没有可以复制的内容");
return;
}
try {
await navigator.clipboard.writeText(content);
} catch (err) {
alert("复制失败,请手动复制");
}
},
/**
* 显示/隐藏内容
*
* @param id 要显示/隐藏的内容的元素id
*/
toggleContent(id) {
const content = document.getElementById(`${id}-content`);
if (content.classList.contains('hidden')) {
content.classList.remove('hidden');
content.classList.add('visible');
} else {
content.classList.remove('visible');
content.classList.add('hidden');
}
},
toggleYoudao() {
this.showYoudao = !this.showYoudao;
},
toggleOpenAI() {
this.showOpenAI = !this.showOpenAI;
},
/**
* 删除上传的旧文件,读取上传的新文件
*
* @param type 上传的文件类型
*/
loadFile(type) {
const input = type === 'en' ? document.getElementById('newFile') : document.getElementById('oldFile');
const file = input.files[0];
if (!file) {
alert("请选择文件");
return;
}
if (type === 'en') {
this.menuStringsEN = [];
this.sourceEN = {};
} else {
this.menuStringsZH = [];
}
const reader = new FileReader();
reader.onload = e => {
try {
const json = JSON.parse(e.target.result);
if (type === 'en') {
this.sourceEN = json;
this.processJsonKeys(json, 'en');
} else {
this.processJsonKeys(json, 'zh');
}
} catch (err) {
alert("文件内容无效,无法解析为JSON");
}
};
reader.readAsText(file);
},
processJsonKeys(json, type) {
let key;
const menuStrings = json['menuStrings'];
for (key in menuStrings) {
if (type === 'en') {
this.menuStringsEN.push({
key: key,
value: menuStrings[key],
scope: 'menuStrings',
});
} else {
this.menuStringsZH.push({
key: key,
value: menuStrings[key],
scope: 'menuStrings',
});
}
}
const strings = json['strings'];
for (key in strings) {
if (type === 'en') {
this.menuStringsEN.push({
key: key,
value: strings[key],
scope: 'strings',
});
} else {
this.menuStringsZH.push({
key: key,
value: strings[key],
scope: 'strings',
});
}
}
},
doCompare() {
if (this.menuStringsEN.length === 0 || this.menuStringsZH.length === 0) {
alert("请先上传新版本文件和旧版翻译文件,然后进行对比");
return;
}
let vm = this;
vm.menuStringsCY = [];
vm.menuStringsEN.forEach(en => {
let idx = vm.menuStringsZH.some((zh) => zh.key === en.key);
if (!idx) {
vm.menuStringsCY.push(en);
}
});
if (vm.menuStringsCY.length === 0) {
alert("没有找到差异内容");
}
},
autoGen() {
if (this.menuStringsZH.length === 0 || Object.keys(this.sourceEN).length === 0) {
alert("没有可生成的 JSON 内容,请先上传文件并进行对比或翻译");
return;
}
for (const item of this.menuStringsZH) {
if (this.sourceEN[item.scope][item.key]) {
this.sourceEN[item.scope][item.key] = item.value;
}
}
document.getElementById("textJson").innerHTML = JSON.stringify(this.sourceEN, null, 2);
},
async autoTranslate() {
if (this.menuStringsCY.length === 0) {
alert('没有需要翻译的内容');
return;
}
const oneCount = 20;
for (let i = 0; i < this.menuStringsCY.length; i += oneCount) {
const data = this.menuStringsCY.slice(i, i + oneCount);
const query = data.map(m => m.value).join('\n');
//根据选项调用API
let result = [];
if (this.selectedApi === 'youdao') {
result = await this.translateYoudao(query);
} else if (this.selectedApi === 'openai') {
result = await this.translateOpenAI(query);
}
for (let j = 0; j < data.length; j++) {
const deep = JSON.parse(JSON.stringify(data[j]));
deep.value = result[j];
this.menuStringsZH.push(deep);
}
//避免请求过于频繁
await this.sleep(1500);
}
this.autoGen();
alert('翻译并整理完成');
},
/**
* 调用有道翻译
*
* @param query 翻译内容
* @return {Promise<unknown>} 翻译结果
*/
async translateYoudao(query) {
const curtime = Math.round(new Date().getTime() / 1000);
const sign = this.buildSign(query, curtime);
const url = 'https://openapi.youdao.com/api';
const data = {
q: query,
appKey: this.youdao.appKey,
salt: this.youdao.salt,
from: this.youdao.from,
to: this.youdao.to,
sign: sign,
signType: "v3",
curtime: curtime,
};
return new Promise(resolve => {
$.ajax({
url: url,
type: 'POST',
dataType: 'jsonp',
data: data,
success: function (data) {
if (data.errorCode !== '0') {
console.log(data);
resolve([]);
return;
}
const result = data.translation[0].split('\n');
resolve(result);
}
});
});
},
/**
* 调用 OpenAI 翻译
*
* @param query 翻译内容
* @return {Promise<string[]|*[]>} 翻译结果
*/
async translateOpenAI(query) {
const messages = [{
"role": "system",
"content": "Translate the following text to Chinese."
}, {
"role": "user",
"content": query
}];
const data = {
model: this.openai.model,
messages: messages,
temperature: this.openai.temperature
};
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
data,
{
headers: {
'Authorization': `Bearer ${this.openai.apiKey}`,
'Content-Type': 'application/json'
}
}
);
const result = response.data.choices[0].message.content.trim();
return result.split('\n');
} catch (error) {
console.error("OpenAI API 错误:", error);
return [];
}
},
buildSign(query, curtime) {
const str = this.youdao.appKey + this.truncate(query) + this.youdao.salt + curtime + this.youdao.appSecret;
return CryptoJS.SHA256(str).toString(CryptoJS.enc.Hex);
},
truncate(q) {
const len = q.length;
if (len <= 20) return q;
return q.substring(0, 10) + len + q.substring(len - 10, len);
},
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
},
saveKeys() {
// 获取 AppKey和 AppSecret 或 OpenAI API 密钥的表单内容
if (this.selectedApi === 'youdao') {
const appKey = this.youdao.appKey;
const appSecret = this.youdao.appSecret;
if (!appKey) {
alert("请填写有道AppKey");
return;
}
if (!appSecret) {
alert("请填写有道AppSecret");
return;
}
this.youdao.appKey = appKey;
this.youdao.appSecret = appSecret;
alert('有道API配置已保存');
} else if (this.selectedApi === 'openai') {
const apiKey = this.openai.apiKey;
const model = this.openai.model;
if (!apiKey) {
alert("请填写OpenAI API密钥");
return;
}
if (!model) {
alert("请选择OpenAI模型");
return;
}
this.openai.apiKey = apiKey;
this.openai.model = model;
alert('OpenAI API配置已保存');
}
}
},
})
</script>