forked from tinoni/translate.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.translate.js
114 lines (90 loc) · 2.71 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
109
110
111
112
113
114
/**
* @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.
*
*/
(function($){
$.fn.translate = function(options) {
var that = this; //a reference to ourselves
var settings = {
css: "trn",
tag: "trn",
css_attr: "trn-attr",
lang: "en",
debug: false,
};
settings = $.extend(settings, options || {});
if (settings.css.lastIndexOf(".", 0) !== 0) //doesn't start with '.'
settings.css = "." + settings.css;
if (settings.css_attr.lastIndexOf(".", 0) !== 0) //doesn't start with '.'
settings.css_attr = "." + settings.css_attr;
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
if (settings.debug) {
console.log("No '" + settings.lang + "' translation for '" + index + "'");
}
return index;
}
if (res)
return res;
else
return index;
};
this.g = this.get;
//main
this.find(settings.tag).each(function(i) {
var $this = $(this);
var trn_key = $this.attr("data-trn-key");
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));
});
this.find(settings.css).each(function(i) {
var $this = $(this);
var trn_key = $this.attr("data-trn-key");
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));
});
this.find(settings.css_attr).each(function(i) {
var $this = $(this);
var trn_attr_names = $this.attr("data-trn-attr");
trn_attr_names = trn_attr_names.split(" ");
// for each attribute
for (i = 0; i < trn_attr_names.length; i++) {
trn_attr_name = trn_attr_names[i];
var trn_key = $this.attr("data-trn-key-" + trn_attr_name);
if (!trn_key) {
trn_key = $this.attr(trn_attr_name);
$this.attr("data-trn-key-" + trn_attr_name, trn_key); //store key for next time
}
$this.attr(trn_attr_name, that.get(trn_key));
}
});
return this;
};
})(jQuery);