-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation.js
74 lines (60 loc) · 2.36 KB
/
translation.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
$.fn.replaceText = function( search, replace, text_only ) {
return this.each(function(){
var node = this.firstChild,
val,
new_val,
// Elements to be removed at the end.
remove = [];
// Only continue if firstChild exists.
if ( node ) {
// Loop over all childNodes.
do {
// Only process text nodes.
if ( node.nodeType === 3 ) {
// The original node value.
val = node.nodeValue;
// The new value.
new_val = val.replace( search, replace );
// Only replace text if the new value is actually different!
if ( new_val !== val ) {
if ( !text_only && /</.test( new_val ) ) {
// The new value contains HTML, set it in a slower but far more
// robust way.
$(node).before( new_val );
// Don't remove the node yet, or the loop will lose its place.
remove.push( node );
} else {
// The new value contains no HTML, so it can be set in this
// very fast, simple way.
node.nodeValue = new_val;
}
}
}
} while ( node == node.nextSibling );
}
// Time to remove those elements!
if(remove.length){ $(remove).remove(); }
});
};
var toTranslate = [];
$.each( Dictionary, function( key, value ) {
toTranslate.push(key);
});
toTranslate = new RegExp(toTranslate.join("|"), 'gi');
var selector= 'a, b, p, h6, h5, h4, h3, h2, h1, option, strong, span, div, dd, dt, td, th, button, header, footer, form, input, select, textarea';
jQuery(document).ready(function() {
jQuery('*').replaceText(toTranslate, function(matched){
return Dictionary[matched];
});
jQuery('textarea').keyup(function() {
var value = jQuery(this).val().toString().replace(toTranslate, function(matched) {
return Dictionary[matched];
});
jQuery(this).val(value);
});
jQuery('body').bind("DOMSubtreeModified",function(){
jQuery(selector).replaceText(toTranslate, function(matched){
return Dictionary[matched];
});
});
});