-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (73 loc) · 2.1 KB
/
index.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
const debug = require('debug')('intl-messages');
const Handlebars = require('handlebars');
const util = require('util'),
extend = require('extend');
const inspect = (obj => {
return util.inspect(obj, {
depth: null
});
});
class Messenger {
constructor(definition = {}, options = {}) {
this._definition = definition;
this.Handlebars = Handlebars.create();
this._locales = require('./locales');
this._options = options;
this.locale(options.locale || 'de-DE');
this.registerHelper(['hb_pluralize', 'hbDates', 'hbNumber', 'hbMapper']);
//replace placeholder $x in templates and compile
this._templates = {};
let repl = /\$(\d*)/g;
if (definition.hasOwnProperty('messages')) {
Object.keys(definition.messages).forEach((key) => {
debug('compiling message', key);
let templateString = definition.messages[key];
templateString = templateString.replace(repl, 'value.[$1]');
this._templates[key] = this.Handlebars.compile(templateString, {
locale: this._options.locale
});
});
}
}
locale(locale = null) {
if (locale) {
if (this._locales.hasOwnProperty(locale)) {
this._options.locale = locale;
this.settings = extend(true, {}, this._definition, this._locales[locale]);
} else {
throw (new Error('Locale ' + locale + ' not supported'));
}
}
return this._locale;
}
registerHelper(helperLibs) {
helperLibs.forEach(lib => {
let Helper = require('./lib/' + lib);
Helper.registerWith(this.Handlebars, this.settings);
});
}
render(txtId, ...data) {
let template = this._templates[txtId];
if (typeof template === 'undefined') {
throw (new Error('template ' + txtId + ' not defined'));
}
if (data.length === 1 && typeof data[0] === 'object') {
return template(data[0]);
} else {
return template({
value: data
});
}
}
template(txtId) {
let template = this._templates[txtId];
if (typeof template === 'undefined') {
throw (new Error('template ' + txtId + ' not defined'));
}
return tempalte;
}
compile(message) {
return this.Handlebars.compile(message);
}
}
module.exports = Messenger;