-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathjquery.i18n.js
120 lines (109 loc) · 2.92 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
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
115
116
117
118
119
120
/*!
* jQuery i18n plugin
* @requires jQuery v1.1 or later
*
* See https://github.com/recurser/jquery-i18n
*
* Licensed under the MIT license.
*
* Version: 1.1.2 (Fri, 11 Aug 2017 03:52:21 GMT)
*/
(function($) {
/**
* i18n provides a mechanism for translating strings using a jscript dictionary.
*
*/
var __slice = Array.prototype.slice;
/*
* i18n property list
*/
var i18n = {
dict: null,
missingPattern: null,
/**
* load()
*
* Load translations.
*
* @param property_list i18nDict : The dictionary to use for translation.
*/
load: function(i18nDict, missingPattern) {
if (this.dict !== null) {
$.extend(this.dict, i18nDict);
} else {
this.dict = i18nDict;
}
if (missingPattern) {
this.missingPattern = missingPattern;
}
},
/**
* unload()
*
* Unloads translations and clears the dictionary.
*/
unload: function() {
this.dict = null;
this.missingPattern = null;
},
/**
* _()
*
* Looks the given string up in the dictionary and returns the translation if
* one exists. If a translation is not found, returns the original word.
*
* @param string str : The string to translate.
* @param property_list params.. : params for using printf() on the string.
*
* @return string : Translated word.
*/
_: function (str) {
dict = this.dict;
if (dict && dict.hasOwnProperty(str)) {
str = dict[str];
} else if (this.missingPattern !== null) {
return this.printf(this.missingPattern, str);
}
args = __slice.call(arguments);
args[0] = str;
// Substitute any params.
return this.printf.apply(this, args);
},
/*
* printf()
*
* Substitutes %s with parameters given in list. %%s is used to escape %s.
*
* @param string str : String to perform printf on.
* @param string args : Array of arguments for printf.
*
* @return string result : Substituted string
*/
printf: function(str, args) {
if (arguments.length < 2) return str;
args = $.isArray(args) ? args : __slice.call(arguments, 1);
return str.replace(/([^%]|^)%(?:(\d+)\$)?s/g, function(p0, p, position) {
if (position) {
return p + args[parseInt(position)-1];
}
return p + args.shift();
}).replace(/%%s/g, '%s');
}
};
/*
* _t()
*
* Allows you to translate a jQuery selector.
*
* eg $('h1')._t('some text')
*
* @param string str : The string to translate .
* @param property_list params : Params for using printf() on the string.
*
* @return element : Chained and translated element(s).
*/
$.fn._t = function(str, params) {
return $(this).html(i18n._.apply(i18n, arguments));
};
$.i18n = i18n;
})(jQuery);