-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.translate.js
108 lines (95 loc) · 3.32 KB
/
jquery.translate.js
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
(function($) {
$.fn.jtranslate = function(options) {
var self = this,
settings = {
classVal: "jqt",
lang: "en",
dictionary: null
},
settings = $.extend(settings, options || {});
this.lang = function(language) {
if (language.length) {
settings.lang = language;
this.jtranslate(settings);
}
return settings.lang;
};
this.get = function(index) {
var res = "";
try {
res = settings.dictionary[index][settings.lang];
} catch (err) {
res = "";
return res;
}
if (res.length) {
return res;
}
return index;
};
//search and translate
this.find("."+settings.classVal).each(function() {
var $this = $(this),
key = "",
textValue = "",
textNodes,
textNodeCount = 0,
i = 0,
trnKey = "",
indexArray = [];
indexArray = $.data($this, "keyСount");
if (!indexArray) {
indexArray = [];
textNodes = $this.currentNodeText();
textNodeCount = textNodes.length;
for (i = 0; i < textNodeCount; i = i + 1) {
indexArray.push(textNodes[i].index);
}
$.data($this, "keyCount", indexArray);
if (textNodeCount > 0) {
for (i = 0; i < textNodeCount; i = i + 1) {
textValue = $.trim($(textNodes[i].textNode).text());
trnKey = textValue.replace(' ', ' ');
$.data($this, "key_" + indexArray[i], trnKey); //store key for next time
}
}
}
for (i = 0; i < indexArray.length; i = i + 1) {
trnKey = $.data($this, "key_" + indexArray[i]);
key = self.get(trnKey);
if (key !== null) {
$this.setCurrentNodeText(key, indexArray[i]);
}
}
});
return this;
};
$.fn.currentNodeText = function() {
var index = 0,
textValue = "",
retObject = [],
p = $(this.contents().filter(function() {
if (this.nodeType == 3) {
textValue = $.trim($(this).text().replace(' ', ' '));
if (textValue != "" && textValue != null && textValue != "undefined") {
//for ie8
retObject.push({
textNode: this,
index: index
});
//not work in ie8
//this.index = index;
index = index + 1;
return this;
}
}
index = index + 1;
}));
return retObject;
}
$.fn.setCurrentNodeText = function(text, index) {
var domObject = this.get(0),
text_to_change = domObject.childNodes[index];
text_to_change.nodeValue = text;
}
})(jQuery);