-
Notifications
You must be signed in to change notification settings - Fork 37
/
gettext.js
418 lines (371 loc) · 12.4 KB
/
gettext.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
'use strict';
var get = require('lodash.get');
var plurals = require('./plurals');
module.exports = Gettext;
/**
* Creates and returns a new Gettext instance.
*
* @constructor
* @param {Object} [options] A set of options
* @param {String} options.sourceLocale The locale that the source code and its
* texts are written in. Translations for
* this locale is not necessary.
* @param {Boolean} options.debug Whether to output debug info into the
* console.
* @return {Object} A Gettext instance
*/
function Gettext(options) {
options = options || {};
this.catalogs = {};
this.locale = '';
this.domain = 'messages';
this.listeners = [];
// Set source locale
this.sourceLocale = '';
if (options.sourceLocale) {
if (typeof options.sourceLocale === 'string') {
this.sourceLocale = options.sourceLocale;
}
else {
this.warn('The `sourceLocale` option should be a string');
}
}
// Set debug flag
this.debug = 'debug' in options && options.debug === true;
}
/**
* Adds an event listener.
*
* @param {String} eventName An event name
* @param {Function} callback An event handler function
*/
Gettext.prototype.on = function(eventName, callback) {
this.listeners.push({
eventName: eventName,
callback: callback
});
};
/**
* Removes an event listener.
*
* @param {String} eventName An event name
* @param {Function} callback A previously registered event handler function
*/
Gettext.prototype.off = function(eventName, callback) {
this.listeners = this.listeners.filter(function(listener) {
return (
listener.eventName === eventName &&
listener.callback === callback
) === false;
});
};
/**
* Emits an event to all registered event listener.
*
* @private
* @param {String} eventName An event name
* @param {any} eventData Data to pass to event listeners
*/
Gettext.prototype.emit = function(eventName, eventData) {
for (var i = 0; i < this.listeners.length; i++) {
var listener = this.listeners[i];
if (listener.eventName === eventName) {
listener.callback(eventData);
}
}
};
/**
* Logs a warning to the console if debug mode is enabled.
*
* @ignore
* @param {String} message A warning message
*/
Gettext.prototype.warn = function(message) {
if (this.debug) {
console.warn(message);
}
this.emit('error', new Error(message));
};
/**
* Stores a set of translations in the set of gettext
* catalogs.
*
* @example
* gt.addTranslations('sv-SE', 'messages', translationsObject)
*
* @param {String} locale A locale string
* @param {String} domain A domain name
* @param {Object} translations An object of gettext-parser JSON shape
*/
Gettext.prototype.addTranslations = function(locale, domain, translations) {
if (!this.catalogs[locale]) {
this.catalogs[locale] = {};
}
this.catalogs[locale][domain] = translations;
};
/**
* Sets the locale to get translated messages for.
*
* @example
* gt.setLocale('sv-SE')
*
* @param {String} locale A locale
*/
Gettext.prototype.setLocale = function(locale) {
if (typeof locale !== 'string') {
this.warn(
'You called setLocale() with an argument of type ' + (typeof locale) + '. ' +
'The locale must be a string.'
);
return;
}
if (locale.trim() === '') {
this.warn('You called setLocale() with an empty value, which makes little sense.');
}
if (locale !== this.sourceLocale && !this.catalogs[locale]) {
this.warn('You called setLocale() with "' + locale + '", but no translations for that locale has been added.');
}
this.locale = locale;
};
/**
* Sets the default gettext domain.
*
* @example
* gt.setTextDomain('domainname')
*
* @param {String} domain A gettext domain name
*/
Gettext.prototype.setTextDomain = function(domain) {
if (typeof domain !== 'string') {
this.warn(
'You called setTextDomain() with an argument of type ' + (typeof domain) + '. ' +
'The domain must be a string.'
);
return;
}
if (domain.trim() === '') {
this.warn('You called setTextDomain() with an empty `domain` value.');
}
this.domain = domain;
};
/**
* Translates a string using the default textdomain
*
* @example
* gt.gettext('Some text')
*
* @param {String} msgid String to be translated
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.gettext = function(msgid) {
return this.dnpgettext(this.domain, '', msgid);
};
/**
* Translates a string using a specific domain
*
* @example
* gt.dgettext('domainname', 'Some text')
*
* @param {String} domain A gettext domain name
* @param {String} msgid String to be translated
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.dgettext = function(domain, msgid) {
return this.dnpgettext(domain, '', msgid);
};
/**
* Translates a plural string using the default textdomain
*
* @example
* gt.ngettext('One thing', 'Many things', numberOfThings)
*
* @param {String} msgid String to be translated when count is not plural
* @param {String} msgidPlural String to be translated when count is plural
* @param {Number} count Number count for the plural
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.ngettext = function(msgid, msgidPlural, count) {
return this.dnpgettext(this.domain, '', msgid, msgidPlural, count);
};
/**
* Translates a plural string using a specific textdomain
*
* @example
* gt.dngettext('domainname', 'One thing', 'Many things', numberOfThings)
*
* @param {String} domain A gettext domain name
* @param {String} msgid String to be translated when count is not plural
* @param {String} msgidPlural String to be translated when count is plural
* @param {Number} count Number count for the plural
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.dngettext = function(domain, msgid, msgidPlural, count) {
return this.dnpgettext(domain, '', msgid, msgidPlural, count);
};
/**
* Translates a string from a specific context using the default textdomain
*
* @example
* gt.pgettext('sports', 'Back')
*
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.pgettext = function(msgctxt, msgid) {
return this.dnpgettext(this.domain, msgctxt, msgid);
};
/**
* Translates a string from a specific context using s specific textdomain
*
* @example
* gt.dpgettext('domainname', 'sports', 'Back')
*
* @param {String} domain A gettext domain name
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.dpgettext = function(domain, msgctxt, msgid) {
return this.dnpgettext(domain, msgctxt, msgid);
};
/**
* Translates a plural string from a specific context using the default textdomain
*
* @example
* gt.npgettext('sports', 'Back', '%d backs', numberOfBacks)
*
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated when count is not plural
* @param {String} msgidPlural String to be translated when count is plural
* @param {Number} count Number count for the plural
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.npgettext = function(msgctxt, msgid, msgidPlural, count) {
return this.dnpgettext(this.domain, msgctxt, msgid, msgidPlural, count);
};
/**
* Translates a plural string from a specifi context using a specific textdomain
*
* @example
* gt.dnpgettext('domainname', 'sports', 'Back', '%d backs', numberOfBacks)
*
* @param {String} domain A gettext domain name
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @param {String} msgidPlural If no translation was found, return this on count!=1
* @param {Number} count Number count for the plural
* @return {String} Translation or the original string if no translation was found
*/
Gettext.prototype.dnpgettext = function(domain, msgctxt, msgid, msgidPlural, count) {
var defaultTranslation = msgid;
var translation;
var index;
msgctxt = msgctxt || '';
if (!isNaN(count) && count !== 1) {
defaultTranslation = msgidPlural || msgid;
}
translation = this._getTranslation(domain, msgctxt, msgid);
if (translation) {
if (typeof count === 'number') {
var pluralsFunc = plurals[Gettext.getLanguageCode(this.locale)].pluralsFunc;
index = pluralsFunc(count);
if (typeof index === 'boolean') {
index = index ? 1 : 0;
}
} else {
index = 0;
}
return translation.msgstr[index] || defaultTranslation;
}
else if (!this.sourceLocale || this.locale !== this.sourceLocale) {
this.warn('No translation was found for msgid "' + msgid + '" in msgctxt "' + msgctxt + '" and domain "' + domain + '"');
}
return defaultTranslation;
};
/**
* Retrieves comments object for a translation. The comments object
* has the shape `{ translator, extracted, reference, flag, previous }`.
*
* @example
* const comment = gt.getComment('domainname', 'sports', 'Backs')
*
* @private
* @param {String} domain A gettext domain name
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {Object} Comments object or false if not found
*/
Gettext.prototype.getComment = function(domain, msgctxt, msgid) {
var translation;
translation = this._getTranslation(domain, msgctxt, msgid);
if (translation) {
return translation.comments || {};
}
return {};
};
/**
* Retrieves translation object from the domain and context
*
* @private
* @param {String} domain A gettext domain name
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {Object} Translation object or false if not found
*/
Gettext.prototype._getTranslation = function(domain, msgctxt, msgid) {
msgctxt = msgctxt || '';
return get(this.catalogs, [this.locale, domain, 'translations', msgctxt, msgid]);
};
/**
* Returns the language code part of a locale
*
* @example
* Gettext.getLanguageCode('sv-SE')
* // -> "sv"
*
* @private
* @param {String} locale A case-insensitive locale string
* @returns {String} A language code
*/
Gettext.getLanguageCode = function(locale) {
return locale.split(/[\-_]/)[0].toLowerCase();
};
/* C-style aliases */
/**
* C-style alias for [setTextDomain](#gettextsettextdomaindomain)
*
* @see Gettext#setTextDomain
*/
Gettext.prototype.textdomain = function(domain) {
if (this.debug) {
console.warn('textdomain(domain) was used to set locales in node-gettext v1. ' +
'Make sure you are using it for domains, and switch to setLocale(locale) if you are not.\n\n ' +
'To read more about the migration from node-gettext v1 to v2, ' +
'see https://github.com/alexanderwallin/node-gettext/#migrating-from-1x-to-2x\n\n' +
'This warning will be removed in the final 2.0.0');
}
this.setTextDomain(domain);
};
/**
* C-style alias for [setLocale](#gettextsetlocalelocale)
*
* @see Gettext#setLocale
*/
Gettext.prototype.setlocale = function(locale) {
this.setLocale(locale);
};
/* Deprecated functions */
/**
* This function will be removed in the final 2.0.0 release.
*
* @deprecated
*/
Gettext.prototype.addTextdomain = function() {
console.error('addTextdomain() is deprecated.\n\n' +
'* To add translations, use addTranslations()\n' +
'* To set the default domain, use setTextDomain() (or its alias textdomain())\n' +
'\n' +
'To read more about the migration from node-gettext v1 to v2, ' +
'see https://github.com/alexanderwallin/node-gettext/#migrating-from-1x-to-2x');
};