-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlocalization.js
197 lines (169 loc) · 6.11 KB
/
localization.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(function() {
var locale;
var messages = {};
/* Utility functions: */
/**
* Replace variables used in the message by appropriate values.
*
* @method applyReplacements
* @static
* @param {String} message Input message.
* @param {Object} replacements Associative array: { variableName: "replacement", ... }
* @return {String} The input message with all replacements applied.
*/
var applyReplacements = function (message, replacements) {
for (var replacementName in replacements) {
var replacement = String(replacements[replacementName]);
// 'welcome' => 'Welcome, :name' => 'Welcome, dayle'
message = message.replace(
new RegExp(':' + replacementName, 'g'),
replacement
);
// 'welcome' => 'Welcome, :NAME' => 'Welcome, DAYLE'
message = message.replace(
new RegExp(':' + replacementName.toUpperCase(), 'g'),
replacement.toUpperCase()
);
// 'welcome' => 'Welcome, :Name' => 'Welcome, Dayle'
message = message.replace(
new RegExp(':' + (replacementName.charAt(0).toUpperCase() + replacementName.substr(1)), 'g'),
replacement.charAt(0).toUpperCase() + replacement.substr(1)
);
}
return message;
};
var isEmpty = function (obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
};
/* Lang: */
/**
* Lang class. Works similar to the Laravel Lang object.
* @class Lang
*/
var Lang = {
/**
* Translate a message.
*
* @method get
* @static
* @param {String} messageKey The message key (message identifier).
* @param {Object} [replacements] Associative array: { variableName: "replacement", ... }
* @return {String} Translated message.
*/
get : function(messageKey, replacements, forceLocale) {
var uselocale = locale;
if (forceLocale) {
uselocale = forceLocale;
}
if (typeof messages[uselocale][messageKey] == "undefined") {
/* like Lang::get(), if messageKey is the name of a lang file, return it as an array */
var result = {};
for (var prop in messages[uselocale]) {
if (prop.indexOf(messageKey + '.') > -1) {
result[prop] = messages[uselocale][prop];
}
};
if (!isEmpty(result)) {
return result;
}
/* if there is nothing to return, return messageKey */
return messageKey;
}
var message = messages[uselocale][messageKey];
if (replacements) {
message = applyReplacements(message, replacements);
}
return message;
},
/**
* Returns whether the given message is defined or not.
*
* @method has
* @static
* @param {String} messageKey Message key.
* @return {Boolean} True if the given message exists.
*/
has : function(messageKey) {
return typeof messages[locale][messageKey] != "undefined";
},
/**
* Choose one of multiple message versions, based on
* pluralization rules. Only English pluralization
* supported for now. If `count` is one then the first
* version of the message is retuned, otherwise the
* second version.
*
* @method choice
* @static
* @param {String} messageKey Message key.
* @param {Integer} count Subject count for pluralization.
* @param {Object} [replacements] Associative array: { variableName: "replacement", ... }
* @return {String} Translated message.
*/
choice : function(messageKey, count, replacements) {
if (typeof messages[locale][messageKey] == "undefined") {
return messageKey;
}
var message;
var messageSplitted = messages[locale][messageKey].split('|');
if (count == 1) {
message = messageSplitted[0];
} else {
message = messageSplitted[1];
}
if (replacements) {
message = applyReplacements(message, replacements);
}
return message;
},
/**
* Sets the current locale. Normally only used once
* during initialization. The value comes from the backend.
*
* @method setLocale
* @static
* @param {String} localeId The locale returned by Laravel's Lang::locale().
* @throws {Error} An error is thrown if messages[localeId] is not defined.
*/
setLocale : function(localeId) {
locale = localeId;
if (!messages[localeId]) {
throw new Error(
'No messages defined for locale: "' + localeId + '". ' +
'Did you forget to enable it in the configuration?'
);
}
},
/**
* Returns the current locale.
*
* @method locale
* @static
* @return {String} The current locale.
*/
locale : function() {
return locale;
},
/**
* Used to initialize the message catalog. You may use this
* method to add further messages on runtime if necessary.
*
* @method addMessages
* @static
* @param {Object} _messages An associative array: { messageKey: "message", ... }
*/
addMessages : function(_messages) {
for (var key in _messages) {
messages[key] = _messages[key];
}
}
};
/* Export: */
this.Lang = Lang;
this.trans = Lang.get;
this.transChoice = Lang.choice;
})();