i18n-jsautotranslate 3.18.63 → 3.18.66

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.
@@ -6,7 +6,7 @@
6
6
  value-key="id"
7
7
  @change="LanguageSelectOnChange"
8
8
  popup-class="ignore"
9
- placeholder="请选择语言"
9
+ placeholder="please select language"
10
10
  >
11
11
  <!-- 语言选项列表(使用Select的子组件Option) -->
12
12
  <a-select-option
@@ -52,6 +52,15 @@ const LanguageSelectOnChange = (value: string) => {
52
52
  };
53
53
 
54
54
  onMounted(() => {
55
+ if(typeof(translate) == 'object' && typeof(translate.vue3) == 'object' && typeof(translate.vue3.isUse) == 'boolean' && translate.vue3.isUse == true){
56
+ //正常,需要的,需要加载多语言切换Select
57
+ }else{
58
+ //不需要显示
59
+ return;
60
+ }
61
+
62
+ translate.time.log(translate.vue3.isUse);
63
+
55
64
  //重写渲染语言下拉列表出现时的函数,这里是为了把默认创建的 <div id="translate" 这个给去掉,其他无改变
56
65
  translate.selectLanguageTag.render = function(){ //v2增加
57
66
  if(translate.selectLanguageTag.alreadyRender){
@@ -82,7 +91,9 @@ onMounted(() => {
82
91
 
83
92
  //显示切换语言
84
93
  var TranslateJsSelectLanguages = document.getElementsByClassName('LanguageSelect');
85
- TranslateJsSelectLanguages[0].style.display = 'block';
94
+ for(var li = 0; li<TranslateJsSelectLanguages.length; li++){
95
+ TranslateJsSelectLanguages[li].style.display = 'block';
96
+ }
86
97
  }
87
98
 
88
99
  //languageList 便是当前支持的能切换的语种,你可以 console.log(languageList); 打印出来看看
@@ -114,34 +125,12 @@ onMounted(() => {
114
125
 
115
126
  //显示上一次切换后的语种
116
127
  language.value = translate.language.getCurrent();
117
- //setTimeout(function(){
118
- //language.value = translate.language.getCurrent();
119
- //}, 100);
120
- }
121
-
122
- const refreshLanguage = function(){
123
- //渲染语言下拉列表出现
124
- window.translate.selectLanguageTag.refreshRender();
125
- //显示上一次切换后的语种
126
- language.value = translate.language.getCurrent();
127
- };
128
-
129
-
130
- // 当用户打开页面后,第一次过了初始化正式进行执行 translate.execute() 时,进行触发
131
- translate.lifecycle.execute.start.push(function(data){
132
- if(translate.selectLanguageTag.show === true && translate.selectLanguageTag.alreadyRender === false){
133
- //console.log('这是打开页面后,第一次触发 translate.execute() ,因为translate.executeNumber 记录的是translate.execute() 执行完的次数。');
134
- // 触发语言下拉列表出现
135
- //渲染语言下拉列表出现
136
- refreshLanguage();
137
- }
138
- });
139
- //如果已经触发了 translate.execute() 那么直接就渲染
140
- //console.log(translate.executeNumber+ ', '+translate.state)
141
- if(translate.executeNumber > 0 || translate.state > 0){
142
- refreshLanguage();
143
128
  }
144
129
 
130
+ //渲染语言下拉列表出现
131
+ translate.selectLanguageTag.refreshRender();
132
+ //显示上一次切换后的语种
133
+ //language.value = translate.language.getCurrent();
145
134
  });
146
135
 
147
136
 
package/index.js CHANGED
@@ -14,7 +14,7 @@ var translate = {
14
14
  * 格式:major.minor.patch.date
15
15
  */
16
16
  // AUTO_VERSION_START
17
- version: '3.18.63.20250928',
17
+ version: '3.18.66.20250930',
18
18
  // AUTO_VERSION_END
19
19
  /*
20
20
  当前使用的版本,默认使用v2. 可使用 setUseVersion2();
@@ -518,6 +518,12 @@ var translate = {
518
518
  if(ele == null || typeof(ele) == 'undefined'){
519
519
  return false;
520
520
  }
521
+ if(ele.nodeType === 2){ //是属性,将其转为元素判断,因为当前忽略配置,是针对元素配置的
522
+ ele = ele.ownerElement;
523
+ }else if(ele.nodeType === 3){
524
+ //文本节点,转为元素
525
+ ele = ele.parentNode;
526
+ }
521
527
 
522
528
  var parentNode = ele;
523
529
  var maxnumber = 100; //最大循环次数,避免死循环
@@ -765,8 +771,7 @@ var translate = {
765
771
  * 如果传入 null,则不进行任何替换操作
766
772
  * 如果传入具体的值,则是:
767
773
  * {
768
- * node: node节点
769
- * attribute: 要替换的node的attribute名称。如果传入 null,则是直接对 nodeValue 生效
774
+ * node: node节点 ,要改动的文字所在的node节点。 如果改动的文字比如是 div 的title中,那么这里传入的node应该是 title 的node,而不是 div 的node
770
775
  * }
771
776
  *
772
777
  * @returns {
@@ -899,8 +904,7 @@ var translate = {
899
904
  如果传入 null,则不进行任何替换操作
900
905
  如果传入具体的值,则是:
901
906
  {
902
- node: node节点
903
- attribute: 要替换的node的attribute名称。如果传入 null,则是直接对 nodeValue 生效
907
+ node: node节点 ,要改动的文字所在的node节点。 如果改动的文字比如是 div 的title中,那么这里传入的node应该是 title 的node,而不是 div 的node
904
908
  }
905
909
 
906
910
 
@@ -1309,6 +1313,54 @@ var translate = {
1309
1313
  //console.log('refresh ignore finish: '+Object.keys(translate.listener.ignoreNode).length);
1310
1314
  },
1311
1315
 
1316
+ /*
1317
+ 用于监听发生改变的这个 node 是否有正常需要翻译的内容、以及是否是非translate.js触发的需要被翻译。
1318
+ 注意,传入进行判断的node中的文本必须是 node.nodeValue ,也就是这个必须是 node.nodeType == 2(某个元素的属性,比如 input 的 placeholder) 或 3(文本节点), 这样他们才会有正常的 node.nodeValue,而且文本也存在于 node.nodeValue 中
1319
+ 比如 div title="你好" ,要对 title 的 你好 这个值进行判定,传入的node必须是 title 的 node,而非 div 的 node
1320
+ 它主要是为了给 translate.listener.addListener 中的动态监听node改变所服务的
1321
+
1322
+ @param node 要判断的这个是否需要触发翻译的node
1323
+ @return boolean true:需要触发 translate.execute(node) 进行翻译
1324
+ */
1325
+ nodeValueChangeNeedTranslate: function(node){
1326
+ //是否是要加入翻译扫描触发执行,是则是true
1327
+ var addTranslateExecute = true;
1328
+
1329
+ /*
1330
+ 不会进入翻译的情况 -
1331
+ 1. 认为是有 translate.js 本身翻译导致的改变,不进行翻译
1332
+ 取 translate.node.data 中的数据,当改变的node节点在其中找到了对应的数据后,进行判定
1333
+ 1. 是整体翻译,且当前node改变后的内容,跟上次翻译后的结果一样,那说明当前node改变事件
1334
+ 2. 不是整体翻译,可能是触发自定义术语、或直接没启用整体翻译能力,那就要根据最后翻译时间这个来判定了。如果这个node元素,已经被翻译过了,最后一次翻译渲染时间,距离当前时间不超过500毫秒
1335
+ 2. 其他的情况如果后续发现有遗漏,再加入,当前没有这种考虑
1336
+ */
1337
+ if(translate.node.get(node) != null){
1338
+ if(typeof(translate.node.get(node).whole) !== 'undefined' && translate.node.get(node).whole == true){
1339
+ //整体翻译
1340
+ if(typeof(translate.node.get(node).resultText) !== 'undefined' && translate.node.get(node).resultText === node.nodeValue){
1341
+ //当前改变后的内容,跟上次翻译后的结果一样,那说明当前node改变事件,是有translate.js 本身翻译导致的,不进行翻译
1342
+ addTranslateExecute = false;
1343
+ }
1344
+ }else{
1345
+ //不是整体翻译,可能是触发自定义术语、或直接没启用整体翻译能力
1346
+ //这就要根据最后翻译时间这个来判定了
1347
+ if(typeof(translate.node.get(node).lastTranslateRenderTime) == 'number' && translate.node.get(node).lastTranslateRenderTime + 500 > Date.now()){
1348
+ //如果这个node元素,已经被翻译过了,最后一次翻译渲染时间,距离当前时间不超过500毫秒,那认为这个元素动态改变,是有translate.js 本身引起的,将不做任何动作
1349
+ addTranslateExecute = false;
1350
+ }
1351
+ }
1352
+ }
1353
+
1354
+ //如果新的里面没有非空白字符的值,那也不再触发翻译
1355
+ if(addTranslateExecute === true){
1356
+ if(node.nodeValue.trim().length === 0){
1357
+ addTranslateExecute = false;
1358
+ }
1359
+ }
1360
+
1361
+ return addTranslateExecute;
1362
+ },
1363
+
1312
1364
 
1313
1365
  //增加监听,开始监听。这个不要直接调用,需要使用上面的 start() 开启
1314
1366
  addListener:function(){
@@ -1345,61 +1397,47 @@ var translate = {
1345
1397
  }
1346
1398
  }
1347
1399
  }else if (mutation.type === 'attributes') {
1348
- //console.log(mutation);
1349
- if(mutation.attributeName === 'class'){
1350
- //如果是class,不做任何改变,直接跳出
1400
+ if(mutation.attributeName === 'class' || mutation.attributeName === 'style'){
1401
+ //如果是class/ style 这种常见的,不做任何改变,直接跳出
1351
1402
  continue;
1352
1403
  }
1353
- //console.log('listener attributes change --> ' + mutation.target.nodeName+'['+ mutation.attributeName + '] oldValue :'+mutation.oldValue);
1354
1404
 
1355
- if(translate.node.get(mutation.target) != null){
1356
-
1357
- if(typeof(translate.node.get(mutation.target).lastTranslateRenderTime) == 'number' && translate.node.get(mutation.target).lastTranslateRenderTime + 1000 > Date.now()){
1358
- //如果这个node元素,已经被翻译过了,最后一次翻译渲染时间,距离当前时间不超过1秒,那认为这个元素动态改变,是有translate.js 本身引起的,将不做任何动作
1405
+ /*
1406
+ 这里要判断一些允许翻译的属性
1407
+ input placeholder 属性 ,直接判断 placeholder 就行了,也就 input、textarea 有这个属性
1408
+ img 的 alt 属性
1409
+ 所有标签的 title 属性
1410
+ */
1411
+
1412
+ if(mutation.attributeName === 'placeholder' || mutation.attributeName === 'alt' || mutation.attributeName === 'title'){
1413
+ //允许翻译
1414
+ }else{
1415
+ //判断是否是 translate.element.tagAttribute 自定义翻译属性的
1416
+ var divTagAttribute = translate.element.tagAttribute[mutation.target.nodeName.toLowerCase()];
1417
+ if(typeof(divTagAttribute) !== 'undefined' && divTagAttribute.attribute.indexOf(mutation.attributeName) > -1 && divTagAttribute.condition(mutation.target)){
1418
+ //是自定义翻译这个属性的,以及判定是否达到翻译条件
1419
+ //条件满足,允许翻译
1359
1420
  }else{
1360
- //不是 translate.js 触发的改动
1361
- var nodeAttribute = translate.node.getAttribute(mutation.attributeName);
1362
- //console.log(translate.node.get(mutation.target)[nodeAttribute.key])
1363
- if(typeof(translate.node.get(mutation.target)[nodeAttribute.key]) != 'undefined'){
1364
- //从翻译历史中删掉,使这个属性还能继续被翻译
1365
- //console.log('delete translate.node.get(mutation.target)[nodeAttribute.key] -> '+mutation.attributeName);
1366
- delete translate.node.get(mutation.target)[nodeAttribute.key];
1367
- }
1421
+ //条件不满足,不在翻译的属性范围
1422
+ continue;
1368
1423
  }
1369
1424
  }
1370
1425
 
1371
- //最后,将这个属性加入待翻译
1372
- addNodes.push(mutation.target);
1426
+ //这里出现的 mutation.target 是定位到了元素上面,而不是变化的这个 attributes 属性上,需要用 mutation.attributeName 获取到这个属性的node
1427
+ var node = mutation.target.getAttributeNode(mutation.attributeName);
1428
+
1429
+ //是否是要加入翻译扫描触发执行,是则是true
1430
+ var addTranslateExecute = translate.listener.nodeValueChangeNeedTranslate(node);
1431
+ if(addTranslateExecute){ //不是 translate.js 翻译引起的改变,那么
1432
+ //console.log('listener attributes change ' + mutation.target.nodeName+'['+ mutation.attributeName + '] '+mutation.oldValue+' --> '+node.nodeValue);
1433
+ translate.node.delete(node);
1434
+ addNodes = [node]; //将这个属性转为的node加入待翻译
1435
+ }
1373
1436
  }else if(mutation.type === 'characterData'){
1374
1437
  //内容改变
1375
1438
 
1376
1439
  //是否是要加入翻译扫描触发执行,是则是true
1377
- var addTranslateExecute = true;
1378
-
1379
- /*
1380
- 不会进入翻译的情况 -
1381
- 1. 认为是有 translate.js 本身翻译导致的改变,不进行翻译
1382
- 取 translate.node.data 中的数据,当改变的node节点在其中找到了对应的数据后,进行判定
1383
- 1. 是整体翻译,且当前node改变后的内容,跟上次翻译后的结果一样,那说明当前node改变事件
1384
- 2. 不是整体翻译,可能是触发自定义术语、或直接没启用整体翻译能力,那就要根据最后翻译时间这个来判定了。如果这个node元素,已经被翻译过了,最后一次翻译渲染时间,距离当前时间不超过500毫秒
1385
- 2. 其他的情况如果后续发现有遗漏,再加入,当前没有这种考虑
1386
- */
1387
- if(translate.node.get(mutation.target) != null && typeof(translate.node.get(mutation.target).translate_default_value) != 'undefined'){
1388
- if(typeof(translate.node.get(mutation.target).translate_default_value.whole) != 'undefined' && translate.node.get(mutation.target).translate_default_value.whole == true){
1389
- //整体翻译
1390
- if(typeof(translate.node.get(mutation.target).translate_default_value.resultText) != 'undefined' && translate.node.get(mutation.target).translate_default_value.resultText === mutation.target.nodeValue){
1391
- //当前改变后的内容,跟上次翻译后的结果一样,那说明当前node改变事件,是有translate.js 本身翻译导致的,不进行翻译
1392
- addTranslateExecute = false;
1393
- }
1394
- }else{
1395
- //不是整体翻译,可能是触发自定义术语、或直接没启用整体翻译能力
1396
- //这就要根据最后翻译时间这个来判定了
1397
- if(typeof(translate.node.get(mutation.target).lastTranslateRenderTime) == 'number' && translate.node.get(mutation.target).lastTranslateRenderTime + 500 > Date.now()){
1398
- //如果这个node元素,已经被翻译过了,最后一次翻译渲染时间,距离当前时间不超过500毫秒,那认为这个元素动态改变,是有translate.js 本身引起的,将不做任何动作
1399
- addTranslateExecute = false;
1400
- }
1401
- }
1402
- }
1440
+ var addTranslateExecute = translate.listener.nodeValueChangeNeedTranslate(mutation.target);
1403
1441
 
1404
1442
  if(addTranslateExecute){ //不是 translate.js 翻译引起的改变,那么
1405
1443
  translate.node.delete(mutation.target);
@@ -1428,10 +1466,8 @@ var translate = {
1428
1466
  }
1429
1467
  }
1430
1468
 
1431
- //console.log(documents.length);
1432
1469
  if(documents.length > 0){
1433
1470
  //有变动,需要看看是否需要翻译,延迟10毫秒执行
1434
-
1435
1471
  translate.time.log('监听到元素发生变化,'+documents.length+'个元素');
1436
1472
 
1437
1473
  //判断是否属于在正在翻译的节点,重新组合出新的要翻译的node集合
@@ -1473,7 +1509,7 @@ var translate = {
1473
1509
  //console.log('translateNodeslength: '+translateNodes.length);
1474
1510
 
1475
1511
  translate.time.log('将监听到的发生变化的元素进行整理,得到'+translateNodes.length+'个元素,对其进行翻译');
1476
- //console.log(translateNodes[0]);
1512
+ //console.log(translateNodes);
1477
1513
 
1478
1514
  translate.execute(translateNodes);
1479
1515
  //setTimeout(function() {
@@ -1694,12 +1730,25 @@ var translate = {
1694
1730
 
1695
1731
 
1696
1732
  // translate.node 记录
1697
- var nodeAttribute = translate.node.getAttribute(task['attribute']);
1698
- if(translate.node.data.get(this.nodes[hash][task_index]) != null){
1733
+
1734
+ var translateNode; //当前操作的,要记录入 translate.node 中的,进行翻译的node
1735
+ var translateNode_attribute = ''; //当前操作的是node中的哪个attribute,如果没有是node本身则是空字符串
1736
+ if(typeof(task['attribute']) === 'string' && task['attribute'].length > 0){
1737
+ //当前渲染任务是针对的元素的某个属性,这是要取出这个元素的具体属性,作为一个目的 node 来进行加入 translate.node
1738
+ //是操作的元素的某个属性
1739
+ translateNode = this.nodes[hash][node_index].getAttributeNode(task['attribute']);
1740
+ translateNode_attribute = task['attribute'];
1741
+ }else{
1742
+ //操作的就是node本身
1743
+ translateNode = this.nodes[hash][node_index];
1744
+ }
1745
+ //console.log(translateNode)
1746
+ //var nodeAttribute = translate.node.getAttribute(task['attribute']);
1747
+ if(translate.node.data.get(translateNode) != null){
1699
1748
  // 记录当前有 translate.js 所触发翻译之后渲染到dom界面显示的时间,13位时间戳
1700
- translate.node.get(this.nodes[hash][task_index]).lastTranslateRenderTime = Date.now();
1749
+ translate.node.get(translateNode).lastTranslateRenderTime = Date.now();
1701
1750
  }else{
1702
- translate.log('执行异常,渲染时,node未在 nodeHistory 中找到, 这个理论上是不应该存在的,当前异常已被容错。 node:'+this.nodes[hash][task_index]);
1751
+ translate.log('执行异常,渲染时,node 未在 translate.node 中找到, 这个理论上是不应该存在的,当前异常已被容错。 node:'+translateNode);
1703
1752
  translate.log(this.nodes[hash][task_index]);
1704
1753
  }
1705
1754
 
@@ -1708,22 +1757,14 @@ var translate = {
1708
1757
  var analyseSet = translate.element.nodeAnalyse.set(this.nodes[hash][task_index], task.originalText, task.resultText, task['attribute']);
1709
1758
  //console.log(analyseSet);
1710
1759
 
1711
- if(translate.node.data.get(this.nodes[hash][task_index]) != null){
1712
- if(typeof(translate.node.get(this.nodes[hash][task_index])[nodeAttribute.key]) == 'undefined'){
1713
- //这里不应该的
1714
- translate.log('执行异常,渲染时,node 的 '+(nodeAttribute.attribute.length == 0? 'nodeValue':'attribute : '+nodeAttribute.attribute)+' 未在 nodeHistory 中找到, 这个理论上是不应该存在的,当前异常已被容错。 node:'+this.nodes[hash][task_index]);
1715
- }else{
1716
- //将具体通过文本翻译接口进行翻译的文本记录到 translate.node.data
1717
- translate.node.get(this.nodes[hash][task_index])[nodeAttribute.key].translateTexts[task.originalText] = task.resultText;
1718
- //将翻译完成后要显示出的文本进行记录
1719
- translate.node.get(this.nodes[hash][task_index])[nodeAttribute.key].resultText = analyseSet.resultText;
1760
+ if(translate.node.data.get(translateNode) != null){
1761
+ //将具体通过文本翻译接口进行翻译的文本记录到 translate.node.data
1762
+ translate.node.get(translateNode).translateTexts[task.originalText] = task.resultText;
1763
+ //将翻译完成后要显示出的文本进行记录
1764
+ translate.node.get(translateNode).resultText = analyseSet.resultText;
1720
1765
 
1721
- //将其加入 translate.history.translateTexts
1722
- translate.history.translateText.add(translate.node.get(this.nodes[hash][task_index])[nodeAttribute.key].originalText ,analyseSet.resultText);
1723
- }
1724
- }else{
1725
- translate.log('执行异常,渲染时,node未在 nodeHistory 中找到, 这个理论上是不应该存在的,当前异常已被容错。 node:'+this.nodes[hash][task_index]);
1726
- translate.log(this.nodes[hash][task_index]);
1766
+ //将其加入 translate.history.translateTexts
1767
+ translate.history.translateText.add(translate.node.get(translateNode).originalText ,analyseSet.resultText);
1727
1768
  }
1728
1769
 
1729
1770
  //加入 translate.listener.ignoreNode
@@ -1856,12 +1897,14 @@ var translate = {
1856
1897
  translate.listener.execute.renderFinishByApiRun(uuid, from, to);
1857
1898
 
1858
1899
  //通过 uuid、from 取得本次翻译相关的 texts、nodes , 触发 translateNetworkAfter_Trigger 钩子
1900
+ //获取请求日志
1901
+ var requestData = translate.request.data[uuid].list[from][to];
1859
1902
  translate.lifecycle.execute.translateNetworkAfter_Trigger({
1860
1903
  uuid: uuid,
1861
1904
  from: from,
1862
1905
  to: to,
1863
- texts: translate.request.data[uuid].list[from].texts,
1864
- nodes: translate.request.data[uuid].list[from].nodes,
1906
+ texts: requestData.texts,
1907
+ nodes: requestData.nodes,
1865
1908
  result: result,
1866
1909
  info: info
1867
1910
  });
@@ -2229,8 +2272,8 @@ var translate = {
2229
2272
  if(sliceDoc[di].getAttribute('class') != null && typeof(sliceDoc[di].getAttribute('class')) == 'string' && sliceDoc[di].getAttribute('class').length > 0){
2230
2273
  sliceDocString = sliceDocString + " class="+sliceDoc[di].getAttribute('class');
2231
2274
  }
2232
- }else if(sliceDoc[di].nodeType === 3){
2233
- //node
2275
+ }else if(sliceDoc[di].nodeType === 2 || sliceDoc[di].nodeType === 3){
2276
+ //2属性 或 3文本节点
2234
2277
  sliceDocString = sliceDocString + sliceDoc[di].nodeValue.replaceAll(/\r?\n/g, '[换行符]');
2235
2278
  }
2236
2279
  }
@@ -2239,7 +2282,6 @@ var translate = {
2239
2282
 
2240
2283
 
2241
2284
  translate.log('当前翻译未完结,新翻译任务已加入等待翻译队列,待上个翻译任务结束后便会执行当前翻译任务'+sliceDocString);
2242
- //console.log(docs);
2243
2285
  translate.waitingExecute.add(docs);
2244
2286
 
2245
2287
  //钩子
@@ -2928,6 +2970,11 @@ var translate = {
2928
2970
  将翻译请求的信息记录到 translate.js 本身中
2929
2971
  uuid 每次 translate.execute() 触发生成的uuid
2930
2972
  time: 触发后加入到 data 中的时间,13位时间戳
2973
+ list: 记录当前uuid下发起的网络请求
2974
+ from: 从什么语种进行的翻译,如: chinese_simplified
2975
+ to: 翻译为什么语种,如 : english
2976
+ nodes: 当前网络请求有哪些node节点,值为 [node1, node2, ...]
2977
+ texts: 当前网络请求有哪些文本进行翻译,值为 [text1, text2, ...]
2931
2978
 
2932
2979
  */
2933
2980
  translate.request.data[uuid] = {
@@ -2998,8 +3045,10 @@ var translate = {
2998
3045
  });
2999
3046
 
3000
3047
  //记入请求日志
3001
- translate.request.data[uuid].list[lang] = {
3002
- to: translate.to,
3048
+ if(typeof(translate.request.data[uuid].list[lang]) === 'undefined'){
3049
+ translate.request.data[uuid].list[lang] = {};
3050
+ }
3051
+ translate.request.data[uuid].list[lang][translate.to] = {
3003
3052
  texts: translateTextArray[lang],
3004
3053
  nodes: translateTextNodes,
3005
3054
  };
@@ -3193,7 +3242,7 @@ var translate = {
3193
3242
  将已扫描的节点进行记录,这里是只要进行扫描到了,也就是在加入 translate.nodeQueue 时就也要加入到这里。
3194
3243
  这是一个map,为了兼容es5,这里设置为null,在 translate.execute 中在进行初始化
3195
3244
 
3196
- key: node
3245
+ key: node ,进行翻译的文本的node, 如果是 div 的 title属性进行的翻译,那这个node是定位在 title 上的node,而不是 div 这个依附的元素
3197
3246
  value: 这是一个对像
3198
3247
  其中,key的取值有这几种:
3199
3248
  translate_default_value: 如果当前翻译的是元素本身的值或node节点本身的值(nodeValue),那么这里的key就是固定的 translate_default_value
@@ -3246,7 +3295,7 @@ var translate = {
3246
3295
  key: translate.node 中 translate.node.get(node)[attribute] 所使用的 attribute 的字符串,如 attribute_title 、translate_default_value
3247
3296
  attribute: 这里是attribute具体的内容,比如 key 是 attribute_title 那么这里就是 title , key 是 translate_default_value 这里就是 '' 空字符串
3248
3297
  }
3249
- */
3298
+
3250
3299
  getAttribute:function(attribute){
3251
3300
  var history_attribute;
3252
3301
  if(typeof(attribute) != 'undefined' && attribute.length > 0){
@@ -3262,6 +3311,7 @@ var translate = {
3262
3311
  attribute:attribute
3263
3312
  }
3264
3313
  },
3314
+ */
3265
3315
  /*
3266
3316
  刷新 translate.node.data 中的数据,剔除过时的(node已经不存在于dom的)
3267
3317
  */
@@ -3360,6 +3410,147 @@ var translate = {
3360
3410
  get:function(node, attribute){
3361
3411
  return translate.element.nodeAnalyse.analyse(node,'','', attribute);
3362
3412
  },
3413
+ /*
3414
+ 同上,只不过这个是扫描 element/node 下的所有可翻译的子节点(下层节点),返回数组形态。
3415
+ 这里面的数组,已经经过判断, text 必然是有不为空的值的。
3416
+ 所以它的返回值,有可能是一个空的数组
3417
+
3418
+ [
3419
+ {
3420
+ node: 当前扫描出的node (传入的node、或下层node)
3421
+ attribute: 是否是下层属性,比如 alt、placeholder , 如果是传入的node本身,不是任何下层属性,则这里是空白字符串 ''
3422
+ text: 可进行翻译的文本,也就是当前数组中 node 的值的文本
3423
+ },
3424
+ ...
3425
+ ]
3426
+ */
3427
+ gets:function(node){
3428
+ var resultArray = [];
3429
+
3430
+ var nodename = translate.element.getNodeName(node).toUpperCase();
3431
+ switch (nodename) {
3432
+ case 'META': //meta标签,如是关键词、描述等
3433
+ var nodeAttributeName = node.name.toLowerCase(); //取meta 标签的name 属性
3434
+ var nodeAttributePropertyOri = node.getAttribute('property'); //取 property的值
3435
+ var nodeAttributeProperty = '';
3436
+ if(typeof(nodeAttributePropertyOri) === 'string' && nodeAttributePropertyOri.length > 0){
3437
+ nodeAttributeProperty = nodeAttributePropertyOri.toLowerCase();
3438
+ }
3439
+
3440
+ if(nodeAttributeName == 'keywords' || nodeAttributeName == 'description' || nodeAttributeName == 'sharetitle' || nodeAttributeProperty == 'og:title' || nodeAttributeProperty == 'og:description' || nodeAttributeProperty == 'og:site_name' || nodeAttributeProperty == 'og:novel:latest_chapter_name'){
3441
+ if(typeof(node.content) === 'string' && node.content.trim().length > 0){
3442
+ resultArray.push({
3443
+ text: node.content,
3444
+ attribute: 'content',
3445
+ node: node.getAttributeNode('content')
3446
+ });
3447
+ }
3448
+ }
3449
+ break;
3450
+ case 'IMG':
3451
+ if(typeof(node.alt) === 'string' && node.alt.trim().length > 0){
3452
+ resultArray.push({
3453
+ text: node.alt,
3454
+ attribute: 'alt',
3455
+ node: node.getAttributeNode('alt')
3456
+ });
3457
+ }
3458
+ break;
3459
+ case 'INPUT':
3460
+ /*
3461
+ input,要对以下情况进行翻译
3462
+ placeholder
3463
+ type=button、submit 的情况下的 value
3464
+ */
3465
+
3466
+ //针对 type=button、submit 的情况下的 value
3467
+ if(typeof(node.attributes.type) !== 'undefined' && node.attributes.type !== null && typeof(node.attributes.type.nodeValue) === 'string' && (node.attributes.type.nodeValue.toLowerCase() == 'button' || node.attributes.type.nodeValue.toLowerCase() == 'submit')){
3468
+ //取它的value
3469
+ var input_value_node = node.attributes.value;
3470
+ if(typeof(input_value_node) !== 'undefined' && input_value_node !== null && typeof(input_value_node.nodeValue) === 'string' && input_value_node.nodeValue.trim().length > 0){
3471
+ resultArray.push({
3472
+ text: input_value_node.nodeValue,
3473
+ attribute: 'value',
3474
+ node: input_value_node
3475
+ });
3476
+ }
3477
+ }
3478
+
3479
+ //针对 placeholder
3480
+ if(typeof(node.attributes['placeholder']) !== 'undefined' && typeof(node.attributes['placeholder'].nodeValue) === 'string' && node.attributes['placeholder'].nodeValue.trim().length > 0){
3481
+ resultArray.push({
3482
+ text: node.attributes['placeholder'].nodeValue,
3483
+ attribute: 'placeholder',
3484
+ node: node.attributes['placeholder']
3485
+ });
3486
+ }
3487
+ break;
3488
+ case 'TEXTAREA':
3489
+ //针对 placeholder
3490
+ if(typeof(node.attributes['placeholder']) !== 'undefined' && typeof(node.attributes['placeholder'].nodeValue) === 'string' && node.attributes['placeholder'].nodeValue.trim().length > 0){
3491
+ resultArray.push({
3492
+ text: node.attributes['placeholder'].nodeValue,
3493
+ attribute: 'placeholder',
3494
+ node: node.attributes['placeholder']
3495
+ });
3496
+ }
3497
+ break;
3498
+ }
3499
+
3500
+ //判断是否是 translate.element.tagAttribute 自定义翻译属性的
3501
+ var divTagAttribute = translate.element.tagAttribute[nodename.toLowerCase()];
3502
+ if(typeof(divTagAttribute) !== 'undefined'){
3503
+ //有这个标签的自定义翻译某个属性
3504
+ for(var ai = 0; ai<node.attributes.length; ai++){
3505
+ var arrtibuteNodeName = translate.element.getNodeName(node.attributes[ai]).toLowerCase();
3506
+ if(divTagAttribute.attribute.indexOf(arrtibuteNodeName) > -1 && divTagAttribute.condition(node)){
3507
+ //包含这个属性,且自定义判断条件满足,允许翻译
3508
+ //判定一下是否已经加入过了,如果没有加入过,才会加入。这里主要是针对input 标签进行判断,比如 input type="submit" 的,value值如果也被用户自定义翻译,那上面的value就已经加上了,不需要在加了
3509
+ var isAlreadyAdd = false; //true已经加入过了
3510
+ for(var ri = 0; ri < resultArray.length; ri++){
3511
+ if(resultArray[ri].node === node.attributes[ai]){
3512
+ //相同,则不在加入了
3513
+ isAlreadyAdd = true;
3514
+ }
3515
+ }
3516
+ if(!isAlreadyAdd){
3517
+ resultArray.push({
3518
+ text: node.attributes[ai].nodeValue,
3519
+ attribute: arrtibuteNodeName,
3520
+ node: node.attributes[ai]
3521
+ });
3522
+ }
3523
+ }
3524
+ }
3525
+ }else{
3526
+ //条件不满足,不在翻译的属性范围
3527
+ }
3528
+
3529
+
3530
+ //所有元素都要判定的属性 - title 属性
3531
+ if(typeof(node['title']) === 'string' && node['title'].trim().length > 0){
3532
+ var titleNode = node.getAttributeNode('title');
3533
+ resultArray.push({
3534
+ text: titleNode.nodeValue,
3535
+ attribute: 'title',
3536
+ node: titleNode
3537
+ });
3538
+ }
3539
+
3540
+
3541
+ //最后判定 node 本身
3542
+ if(typeof(node.nodeValue) === 'string' && node.nodeValue.trim().length > 0){
3543
+ //返回传入的node本身
3544
+ resultArray.push({
3545
+ text: node.nodeValue,
3546
+ attribute: '',
3547
+ node: node
3548
+ });
3549
+ }
3550
+
3551
+
3552
+ return resultArray;
3553
+ },
3363
3554
  /*
3364
3555
  进行翻译之后的渲染显示
3365
3556
  注意,它会对node本身进行扫描的,需要进行通过文本翻译接口进行翻译的文本进行识别,比如 这个 node 其内容为:
@@ -3467,6 +3658,7 @@ var translate = {
3467
3658
 
3468
3659
  //正常的node ,typeof 都是 object
3469
3660
 
3661
+ /* 这里是通用方法,不应该有限制
3470
3662
  //console.log(typeof(node)+node);
3471
3663
  if(nodename == '#text'){
3472
3664
  //如果是普通文本,判断一下上层是否是包含在textarea标签中
@@ -3480,7 +3672,7 @@ var translate = {
3480
3672
  }
3481
3673
  }
3482
3674
  }
3483
-
3675
+ */
3484
3676
 
3485
3677
 
3486
3678
  //console.log(nodename)
@@ -3639,11 +3831,23 @@ var translate = {
3639
3831
  resultShowText: translate.element.nodeAnalyse.analyse 进行设置翻译后的文本渲染时,提前计算好这个node显示的所有文本,然后在赋予 dom,这里是计算好的node要整体显示的文本
3640
3832
  */
3641
3833
  analyseReplaceBefore_DateToTranslateNode:function(node, attribute, resultShowText){
3642
- if(translate.node.find(node)){
3643
- if(typeof(translate.node.get(node).translateResults) == 'undefined'){
3644
- translate.node.get(node).translateResults = {};
3834
+ var translateNode; //当前操作的,要记录入 translate.node 中的,进行翻译的node
3835
+ var translateNode_attribute = ''; //当前操作的是node中的哪个attribute,如果没有是node本身则是空字符串
3836
+
3837
+ if(typeof(attribute) === 'string' && attribute.length > 0){
3838
+ //是操作的元素的某个属性
3839
+ translateNode = node.getAttributeNode(attribute);
3840
+ translateNode_attribute = attribute;
3841
+ }else{
3842
+ //操作的就是node本身
3843
+ translateNode = node;
3844
+ }
3845
+
3846
+ if(translate.node.find(translateNode)){
3847
+ if(typeof(translate.node.get(translateNode).translateResults) == 'undefined'){
3848
+ translate.node.get(translateNode).translateResults = {};
3645
3849
  }
3646
- translate.node.get(node).translateResults[resultShowText] = 1;
3850
+ translate.node.get(translateNode).translateResults[resultShowText] = 1;
3647
3851
  }else{
3648
3852
  //翻译过程中,会有时间差,比如通过文本翻译api请求,这时node元素本身被其他js改变了,导致翻译完成后,原本的node不存在了
3649
3853
  //console.log('[debug] 数据异常,analyse - set 中发现 translate.node 中的 node 不存在,理论上应该只要被扫描了,被翻译了,到这里就一定会存在的,不存在怎么会扫描到交给去翻译呢');
@@ -3690,7 +3894,7 @@ var translate = {
3690
3894
 
3691
3895
  //console.log('---'+typeof(node)+', ');
3692
3896
  //判断是否是有title属性,title属性也要翻译
3693
- if(typeof(node) == 'object' && typeof(node['title']) == 'string' && node['title'].length > 0){
3897
+ if(typeof(node) == 'object' && typeof(node['title']) == 'string' && node['title'].trim().length > 0){
3694
3898
  //将title加入翻译队列
3695
3899
  //console.log('---'+node.title+'\t'+node.tagName);
3696
3900
  //console.log(node)
@@ -3699,7 +3903,8 @@ var translate = {
3699
3903
  //判断当前元素是否在ignore忽略的tag、id、class name中
3700
3904
  if(!translate.ignore.isIgnore(node)){
3701
3905
  //不在忽略的里面,才会加入翻译
3702
- translate.addNodeToQueue(uuid, node, node['title'], 'title');
3906
+ //translate.addNodeToQueue(uuid, node, node['title'], 'title');
3907
+ translate.addNodeToQueue(uuid, node.getAttributeNode('title'), node['title'], '');
3703
3908
  }
3704
3909
  }
3705
3910
 
@@ -3753,7 +3958,7 @@ var translate = {
3753
3958
  //判断当前元素是否在ignore忽略的tag、id、class name中 v3.15.7 增加
3754
3959
  if(!translate.ignore.isIgnore(node)){
3755
3960
  //加入翻译
3756
- translate.addNodeToQueue(uuid, node, attributeValue, attributeName);
3961
+ translate.addNodeToQueue(uuid, node.getAttributeNode(attributeName), attributeValue, '');
3757
3962
  }
3758
3963
  }
3759
3964
  }
@@ -3776,22 +3981,16 @@ var translate = {
3776
3981
  if(node == null || typeof(node) == 'undefined'){
3777
3982
  return;
3778
3983
  }
3779
- if(node.parentNode == null){
3780
- return;
3781
- }
3782
-
3783
- //console.log('-----parent')
3784
- var parentNodeName = translate.element.getNodeName(node.parentNode);
3785
- //node.parentNode.nodeName;
3786
- if(parentNodeName == ''){
3787
- return;
3788
- }
3789
- if(translate.ignore.tag.indexOf(parentNodeName.toLowerCase()) > -1){
3790
- //忽略tag
3791
- //console.log('忽略tag:'+parentNodeName);
3792
- return;
3984
+ if(node.nodeType === 2){ //是属性node,比如 div 的 title 属性的 node
3985
+ if(node.ownerElement == null){
3986
+ return;
3987
+ }
3988
+ }else{ //是元素了
3989
+ if(node.parentNode == null){
3990
+ return;
3991
+ }
3793
3992
  }
3794
-
3993
+
3795
3994
  /****** 判断忽略的class ******/
3796
3995
  /*
3797
3996
  这段理论上不需要了,因为在 translate.ignore.isIgnore 判断了
@@ -3825,13 +4024,21 @@ var translate = {
3825
4024
  return;
3826
4025
  }
3827
4026
 
3828
- //node分析
4027
+ //node分析,分析这个node的所有可翻译属性(包含自定义翻译属性 translate.element.tagAttribute )
4028
+ var nodeAnalyChild = translate.element.nodeAnalyse.gets(node);
4029
+ //console.log(nodeAnalyChild);
4030
+ for(var nci = 0; nci < nodeAnalyChild.length; nci++){
4031
+ translate.addNodeToQueue(uuid, nodeAnalyChild[nci].node, nodeAnalyChild[nci].text, '');
4032
+ }
4033
+ /*
3829
4034
  var nodeAnaly = translate.element.nodeAnalyse.get(node);
3830
4035
  if(nodeAnaly['text'].length > 0){
3831
4036
  //有要翻译的目标内容,加入翻译队列
3832
- //console.log('addNodeToQueue -- '+nodeAnaly['node']+', text:' + nodeAnaly['text']);
3833
- translate.addNodeToQueue(uuid, nodeAnaly['node'], nodeAnaly['text']);
4037
+ console.log(nodeAnaly)
4038
+ console.log('addNodeToQueue -- '+nodeAnaly['node']+', text:' + nodeAnaly['text']);
4039
+ translate.addNodeToQueue(uuid, nodeAnaly['node'], nodeAnaly['text'], '');
3834
4040
  }
4041
+ */
3835
4042
 
3836
4043
  //console.log(nodeAnaly);
3837
4044
  /*
@@ -3892,6 +4099,56 @@ var translate = {
3892
4099
  */
3893
4100
 
3894
4101
  },
4102
+ /*
4103
+ 将node转为element输出。
4104
+ 如果node是文本元素,则转化为这个文本元素所在的element元素
4105
+ 如果node是属性,则转化为这个属性所在的element元素
4106
+ 如果node本身就是元素标签,那就还是这样返回。
4107
+
4108
+
4109
+ nodes: node数组,传入如 [node1,node2, ...] 它里面可能包含 node.nodeType 1\2\3 等值
4110
+
4111
+ 返回这些node转化为所在元素后的数组,返回如 [element1, element2, ...]
4112
+ 注意的是
4113
+ 1. 输出的一定是 element 元素,也就是 node.nodeType 一定等于1
4114
+ 2. 输出的元素数组不一定等于传入的nodes数组,也就是他们的数量跟下标并不是对应相等的
4115
+
4116
+ */
4117
+ nodeToElement: function(nodes){
4118
+ var elements = new Array(); //要改动的元素
4119
+
4120
+ //遍历所有node组合到 nodes. 这个不单纯只是遍历组合,它会判断如果是文本节点,则取它的父级元素。它组合的结果是元素的集合
4121
+ for(var r = 0; r<nodes.length; r++){
4122
+ var node = nodes[r];
4123
+ if(typeof(node) == 'undefined' || typeof(node.parentNode) == 'undefined'){
4124
+ continue;
4125
+ }
4126
+ if(node.nodeType === 2){
4127
+ //是属性节点,可能是input、textarea 的 placeholder ,获取它的父元素
4128
+ var nodeParentElement = node.ownerElement;
4129
+ if(nodeParentElement == null){
4130
+ continue;
4131
+ }
4132
+ elements.push(nodeParentElement);
4133
+ }else if(node.nodeType === 3){
4134
+ //是文本节点
4135
+ var nodeParentElement = node.parentNode;
4136
+ if(nodeParentElement == null){
4137
+ continue;
4138
+ }
4139
+ elements.push(nodeParentElement);
4140
+ }else if(node.nodeType === 1){
4141
+ //元素节点了,直接加入
4142
+ elements.push(node);
4143
+ }else{
4144
+ //1\2\3 都不是,这不应该是 translate.js 中应该出现的
4145
+ translate.log('translate.element.nodeToElement 中,发现传入的node.nodeType 类型有异常,理论上不应该存在, node.nodeType:'+node.nodeType);
4146
+ translate.log(node);
4147
+ }
4148
+ }
4149
+
4150
+ return elements;
4151
+ }
3895
4152
  },
3896
4153
 
3897
4154
 
@@ -3957,27 +4214,34 @@ var translate = {
3957
4214
 
3958
4215
 
3959
4216
  /***** 记录这个node 到 translate.node.data,这也是node进入 translate.node.data 记录的第一入口 *****/
3960
- if(translate.node.get(node) == null){
3961
- translate.node.set(node, {});
4217
+ var translateNode; //当前操作的,要记录入 translate.node 中的,进行翻译的node
4218
+ var translateNode_attribute = ''; //当前操作的是node中的哪个attribute,如果没有是node本身则是空字符串
4219
+ if(typeof(attribute) === 'string' && attribute.length > 0){
4220
+ //是操作的元素的某个属性
4221
+ translateNode = node.getAttributeNode(attribute);
4222
+ translateNode_attribute = attribute;
4223
+ }else{
4224
+ //操作的就是node本身
4225
+ translateNode = node;
3962
4226
  }
3963
-
3964
-
3965
- var nodeAttribute = translate.node.getAttribute(attribute);
4227
+ if(translate.node.get(translateNode) == null){
4228
+ translate.node.set(translateNode, {});
4229
+ }
4230
+
4231
+ //var nodeAttribute = translate.node.getAttribute(attribute);
3966
4232
  //console.log(text+'-----:');
3967
4233
  //console.log(nodeAttribute);
3968
- if(typeof(translate.node.get(node)[nodeAttribute.key]) == 'undefined'){
3969
- translate.node.get(node)[nodeAttribute.key] = {};
3970
- //console.log(typeof(translate.node.get(node)[nodeAttribute.key]));
3971
- }
3972
- translate.node.get(node)[nodeAttribute.key].attribute = nodeAttribute.attribute;
3973
- if(typeof(translate.node.get(node)[nodeAttribute.key].originalText) == 'string'){
4234
+ //if(typeof(translate.node.get(translateNode)[nodeAttribute.key]) == 'undefined'){
4235
+ // translate.node.get(node)[nodeAttribute.key] = {};
4236
+ //}
4237
+ translate.node.get(translateNode).attribute = translateNode_attribute;
4238
+ if(typeof(translate.node.get(translateNode).originalText) === 'string'){
3974
4239
  //这个节点有过记录原始显示的文本了,那么不再对其进行后续的扫描,除非它有被触发过动态监听元素改变, --- 至于它有被触发过动态监听元素改变--后续想怎么判定
3975
4240
  //console.log(translate.node.get(node)[nodeAttribute.key].originalText+'\t又过了,不在翻译');
3976
4241
  return;
3977
4242
  }else{
3978
4243
  //没有过,是第一次,那么赋予值
3979
- translate.node.get(node)[nodeAttribute.key].originalText = text;
3980
- //console.log(translate.node.get(node));
4244
+ translate.node.get(translateNode).originalText = text;
3981
4245
  }
3982
4246
  //console.log(translate.node.get(node)[nodeAttribute.key]);
3983
4247
  /*
@@ -4007,13 +4271,12 @@ var translate = {
4007
4271
  return;
4008
4272
  }
4009
4273
  */
4010
- if(typeof(translate.node.get(node)[nodeAttribute.key].translateTexts) == 'undefined'){
4011
- translate.node.get(node)[nodeAttribute.key].translateTexts = {};
4274
+ if(typeof(translate.node.get(translateNode).translateTexts) === 'undefined'){
4275
+ translate.node.get(translateNode).translateTexts = {};
4012
4276
  }
4013
4277
  /***** 自检完毕,准备进行翻译了 *****/
4014
4278
 
4015
4279
 
4016
-
4017
4280
  //原本传入的text会被切割为多个小块
4018
4281
  var textArray = new Array();
4019
4282
  textArray.push(text); //先将主 text 赋予 ,后面如果对主text进行加工分割,分割后会将主text给删除掉
@@ -4053,8 +4316,8 @@ var translate = {
4053
4316
 
4054
4317
  //console.log(textArray);
4055
4318
  textArray = translate.nomenclature.dispose(textArray, temporaryIgnoreTexts[ti], temporaryIgnoreTexts[ti], {
4056
- node:node,
4057
- attribute:attribute
4319
+ node:translateNode,
4320
+ attribute:''
4058
4321
  }).texts;
4059
4322
  //console.log(textArray);
4060
4323
  }
@@ -4082,8 +4345,8 @@ var translate = {
4082
4345
  //console.log('----translate.nomenclature.dispose---');
4083
4346
  //console.log(textArray);
4084
4347
  var nomenclatureDispose = translate.nomenclature.dispose(textArray, nomenclatureKey, nomenclatureValue, {
4085
- node:node,
4086
- attribute:attribute
4348
+ node:translateNode,
4349
+ attribute:''
4087
4350
  });
4088
4351
 
4089
4352
  textArray = nomenclatureDispose.texts;
@@ -4107,10 +4370,10 @@ var translate = {
4107
4370
 
4108
4371
  //记录 nodeHistory - 判断text是否已经被拆分了
4109
4372
  if(textArray.length > 0 && textArray[0] != text){ //主要是后面的是否相等,前面的>0只是避免代码报错
4110
- translate.node.get(node)[nodeAttribute.key].whole = false; //已经被拆分了,不是整体翻译了
4373
+ translate.node.get(translateNode).whole = false; //已经被拆分了,不是整体翻译了
4111
4374
  //这时,也默认给其赋值操作,将自定义术语匹配后的结果进行赋予
4112
4375
  }else{
4113
- translate.node.get(node)[nodeAttribute.key].whole = true; //未拆分,是整体翻译
4376
+ translate.node.get(translateNode).whole = true; //未拆分,是整体翻译
4114
4377
  }
4115
4378
  //成功加入到 nodeQueue 的对象。 如果长度为0,那就是还没有加入到 translate.nodeQueue 中,可能全被自定义术语命中了
4116
4379
  var addQueueObjectArray = [];
@@ -4151,14 +4414,14 @@ var translate = {
4151
4414
  // translate.node 记录
4152
4415
 
4153
4416
  // 记录当前有 translate.js 所触发翻译之后渲染到dom界面显示的时间,13位时间戳
4154
- translate.node.get(node).lastTranslateRenderTime = Date.now();
4417
+ translate.node.get(translateNode).lastTranslateRenderTime = Date.now();
4155
4418
  //将具体通过文本翻译接口进行翻译的文本记录到 translate.node.data
4156
- translate.node.get(node)[nodeAttribute.key].translateTexts = {}; //这里全部命中了,所以根本没有走翻译接口的文本
4419
+ translate.node.get(translateNode).translateTexts = {}; //这里全部命中了,所以根本没有走翻译接口的文本
4157
4420
  //将翻译完成后要显示出的文本进行记录
4158
- translate.node.get(node)[nodeAttribute.key].resultText = translate.element.nodeAnalyse.get(node).text; //直接获取当前node显示出来的文本作为最后的结果的文本
4421
+ translate.node.get(translateNode).resultText = translate.element.nodeAnalyse.get(node, attribute).text; //直接获取当前node显示出来的文本作为最后的结果的文本
4159
4422
 
4160
4423
  //将其加入 translate.history.translateTexts 中
4161
- translate.history.translateText.add(translate.node.get(node)[nodeAttribute.key].originalText, translate.node.get(node)[nodeAttribute.key].resultText);
4424
+ translate.history.translateText.add(translate.node.get(translateNode).originalText, translate.node.get(translateNode).resultText);
4162
4425
  }
4163
4426
 
4164
4427
  },
@@ -8341,24 +8604,7 @@ var translate = {
8341
8604
  config.notTranslateTip = true;
8342
8605
  }
8343
8606
 
8344
- var currentLanguage = translate.language.getCurrent(); //获取当前翻译至的语种
8345
-
8346
- var lastUuid = ''; //最后一次的uuid
8347
- for(var queue in translate.nodeQueue){
8348
- if (!translate.nodeQueue.hasOwnProperty(queue)) {
8349
- continue;
8350
- }
8351
- lastUuid = queue;
8352
- }
8353
- //console.log(queue);
8354
-
8355
- if(lastUuid == ''){
8356
- if(config.selectLanguageRefreshRender){
8357
- translate.log('提示,当前还未执行过翻译,所以 translate.reset(); 还原至翻译前的执行指令忽略');
8358
- }
8359
- return;
8360
- }
8361
-
8607
+
8362
8608
  /*
8363
8609
  for(var lang in translate.nodeQueue[lastUuid].list){
8364
8610
  if (!translate.nodeQueue[lastUuid].list.hasOwnProperty(lang)) {
@@ -8419,16 +8665,20 @@ var translate = {
8419
8665
  if (!translate.node.get(key) == null) {
8420
8666
  continue;
8421
8667
  }
8422
- for(var attr in translate.node.get(key)){
8423
- if (!translate.node.get(key).hasOwnProperty(attr)) {
8424
- continue;
8425
- }
8426
- var analyse = translate.element.nodeAnalyse.get(key,translate.node.get(key)[attr].attribute);
8427
- if(typeof(translate.node.get(key)[attr].originalText) != 'string'){
8668
+ //for(var attr in translate.node.get(key)){
8669
+ //if (!translate.node.get(key).hasOwnProperty(attr)) {
8670
+ // continue;
8671
+ //}
8672
+
8673
+ //var analyse = translate.element.nodeAnalyse.get(key,translate.node.get(key).attribute);
8674
+ if(typeof(translate.node.get(key).originalText) !== 'string'){
8428
8675
  continue;
8429
8676
  }
8430
- translate.element.nodeAnalyse.analyse(key, analyse.text, translate.node.get(key)[attr].originalText, translate.node.get(key)[attr].attribute);
8431
- }
8677
+ //translate.element.nodeAnalyse.analyse(key, analyse.text, translate.node.get(key).originalText, translate.node.get(key).attribute);
8678
+
8679
+ //标注此次改动是有 translate.js 导致的 -- 这里就不用标记了,因为先已经移除了 translate.listener.observer 监听,所以不会再监听到还原的操作了
8680
+ key.nodeValue = translate.node.get(key).originalText;
8681
+ //}
8432
8682
  }
8433
8683
 
8434
8684
 
@@ -8449,6 +8699,15 @@ var translate = {
8449
8699
  translate.storage.set('to', '');
8450
8700
  translate.to = null;
8451
8701
 
8702
+ //清除文本翻译记录
8703
+ if(translate.history.translateText.originalMap !== null){
8704
+ translate.history.translateText.originalMap.clear();
8705
+ }
8706
+ if(translate.history.translateText.resultMap !== null){
8707
+ translate.history.translateText.resultMap.clear();
8708
+ }
8709
+
8710
+
8452
8711
  //重新绘制 select 选择语言
8453
8712
  if(config.selectLanguageRefreshRender){
8454
8713
  translate.selectLanguageTag.refreshRender();
@@ -8677,7 +8936,7 @@ var translate = {
8677
8936
  if(typeof(translatejsTextElementHidden) == 'undefined' || translatejsTextElementHidden == null){
8678
8937
  const style = document.createElement('style');
8679
8938
  // 设置 style 元素的文本内容为要添加的 CSS 规则
8680
- style.textContent = ' .translatejs-text-element-hidden{color: transparent !important; text-shadow: none !important;}';
8939
+ style.textContent = ' .translatejs-text-element-hidden, .translatejs-text-element-hidden[type="text"]::placeholder{color: transparent !important; -webkit-text-fill-color: transparent !important; text-shadow: none !important;} ';
8681
8940
  style.id = 'translatejs-text-element-hidden';
8682
8941
  // 将 style 元素插入到 head 元素中
8683
8942
  document.head.appendChild(style);
@@ -8698,38 +8957,16 @@ var translate = {
8698
8957
  if(translate.progress.api.isTip){
8699
8958
  //translate.listener.execute.renderStartByApi.push(function(uuid, from, to){
8700
8959
  translate.lifecycle.execute.translateNetworkBefore.push(function(data){
8701
- var nodes = new Array(); //要改动的元素节点
8702
-
8703
- //遍历所有node组合到 nodes
8704
- for(var r = 0; r<data.nodes.length; r++){
8705
- var node = data.nodes[r];
8706
- if(typeof(node) == 'undefined' || typeof(node.parentNode) == 'undefined'){
8707
- continue;
8708
- }
8709
- nodes.push(node);
8710
- }
8960
+ //取出当前变动的node,对应的元素
8961
+ var elements = translate.element.nodeToElement(data.nodes);
8962
+ //console.log(elements)
8711
8963
 
8712
8964
  //隐藏所有node的文本
8713
- for(var r = 0; r<nodes.length; r++){
8714
- if(nodes[r].nodeType === 1){
8715
- nodes[r].className = nodes[r].className+' translatejs-text-element-hidden';
8716
- }else{
8717
- //不是元素,那么就取父级了 -- 这里即使翻译的属性,也要进行,比如 value 的 placeholder
8718
- var nodeParent = nodes[r].parentNode;
8719
- if(nodeParent == null){
8720
- continue;
8721
- }
8722
- if(typeof(nodeParent.className) != 'undefined' && nodeParent.className != null && nodeParent.className.indexOf('translatejs-text-element-hidden') > -1){
8723
- //父有了,那么子就不需要再加了
8724
- continue;
8725
- }else{
8726
- //没有,添加
8727
- nodeParent.className = nodeParent.className+' translatejs-text-element-hidden';
8728
- }
8729
- }
8965
+ for(var r = 0; r<elements.length; r++){
8966
+ elements[r].className = elements[r].className+' translatejs-text-element-hidden';
8730
8967
  }
8731
8968
 
8732
- var rects = translate.visual.getRects(nodes);
8969
+ var rects = translate.visual.getRects(elements);
8733
8970
  //console.log(rects)
8734
8971
  var rectsOneArray = translate.visual.rectsToOneArray(rects);
8735
8972
 
@@ -8743,70 +8980,29 @@ var translate = {
8743
8980
 
8744
8981
  var rectLineSplit = translate.visual.filterRectsByLineInterval(rectsOneArray, 2);
8745
8982
  for(var r = 0; r<rectLineSplit.length; r++){
8746
- if(rectLineSplit[r].node.nodeType === 1){
8747
- rectLineSplit[r].node.className = rectLineSplit[r].node.className+' translate_api_in_progress';
8748
- }else{
8749
- //不是元素,那么就取父级了
8750
- var nodeParent = rectLineSplit[r].node.parentNode;
8751
- if(nodeParent == null){
8752
- continue;
8753
- }
8754
- if(typeof(nodeParent.className) != 'undefined' && nodeParent.className != null && nodeParent.className.indexOf('translate_api_in_progress') > -1){
8755
- //父有了,那么子就不需要再加了
8756
- continue;
8757
- }else{
8758
- //没有,添加
8759
- nodeParent.className = nodeParent.className+' translate_api_in_progress';
8760
- }
8761
- }
8983
+ if(typeof(rectLineSplit[r].node.className) === 'string' && rectLineSplit[r].node.className.indexOf('translate_api_in_progress') > -1){
8984
+ //已经存在了,就不继续加了
8985
+ }else{
8986
+ rectLineSplit[r].node.className = rectLineSplit[r].node.className+' translate_api_in_progress';
8987
+ }
8762
8988
  }
8763
-
8764
8989
  });
8765
8990
 
8766
- translate.listener.execute.renderFinishByApi.push(function(uuid, from, to){
8767
- //translate.lifecycle.execute.renderFinish.push(function(uuid, to){ 这个是所有的全完成,得用单一的from完成就要立即对完成的from的显示,不然全完成就太慢了
8768
- for(var hash in translate.nodeQueue[uuid].list[from]){
8769
- if (!translate.nodeQueue[uuid].list[from].hasOwnProperty(hash)) {
8770
- continue;
8771
- }
8772
-
8773
- for(var nodeindex in translate.nodeQueue[uuid].list[from][hash].nodes){
8774
- if (!translate.nodeQueue[uuid].list[from][hash].nodes.hasOwnProperty(nodeindex)) {
8775
- continue;
8776
- }
8777
-
8778
- var node = translate.nodeQueue[uuid].list[from][hash].nodes[nodeindex].node;
8779
-
8780
- var operationNode;
8781
- if(node.nodeType === 1){
8782
- //是元素
8783
- operationNode = node;
8784
- }else{
8785
- //节点,如 #text
8786
- operationNode = node.parentNode;
8787
- if(operationNode == null){
8788
- continue;
8789
- }
8790
- }
8791
-
8792
-
8793
-
8794
-
8795
- if(typeof(operationNode.className) != 'undefined' && operationNode.className != null){
8796
-
8797
- if(operationNode.className.indexOf('translatejs-text-element-hidden') > -1){
8798
- operationNode.className = operationNode.className.replace(/translatejs-text-element-hidden/g, '');
8799
- }
8800
- if(operationNode.className.indexOf('translate_api_in_progress') > -1){
8801
- operationNode.className = operationNode.className.replace(/translate_api_in_progress/g, '');
8802
- }
8991
+ translate.lifecycle.execute.translateNetworkAfter.push(function(data){
8992
+ //取出当前变动的node,对应的元素
8993
+ var elements = translate.element.nodeToElement(data.nodes);
8994
+
8995
+ for(var r = 0; r<elements.length; r++){
8996
+ if(typeof(elements[r].className) === 'string'){
8997
+ if(elements[r].className.indexOf('translatejs-text-element-hidden') > -1){
8998
+ elements[r].className = elements[r].className.replace(/translatejs-text-element-hidden/g, '');
8999
+ }
9000
+ if(elements[r].className.indexOf('translate_api_in_progress') > -1){
9001
+ elements[r].className = elements[r].className.replace(/translate_api_in_progress/g, '');
8803
9002
  }
8804
-
8805
-
8806
- //nodeParent.className = parentClassName.replace(/translate_api_in_progress/g, '');
8807
-
8808
- }
8809
- }
9003
+ }
9004
+ }
9005
+
8810
9006
 
8811
9007
  });
8812
9008
 
@@ -10455,13 +10651,11 @@ var translate = {
10455
10651
  });
10456
10652
  //将其记录到 translate.node.data
10457
10653
  translate.node.set(textNode,{
10458
- translate_default_value:{
10459
- attribute:"",
10460
- originalText: originalText,
10461
- resultText: text,
10462
- translateTexts: {}, //这里因为直接从缓存中取的,没有走网络接口,所以这里直接空
10463
- whole: true
10464
- },
10654
+ attribute:"",
10655
+ originalText: originalText,
10656
+ resultText: text,
10657
+ translateTexts: {}, //这里因为直接从缓存中取的,没有走网络接口,所以这里直接空
10658
+ whole: true,
10465
10659
  translateResults: {
10466
10660
  [originalText]:1
10467
10661
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18n-jsautotranslate",
3
- "version": "3.18.63",
3
+ "version": "3.18.66",
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
  "scripts": {