torchv-translate 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/translate.js/translate.js +178 -52
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "torchv-translate",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Two lines of js realize automatic html translation. No need to change the page, no language configuration file, no API key, SEO friendly!",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"exports": {
|
|
@@ -2184,27 +2184,37 @@ var translate = {
|
|
|
2184
2184
|
@param info 如果result为0,这里是失败信息
|
|
2185
2185
|
*/
|
|
2186
2186
|
isAllExecuteFinish:function(uuid, from, to, result, info){
|
|
2187
|
-
|
|
2188
|
-
translate.listener.execute.renderFinishByApiRun(uuid, from, to);
|
|
2189
|
-
|
|
2190
|
-
//通过 uuid、from 取得本次翻译相关的 texts、nodes , 触发 translateNetworkAfter_Trigger 钩子
|
|
2191
|
-
//获取请求日志
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
uuid
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2187
|
+
|
|
2188
|
+
translate.listener.execute.renderFinishByApiRun(uuid, from, to);
|
|
2189
|
+
|
|
2190
|
+
//通过 uuid、from 取得本次翻译相关的 texts、nodes , 触发 translateNetworkAfter_Trigger 钩子
|
|
2191
|
+
//获取请求日志
|
|
2192
|
+
if(typeof(translate.request) === 'object'
|
|
2193
|
+
&& typeof(translate.request.data) === 'object'
|
|
2194
|
+
&& typeof(translate.request.data[uuid]) === 'object'
|
|
2195
|
+
&& typeof(translate.request.data[uuid].list) === 'object'
|
|
2196
|
+
&& typeof(translate.request.data[uuid].list[from]) === 'object'
|
|
2197
|
+
&& typeof(translate.request.data[uuid].list[from][to]) === 'object'){
|
|
2198
|
+
var requestData = translate.request.data[uuid].list[from][to];
|
|
2199
|
+
translate.lifecycle.execute.translateNetworkAfter_Trigger({
|
|
2200
|
+
uuid: uuid,
|
|
2201
|
+
from: from,
|
|
2202
|
+
to: to,
|
|
2203
|
+
texts: requestData.texts,
|
|
2204
|
+
nodes: requestData.nodes,
|
|
2205
|
+
result: result,
|
|
2206
|
+
info: info
|
|
2207
|
+
});
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2210
|
+
//console.log('uuid:'+uuid+', from:'+from+', to:'+to);
|
|
2211
|
+
if(typeof(translate.translateRequest) !== 'object' || typeof(translate.translateRequest[uuid]) !== 'object'){
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
for(var lang in translate.translateRequest[uuid]){
|
|
2215
|
+
if (!translate.translateRequest[uuid].hasOwnProperty(lang)) {
|
|
2216
|
+
continue;
|
|
2217
|
+
}
|
|
2208
2218
|
if(translate.translateRequest[uuid][lang].executeFinish == 0){
|
|
2209
2219
|
//这个还没执行完,那么直接退出,不在向后执行了
|
|
2210
2220
|
//console.log('uuid:'+uuid+' lang:'+lang+' executeFinish:0 time:'+translate.translateRequest[uuid][lang][i][addtime]);
|
|
@@ -2233,6 +2243,10 @@ var translate = {
|
|
|
2233
2243
|
translate.cancelVersion = translate.cancelVersion + 1;
|
|
2234
2244
|
//清空等待队列
|
|
2235
2245
|
translate.waitingExecute.clear();
|
|
2246
|
+
//清空翻译请求去重缓存
|
|
2247
|
+
if(typeof(translate.request) === 'object' && typeof(translate.request.pending) === 'object'){
|
|
2248
|
+
translate.request.pending.clear();
|
|
2249
|
+
}
|
|
2236
2250
|
//终止当前执行状态
|
|
2237
2251
|
translate.state = 0;
|
|
2238
2252
|
//中断未完成的翻译请求
|
|
@@ -2552,6 +2566,7 @@ var translate = {
|
|
|
2552
2566
|
var uuid = translate.util.uuid();
|
|
2553
2567
|
translate.time.log('创建uuid:'+uuid);
|
|
2554
2568
|
var executeCancelVersion = translate.cancelVersion;
|
|
2569
|
+
var pendingKeysCreated = [];
|
|
2555
2570
|
|
|
2556
2571
|
//如果页面打开第一次使用,先判断缓存中有没有上次使用的语种,从缓存中取出
|
|
2557
2572
|
if(translate.to == null || translate.to == ''){
|
|
@@ -2967,12 +2982,12 @@ var translate = {
|
|
|
2967
2982
|
//console.log(originalWord);
|
|
2968
2983
|
|
|
2969
2984
|
//根据hash,判断本地是否有缓存了
|
|
2970
|
-
var cacheHash = originalWord == translateText ? hash:translate.util.hash(translateText); //如果匹配到了自定义术语库,那翻译前的hash是被改变了
|
|
2971
|
-
translate.nodeQueue[uuid]['list'][lang][hash]['cacheHash'] = cacheHash; //缓存的hash。 缓存时,其hash跟翻译的语言是完全对应的,缓存的hash就是翻译的语言转换来的
|
|
2972
|
-
var cache = translate.storage.get('hash_'+translate.to+'_'+cacheHash);
|
|
2973
|
-
|
|
2974
|
-
//缓存是否有拿到具体缓存内容
|
|
2975
|
-
if(cache != null && cache.length > 0){
|
|
2985
|
+
var cacheHash = originalWord == translateText ? hash:translate.util.hash(translateText); //如果匹配到了自定义术语库,那翻译前的hash是被改变了
|
|
2986
|
+
translate.nodeQueue[uuid]['list'][lang][hash]['cacheHash'] = cacheHash; //缓存的hash。 缓存时,其hash跟翻译的语言是完全对应的,缓存的hash就是翻译的语言转换来的
|
|
2987
|
+
var cache = translate.storage.get('hash_'+translate.to+'_'+cacheHash);
|
|
2988
|
+
|
|
2989
|
+
//缓存是否有拿到具体缓存内容
|
|
2990
|
+
if(cache != null && cache.length > 0){
|
|
2976
2991
|
for(var node_index = 0; node_index < translate.nodeQueue[uuid]['list'][lang][hash]['nodes'].length; node_index++){
|
|
2977
2992
|
//console.log(translate.nodeQueue[uuid]['list'][lang][hash]['nodes'][node_index]);
|
|
2978
2993
|
|
|
@@ -3055,14 +3070,27 @@ var translate = {
|
|
|
3055
3070
|
}
|
|
3056
3071
|
|
|
3057
3072
|
|
|
3058
|
-
|
|
3059
|
-
continue; //跳出,不用在传入下面的翻译接口了
|
|
3060
|
-
}
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3073
|
+
|
|
3074
|
+
continue; //跳出,不用在传入下面的翻译接口了
|
|
3075
|
+
}
|
|
3076
|
+
|
|
3077
|
+
//避免虚拟滚动等场景重复请求:如果同一文本正在翻译中,则加入等待队列
|
|
3078
|
+
if(typeof(translate.request) === 'object' && typeof(translate.request.pending) === 'object'){
|
|
3079
|
+
var pendingKey = translate.request.pending.key(lang, translate.to, cacheHash);
|
|
3080
|
+
var pendingItem = translate.request.pending.get(pendingKey);
|
|
3081
|
+
if(pendingItem != null){
|
|
3082
|
+
translate.request.pending.add(pendingKey, {uuid:uuid, lang:lang, hash:hash});
|
|
3083
|
+
continue;
|
|
3084
|
+
}
|
|
3085
|
+
//记录本次创建的pending,方便异常情况下清理
|
|
3086
|
+
translate.request.pending.add(pendingKey, {uuid:uuid, lang:lang, hash:hash});
|
|
3087
|
+
pendingKeysCreated.push(pendingKey);
|
|
3088
|
+
}
|
|
3089
|
+
|
|
3090
|
+
/*
|
|
3091
|
+
//取出数组
|
|
3092
|
+
var queueNodes = this.nodeQueue[lang][hash];
|
|
3093
|
+
if(queueNodes.length > 0){
|
|
3066
3094
|
//因为在这个数组中的值都是一样的,那么只需要取出第一个就行了
|
|
3067
3095
|
var valueStr = queueNodes[0].nodeValue;
|
|
3068
3096
|
valueStr = this.util.charReplace(valueStr);
|
|
@@ -3090,13 +3118,20 @@ var translate = {
|
|
|
3090
3118
|
//console.log(translateTextArray);
|
|
3091
3119
|
//return;
|
|
3092
3120
|
|
|
3093
|
-
if(typeof(translate.request.api.translate) != 'string' || translate.request.api.translate == null || translate.request.api.translate.length < 1){
|
|
3094
|
-
//用户已经设置了不掉翻译接口进行翻译
|
|
3095
|
-
translate.state = 0;
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
translate.
|
|
3099
|
-
|
|
3121
|
+
if(typeof(translate.request.api.translate) != 'string' || translate.request.api.translate == null || translate.request.api.translate.length < 1){
|
|
3122
|
+
//用户已经设置了不掉翻译接口进行翻译
|
|
3123
|
+
translate.state = 0;
|
|
3124
|
+
|
|
3125
|
+
//清理本次创建的pending记录
|
|
3126
|
+
if(pendingKeysCreated.length > 0 && typeof(translate.request) === 'object' && typeof(translate.request.pending) === 'object'){
|
|
3127
|
+
for(var pk = 0; pk < pendingKeysCreated.length; pk++){
|
|
3128
|
+
translate.request.pending.take(pendingKeysCreated[pk]);
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
|
|
3132
|
+
//生命周期触发事件
|
|
3133
|
+
translate.lifecycle.execute.renderFinish_Trigger(uuid, translate.to);
|
|
3134
|
+
translate.executeNumber++;
|
|
3100
3135
|
|
|
3101
3136
|
//钩子
|
|
3102
3137
|
translate.lifecycle.execute.finally_Trigger({
|
|
@@ -3476,14 +3511,19 @@ var translate = {
|
|
|
3476
3511
|
}
|
|
3477
3512
|
|
|
3478
3513
|
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3514
|
+
//翻译前的hash对应下标
|
|
3515
|
+
var hash = splitHashes[i];
|
|
3516
|
+
var cacheHash = translate.nodeQueue[uuid]['list'][lang][hash]['cacheHash'];
|
|
3517
|
+
var pendingItem = null;
|
|
3518
|
+
if(typeof(translate.request) === 'object' && typeof(translate.request.pending) === 'object'){
|
|
3519
|
+
var pendingKey = translate.request.pending.key(lang, responseData.to, cacheHash);
|
|
3520
|
+
pendingItem = translate.request.pending.take(pendingKey);
|
|
3521
|
+
}
|
|
3482
3522
|
|
|
3483
3523
|
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3524
|
+
|
|
3525
|
+
//取原始的词,还未经过翻译的,需要进行翻译的词
|
|
3526
|
+
var originalWord = '';
|
|
3487
3527
|
try{
|
|
3488
3528
|
originalWord = translate.nodeQueue[uuid]['list'][lang][hash]['original'];
|
|
3489
3529
|
//console.log('bef:'+translate.nodeQueue[uuid]['list'][lang][hash]['beforeText']);
|
|
@@ -3494,11 +3534,37 @@ var translate = {
|
|
|
3494
3534
|
}
|
|
3495
3535
|
|
|
3496
3536
|
//for(var index = 0; index < translate.nodeQueue[lang][hash].length; index++){
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3537
|
+
for(var node_index = 0; node_index < translate.nodeQueue[uuid]['list'][lang][hash]['nodes'].length; node_index++){
|
|
3538
|
+
//translate.nodeQueue[lang][hash]['nodes'][node_index].nodeValue = translate.nodeQueue[lang][hash]['nodes'][node_index].nodeValue.replace(new RegExp(originalWord,'g'), text);
|
|
3539
|
+
//加入任务
|
|
3540
|
+
task.add(translate.nodeQueue[uuid]['list'][lang][hash]['nodes'][node_index]['node'], originalWord, translate.nodeQueue[uuid]['list'][lang][hash]['nodes'][node_index]['beforeText']+text+translate.nodeQueue[uuid]['list'][lang][hash]['nodes'][node_index]['afterText'], translate.nodeQueue[uuid]['list'][lang][hash]['nodes'][node_index]['attribute']);
|
|
3541
|
+
}
|
|
3542
|
+
//处理等待同一文本的其他节点
|
|
3543
|
+
if(pendingItem != null && typeof(pendingItem.waits) === 'object'){
|
|
3544
|
+
for(var wi = 0; wi < pendingItem.waits.length; wi++){
|
|
3545
|
+
var waitItem = pendingItem.waits[wi];
|
|
3546
|
+
if(waitItem.uuid === uuid && waitItem.lang === lang && waitItem.hash === hash){
|
|
3547
|
+
continue;
|
|
3548
|
+
}
|
|
3549
|
+
if(typeof(translate.nodeQueue[waitItem.uuid]) === 'undefined'){
|
|
3550
|
+
continue;
|
|
3551
|
+
}
|
|
3552
|
+
if(typeof(translate.nodeQueue[waitItem.uuid]['list'][waitItem.lang]) === 'undefined'){
|
|
3553
|
+
continue;
|
|
3554
|
+
}
|
|
3555
|
+
if(typeof(translate.nodeQueue[waitItem.uuid]['list'][waitItem.lang][waitItem.hash]) === 'undefined'){
|
|
3556
|
+
continue;
|
|
3557
|
+
}
|
|
3558
|
+
var waitQueue = translate.nodeQueue[waitItem.uuid]['list'][waitItem.lang][waitItem.hash];
|
|
3559
|
+
if(typeof(waitQueue) === 'undefined' || typeof(waitQueue.nodes) === 'undefined'){
|
|
3560
|
+
continue;
|
|
3561
|
+
}
|
|
3562
|
+
var waitOriginal = waitQueue.original;
|
|
3563
|
+
for(var wni = 0; wni < waitQueue.nodes.length; wni++){
|
|
3564
|
+
task.add(waitQueue.nodes[wni]['node'], waitOriginal, waitQueue.nodes[wni]['beforeText']+text+waitQueue.nodes[wni]['afterText'], waitQueue.nodes[wni]['attribute']);
|
|
3565
|
+
}
|
|
3501
3566
|
}
|
|
3567
|
+
}
|
|
3502
3568
|
//}
|
|
3503
3569
|
/*
|
|
3504
3570
|
for(var index = 0; index < translate.nodeQueue[data.from][hash].length; index++){
|
|
@@ -3521,6 +3587,23 @@ var translate = {
|
|
|
3521
3587
|
if(translate.cancelVersion != executeCancelVersion){
|
|
3522
3588
|
return;
|
|
3523
3589
|
}
|
|
3590
|
+
//请求异常,清理pending,允许后续重试
|
|
3591
|
+
if(typeof(translate.request) === 'object' && typeof(translate.request.pending) === 'object'){
|
|
3592
|
+
for(var ph = 0; ph < splitHashes.length; ph++){
|
|
3593
|
+
if(typeof(translate.nodeQueue[uuid]) === 'undefined'){
|
|
3594
|
+
break;
|
|
3595
|
+
}
|
|
3596
|
+
if(typeof(translate.nodeQueue[uuid]['list'][lang]) === 'undefined'){
|
|
3597
|
+
break;
|
|
3598
|
+
}
|
|
3599
|
+
if(typeof(translate.nodeQueue[uuid]['list'][lang][splitHashes[ph]]) === 'undefined'){
|
|
3600
|
+
continue;
|
|
3601
|
+
}
|
|
3602
|
+
var cacheHash = translate.nodeQueue[uuid]['list'][lang][splitHashes[ph]]['cacheHash'];
|
|
3603
|
+
var pendingKey = translate.request.pending.key(lang, translate.to, cacheHash);
|
|
3604
|
+
translate.request.pending.take(pendingKey);
|
|
3605
|
+
}
|
|
3606
|
+
}
|
|
3524
3607
|
var info = '';
|
|
3525
3608
|
if(typeof(xhr.status) != 'undefined'){
|
|
3526
3609
|
if(xhr.status < 1){
|
|
@@ -8281,6 +8364,49 @@ var translate = {
|
|
|
8281
8364
|
//单条文本达到该长度后单独成组,避免与其它文本拼接,0则使用 size/2
|
|
8282
8365
|
singleTextMinSize:0
|
|
8283
8366
|
},
|
|
8367
|
+
/*
|
|
8368
|
+
翻译请求去重(处理中)
|
|
8369
|
+
*/
|
|
8370
|
+
pending:{
|
|
8371
|
+
map:new Map(),
|
|
8372
|
+
key:function(from, to, cacheHash){
|
|
8373
|
+
return from+'|'+to+'|'+cacheHash;
|
|
8374
|
+
},
|
|
8375
|
+
get:function(key){
|
|
8376
|
+
if(this.map == null){
|
|
8377
|
+
this.map = new Map();
|
|
8378
|
+
}
|
|
8379
|
+
return this.map.get(key) || null;
|
|
8380
|
+
},
|
|
8381
|
+
add:function(key, waitItem){
|
|
8382
|
+
if(this.map == null){
|
|
8383
|
+
this.map = new Map();
|
|
8384
|
+
}
|
|
8385
|
+
var item = this.map.get(key);
|
|
8386
|
+
if(item == null){
|
|
8387
|
+
item = {waits:[]};
|
|
8388
|
+
this.map.set(key, item);
|
|
8389
|
+
}
|
|
8390
|
+
if(waitItem != null){
|
|
8391
|
+
item.waits.push(waitItem);
|
|
8392
|
+
}
|
|
8393
|
+
return item;
|
|
8394
|
+
},
|
|
8395
|
+
take:function(key){
|
|
8396
|
+
if(this.map == null){
|
|
8397
|
+
this.map = new Map();
|
|
8398
|
+
return null;
|
|
8399
|
+
}
|
|
8400
|
+
var item = this.map.get(key) || null;
|
|
8401
|
+
if(item != null){
|
|
8402
|
+
this.map.delete(key);
|
|
8403
|
+
}
|
|
8404
|
+
return item;
|
|
8405
|
+
},
|
|
8406
|
+
clear:function(){
|
|
8407
|
+
this.map = new Map();
|
|
8408
|
+
}
|
|
8409
|
+
},
|
|
8284
8410
|
/*
|
|
8285
8411
|
将翻译文本数组拆分为多个请求,避免一次请求过大导致超时
|
|
8286
8412
|
注意:不会拆分单条文本,避免破坏段落/语义
|