forked from tinoni/translate.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.translate.js
98 lines (75 loc) · 2.01 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
/**
* @file jquery.translate.js
* @brief jQuery plugin to translate text in the client side.
* @author Manuel Fernandes
* @site
* @version 0.9
* @license MIT license <http://www.opensource.org/licenses/MIT>
*
* translate.js is a jQuery plugin to translate text in the client side.
* Added input and textarea placeholder support
* @author Hakan Torun
* @site hakan.io
*/
(function($){
$.fn.translate = function(options) {
var that = this; //a reference to ourselves
var settings = {
css: "trn",
lang: "en"/*,
t: {
"translate": {
pt: "tradução",
br: "tradução"
}
}*/
};
settings = $.extend(settings, options || {});
if (settings.css.lastIndexOf(".", 0) !== 0) //doesn't start with '.'
settings.css = "." + settings.css;
var t = settings.t;
//public methods
this.lang = function(l) {
if (l) {
settings.lang = l;
this.translate(settings); //translate everything
}
return settings.lang;
};
this.get = function(index) {
var res = index;
try {
res = t[index][settings.lang];
}
catch (err) {
//not found, return index
return index;
}
if (res)
return res;
else
return index;
};
this.g = this.get;
//main
this.find(settings.css).each(function(i) {
var $this = $(this);
var trn_key = $this.attr("data-trn-key");
if($this.is('input') || $this.is('textarea')){
if (!trn_key) {
trn_key = $this.attr("placeholder");
$this.attr("data-trn-key", trn_key); //store key for next time
}
$this.attr("placeholder",that.get(trn_key));
}
else{
if (!trn_key) {
trn_key = $this.html();
$this.attr("data-trn-key", trn_key); //store key for next time
}
$this.html(that.get(trn_key));
}
});
return this;
};
})(jQuery);