-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
539 lines (430 loc) · 13.2 KB
/
index.ts
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { ILokalesOptions, ILokalesCache, ILokalesUpdated, LokalesOptionKeys, IMap, ILokalesResult } from './interfaces';
import { parse, resolve, basename } from 'path';
import { readFileSync, writeFile, writeFileSync, stat, statSync, readdirSync, existsSync } from 'fs';
import { format } from 'util';
import * as mkdir from 'make-dir';
export * from './interfaces';
const DEFAULTS = {
directory: './locales', // directory where locales are stored.
locale: 'en', // the active locale.
localeFallback: 'en', // a fallback locale when active fails.
update: true, // when true allows updates to locale file.
onUpdate: undefined, // method on update called good for translating.
onError: undefined // called on write queue error.
};
export class Lokales {
// private _cache: any;
private _canExit = false;
private _onQueueEmpty: Function = () => { };
path: string; // the active locale path.
cache: ILokalesCache = {};
queue: any[] = [];
options: ILokalesOptions;
constructor(options?: ILokalesOptions) {
this.options = this.extend({}, DEFAULTS, options);
process.on('exit', this.onExit.bind(this, 'exit'));
}
// UTILS //
/**
* Exit handler ensures graceful exit writing any in queue.
*/
private onExit(type, err) {
if (this._canExit) return;
this._canExit = true;
// Remove event listeners.
process.removeListener('exit', this.onExit);
// If queue length and not already in
// exit loop call processQueue.
if (this.queue.length) {
this._canExit = false;
this.processQueue(type, err);
}
// Otherwise if an error was passed
// throw it otherwise exit.
if (this._canExit) {
this._onQueueEmpty();
}
}
/**
* Handles module errors.
*
* @param err the error to be handled.
*/
private error(err: string | Error) {
const errorHandler = this.options.onError;
if (errorHandler) {
err = !(err instanceof Error) ? new Error(err) : err;
errorHandler(err);
}
else {
console.error(err);
}
}
/**
* Is Value ensures the provided argument is not undefined, NaN, Infinity etc.
*
* @param val the value to inspect.
*/
private isValue(val: any) {
return (
(typeof val !== 'undefined') &&
(val !== null)
);
}
/**
* Is Plain Object checks if is plain object.
*
* @param val the value to inspect.
*/
private isPlainObject(val: any) {
return val && val.constructor && val.constructor === {}.constructor;
}
/**
* Minimalistc extend just suits purpose here.
*
* @param dest the destination object.
* @param src the source object.
*/
private extend(dest: any, ...args: any[]) {
return Object.assign(dest, ...args);
}
// FILE SYSTEM //
/**
* Resolve Locale resolves the locale or fallback path.
*
* @param locale the locale to be resovled.
* @param directory the directory where locales are stored.
*/
private resolveFile(locale: string, directory?: string, fallback?: string) {
fallback = fallback || this.options.localeFallback;
directory = directory || this.options.directory;
let path = resolve(directory, `${locale}.json`);
const parsed = parse(path);
mkdir.sync(parsed.dir); // ensure the directory exists.
if (fallback && !existsSync(path))
path = resolve(directory, `${fallback}.json`);
return path;
}
/**
* Resolve the path for a locale file.
*
* @param locale the locale to use for resolving path.
* @param directory an optional directory for resolving locale file.
*/
private resolvePath(locale?: string, directory?: string) {
directory = directory || this.options.directory;
locale = locale || this.options.locale;
return this.resolveFile(locale, directory);
}
// https://hangouts.google.com/hangouts/_/stackbuilders.com/dremachecarey?authuser=0
/**
* Reads the locale file.
*
* @param locale the active locale.
* @param directory the directory for locales.
* @param graceful mutes errors gracefully logs message.
*/
private readLocale(locale?: string, directory?: string, graceful?: boolean) {
const path = this.resolvePath(locale, directory);
this.path = path;
let obj: any = {};
try {
const str = readFileSync(path, 'utf-8');
if (!str)
return obj;
obj = JSON.parse(str);
}
catch (ex) {
obj = {}; // ensure object.
if (graceful) {
this.error(ex.message);
}
else {
// If bad format or no path ignore.
// create empty object and or path
// on next read. Otherwise throw err.
if (ex && (!(ex instanceof SyntaxError) && ex.code !== 'ENOENT'))
this.error(ex);
}
}
return obj;
}
// QUEUE //
private writeFile(path: string, data: string, updated?: IMap<any> | Function, fn?) {
if (typeof updated === 'function') {
fn = updated;
updated = undefined;
}
if (!path || !data)
return;
writeFile(path, data, 'utf-8', (err) => {
if (this.options.onUpdate)
this.options.onUpdate(err, <IMap<any>>updated, this);
if (err)
this.error(err.message);
this.queue.shift();
if (this.queue.length > 0)
this.processQueue();
// Call callback if exists.
if (fn) fn();
});
}
/**
* Adds event to write queue.
*
* @param state the current state of options object.
*/
private writeQueue(updated: ILokalesUpdated) {
this.queue.push(updated);
if (this.queue.length === 1)
this.processQueue();
}
/**
* Process queued jobs saving to file.
*/
private processQueue(type?: string, err?: any) {
if (!this.queue.length) {
this._canExit = true;
this.onExit(type, err);
return;
}
const updated = this.queue[0];
const opts: ILokalesOptions = updated.options;
const serialized = JSON.stringify(this.cache[opts.locale], null, 2);
const path = this.resolveFile(opts.locale, opts.directory, opts.localeFallback);
if (!serialized)
return;
this.writeFile(path, serialized, updated);
}
/**
* Template Literal allows for localizing __`some localized string ${value}`;
*
* @param strings array of template literal strings.
* @param values template literal args.
*/
private templateLiteral(strings: string[], values: any[]): string;
/**
* Template Literal allows for localizing __`some localized string ${value}`;
*
* @param strings array of template literal strings.
* @param values template literal args.
*/
private templateLiteral(strings: string[], values: any[], noFormat: boolean): ILokalesResult;
private templateLiteral(strings: string[], values: any[], noFormat?: boolean) {
let str = '';
strings.forEach((el, i) => {
const arg = values[i];
str += el;
if (typeof arg !== 'undefined') {
str += '%s';
}
});
if (noFormat)
return this.__x(str, ...values);
return this.__(str, ...values);
}
// GETTERS //
get t() {
return this.__;
}
get tn() {
return this.__n;
}
get tx() {
return this.__x;
}
get tnx() {
return this.__nx;
}
// LOCALIZATION //
/**
* Localize common method for localizing strings.
*
* @param singular singular string value.
* @param plural plural string value or count.
* @param count numeric count for pluralization.
* @param args format args.
*/
protected localize(singular: string, plural: string, count: number, ...args: any[]): ILokalesResult {
const cache = this.cache;
const locale = this.options.locale;
const isPlural = count > 1 ? true : false;
const supportsPlural = this.isValue(plural);
let shouldQueue;
if (!cache[locale]) { // ensure loaded locale.
cache[locale] = this.readLocale();
}
const existing = cache[locale][singular]; // value already exists.
if (!existing && this.options.update) {
if (!supportsPlural) { // singular localization.
cache[locale][singular] = singular;
}
else {
cache[locale][singular] = { // plural localization.
one: singular,
other: plural
};
}
shouldQueue = true;
}
if (existing && supportsPlural && // ensure key is object.
this.options.update &&
!this.isPlainObject(cache[locale][singular])) {
cache[locale][singular] = {
one: singular,
other: plural
};
shouldQueue = true;
}
let val = cache[locale][singular]; // default singular value.
if (supportsPlural) { // supports plural
if (isPlural)
val = cache[locale][singular].other; // get plural value.
else
val = cache[locale][singular].one; // get singular.
}
if (shouldQueue) // add write to queue to reflect changes.
this.writeQueue({
singular: singular,
plural: plural,
count: count,
args: args,
options: this.options
});
if (~val.indexOf('%d'))
args.push(count);
return {
val: val,
args: args
};
}
// API METHODS //
/**
* A callback function before exit and after queue has been emptied.
*
* @param fn a callback function on queue empty and ready to exit.
*/
onQueueEmpty(fn: Function) {
this._onQueueEmpty = fn;
}
/**
* Set an option or extends current options.
*
* @param key the key or options object to set.
* @param val the value to set when key is not an object.
*/
setOption(key: LokalesOptionKeys | ILokalesOptions, val: any) {
const isObj = this.isPlainObject(key);
let obj: any = key;
if (!isObj && !this.isValue(val))
return;
if (!isObj) {
obj = {};
obj[<string>key] = val;
}
this.extend(this.options, obj);
return this;
}
/**
* Get an option value by key.
*
* @param key the option key to get.
*/
getOption(key: string) {
return this.options[key] || null;
}
/**
* Key Exists inspects cached checking if key already exists.
*
* @param key the key to check if exists.
* @param locale the locale to inspect for key.
* @param directory optional directory.
*/
keyExists(key: string, locale?: string, directory?: string) {
locale = locale || this.options.locale;
if (!this.cache[locale]) // ensure loaded locale.
this.cache[locale] = this.readLocale(locale, directory);
return this.cache[locale][key];
}
/**
* Sync ensures secondary locales contain same keys of primary from.
*
* @param from the locale to sync from default "en".
*/
sync(from: string = 'en') {
from = from.replace(/\.json$/, '.json').toLowerCase();
const stats = statSync(this.options.directory);
const files = readdirSync(this.options.directory, 'utf-8');
const fromLocale = this.readLocale(from);
let ctr = 0;
files.forEach((f, i) => {
const filename = f.toString().toLowerCase();
const locale = basename(filename).replace(/\.json$/, '');
if (locale !== from) {
// Load the current found locale.
const currLocale = this.readLocale(locale, null, true);
for (const k in fromLocale) {
if (!currLocale[k])
currLocale[k] = fromLocale[k];
}
// Write the file.
writeFileSync(resolve(this.options.directory, `${locale}.json`), JSON.stringify(currLocale, null, 2));
console.error(`Synchronized locales: ${from} >> ${locale}`);
}
});
}
/**
* Localize non plurals template strings.
*
* @param val the value to localize.
* @param args format arguments.
*/
__(val: TemplateStringsArray, ...args: any[]): string;
/**
* Localize non plural string.
*
* @param val the value to localize.
* @param args format arguments.
*/
__(val: string, ...args: any[]): string;
__(val: string | TemplateStringsArray, ...args: any[]): string {
if (Array.isArray(val)) // is template literal.
return this.templateLiteral(val, args);
const result = this.localize(<string>val, null, null, ...args);
return format(result.val, ...result.args);
}
/**
* Localize non plural string unformated as object.
*
* @param val the value to localize.
* @param args format arguments.
*/
__x(val: string | TemplateStringsArray, ...args: any[]): ILokalesResult {
if (Array.isArray(val)) // is template literal.
return this.templateLiteral(val, args, true);
return this.localize(<string>val, null, null, ...args);
}
/**
* Localize plurals.
*
* @param singular the singular localized value.
* @param plural the pluralized valued.
* @param count the count for the plural value.
* @param args argument formatters.
*/
__n(singular: string, plural: string, count?: number, ...args: any[]): string {
const result = this.localize(singular, plural, count, ...args);
return format(result.val, ...result.args);
}
/**
* Localize plurals unformated as object.
*
* @param singular the singular localized value.
* @param plural the pluralized valued.
* @param count the count for the plural value.
* @param args argument formatters.
*/
__nx(singular: string, plural: string, count?: number, ...args: any[]): ILokalesResult {
return this.localize(singular, plural, count, ...args);
}
}