-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.i18n.js
49 lines (39 loc) · 1.03 KB
/
jquery.i18n.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
/**
* jQuery i18n Plugin
* Author: Chris Johnson
* Contact: [email protected]
*/
(function($) {
// What does the pluginName plugin do?
$.fn.i18n = function(options) {
if (!this.length) { return this; }
var opts = $.extend(true, {}, $.fn.i18n.defaults, options);
var valueForKey = function(key, data) {
var keys, value, _i, _len;
keys = key.split(/\./);
value = data;
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
value = value != null ? value[key] : null;
}
return value;
};
this.each(function() {
var $this = $(this);
var language = opts.forceLanguage || window.navigator.userLanguage || window.navigator.language;
var fileName = opts.path +'/'+ opts.namespace +'_'+ language +'.json';
$.getJSON(fileName, function(data) {
var key = $this.data('translate');
value = valueForKey(key, data);
$this.html(value);
});
});
return this;
};
// default options
$.fn.i18n.defaults = {
namespace: 'i18n',
path: 'js/lang',
forceLanguage: null
};
})(jQuery);