forked from gammasoft/browser-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.js
More file actions
113 lines (93 loc) · 3.13 KB
/
i18n.js
File metadata and controls
113 lines (93 loc) · 3.13 KB
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
(function(factory) {
var root = (typeof self == 'object' && self.self === self && self) ||
(typeof global == 'object' && global.global === global && global);
// AMD support
if (typeof define === 'function' && define.amd) {
define(['jquery'], function($) {
return factory($);
});
// CommonJS and Node.js module support.
} else if (typeof exports !== 'undefined') {
var $;
try { $ = require('jquery'); } catch (e) {}
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = factory($);
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.I18n = factory($);
} else {
root.I18n = factory(root.jQuery || root.Zepto || root.ender || root.$);
}
})(function($) {
var I18n = function(options){
for (var prop in options) {
this[prop] = options[prop];
};
return this.getLocaleFileFromServer();
};
I18n.localeCache = {};
I18n.prototype = {
defaultLocale: "en",
directory: "/locales",
extension: ".json",
setLocale: function(locale) {
this.locale = locale;
if (!I18n.localeCache.get(locale)) {
this.getLocaleFileFromServer(locale);
}
return this;
},
getLocale: function(){
return navigator.language;
},
_getLocaleFileFromServer: function(locale) {
return $.ajax({
url: this.directory + "/" + locale + this.extension,
dataType: 'json',
async: true
});
},
getLocaleFileFromServer: function() {
var _this = this,
deferred = $.Deferred();
this.locale = this.locale || this.getLocale();
this._getLocaleFileFromServer(this.locale)
.done(function(localeFile) {
I18n.localeCache[_this.locale] = localeFile;
deferred.resolve(_this);
})
.fail(function(res) {
_this.locale = _this.defaultLocale;
localeFile = _this._getLocaleFileFromServer(_this.defaultLocale)
.done(function(localeFile) {
I18n.localeCache[_this.locale] = localeFile;
deferred.resolve(_this);
})
.fail(function(res) {
deferred.reject(_this);
});
});
return deferred;
},
__: function(){
var msg = I18n.localeCache[this.locale][arguments[0]];
if (arguments.length > 1)
msg = vsprintf(msg, Array.prototype.slice.call(arguments, 1));
return msg;
},
__n: function(singular, count){
var msg = I18n.localeCache[this.locale][singular];
count = parseInt(count, 10);
if(count === 0)
msg = msg.zero;
else
msg = count > 1 ? msg.other : msg.one;
msg = vsprintf(msg, [count]);
if (arguments.length > 2)
msg = vsprintf(msg, Array.prototype.slice.call(arguments, 2));
return msg;
}
};
return I18n;
});