-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
293 lines (253 loc) · 8.18 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
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
/**
* @module i18n
* @since 1.0.0
*/
const get = require('lodash.get');
const paraphrase = require('paraphrase');
const assign = require('@recursive/assign');
const freeze = require('deep-freeze');
const _global = require('./utils/glob');
const getOneOther = require('./utils/get-one-other');
const jsonclone = require('./utils/jsonclone');
const TRANSLATIONS = typeof Symbol === 'function' ? Symbol() : '_translations';
const MISSING = typeof Symbol === 'function' ? Symbol() : '_missing';
const EMPTY = typeof Symbol === 'function' ? Symbol() : '_empty';
const EMPTY_VALUES = [null, ''];
const ACCEPTABLE_RETURN_TYPES = ['object', 'number', 'boolean', 'string'];
const interpolate = paraphrase(/\${([^{}]*)}/g, /%{([^{}]*)}/g, /{{([^{}]*)}}/g);
/**
* @class I18n
* @classdesc an object capable of translating keys and interpolate using given data object
* @param {Object} options.translations JSON compliant object
* @param {String} [options.$scope] Root string to be use for looking for translation keys
* @param {Function} [options.missing] Method to call when key is not found
* @param {Function} [options.empty] Method to call when value is empty
*/
class I18n {
constructor({ translations = {}, $scope, missing, empty } = { translations: {} }) {
this[TRANSLATIONS] = freeze(jsonclone(translations));
this[MISSING] = () => undefined;
this[EMPTY] = () => undefined;
this.onmiss(missing);
this.onempty(empty);
this.$scope = $scope;
this.translate = this.translate.bind(this);
}
/**
@static getDefault
* @param {String} key
* @return {String} A default string for a missing key
*/
static getDefault(key) {
return (key || '').split('.').pop().replace(/_/g, ' ');
}
/**
* translations
* @return {Object} instance translations object
*/
get translations() {
return this[TRANSLATIONS];
}
/**
* t alias to translate
* @return {Function}
*/
get t() {
return this.translate;
}
/**
* Add translation object(s)
* @param {Object(s)} translations [description]
* @return {self}
*/
add(...args) {
args.unshift({}, this.translations);
this[TRANSLATIONS] = freeze(assign(...args));
return this;
}
/**
* translate
* @param {String} key String representing dot notation
* @param {Object} [data] Interpolation data
* @return {String} translated and interpolated
*/
translate(key, data) {
const keys = Array.isArray(key) ? key : [ key ];
// Create key alternatives with prefixes
const alternatives = [].concat(
...keys.map(
(key) => this.alternatives(key, data)
)
);
// Find the first match
let result = this.find(...alternatives);
// Handle one,other translation structure
result = getOneOther(result, data);
if (EMPTY_VALUES.includes(result)) {
return this[EMPTY](
`${key}`, result, this.$scope, this.translations
) || I18n.getDefault(...keys);
}
const type = typeof result;
result = type === 'string' ? interpolate(result, data) : result;
return ACCEPTABLE_RETURN_TYPES.includes(type)
? result
: this[MISSING](
`${key}`, this.$scope, this.translations
) || I18n.getDefault(...keys);
}
/**
* Check if a translation key exists
* @param {string|string[]} keys
* @param {Object} data
* @return {Boolean} [description]
*/
has(keys, data) {
keys = Array.isArray(keys) ? keys : [ keys ];
const alternatives = keys.reduce(
(accumulator, key) => accumulator.concat(this.alternatives(key, data)),
[]
);
const result = this.find(...alternatives);
return typeof result !== 'undefined' && !EMPTY_VALUES.includes(result);
}
/**
* Create key alternatives with prefixes according to instance scopes
* @param {string} key
* @param {object} data Object optionally containing '$scope' parameter
* @return {string[]}
*/
alternatives(key, data) {
return [data || {}, this].map(
({ $scope }) => $scope
).filter(
Boolean
).map(
(scope) => [scope, key].join('.')
).concat(
key
);
}
/**
* find
* @param {...[String]} alternatives Different alternatives of strings to find
* @return {Any} Found match on translations object
*/
find(...alternatives) {
const key = alternatives.shift();
const result = get(this.translations, key);
const done = typeof result !== 'undefined' || alternatives.length === 0;
return done ? result : this.find(...alternatives);
}
/**
* Register callback to be called when a translation was missing.
* Function accepts arguments: {String} missing key
* {String} translation scope
* {Object} The entire translation dictionary
* @param {Function} callback
* @return {self}
*
* @example
* i18n.onmiss((key) => logMissingKeyEvent({key})
*/
onmiss(callback) {
if (typeof callback === 'function') {
this[MISSING] = callback;
}
return this;
}
/**
* Register callback to be called when a translation value is empty.
* Function accepts arguments: {String} missing key
* {String} translation scope
* {Object} The entire translation dictionary
* @param {Function} callback
* @return {self}
*
* @example
* i18n.onempty((key, value) => logEmptyValueEvent({key, value})
*/
onempty(callback) {
if (typeof callback === 'function') {
this[EMPTY] = callback;
}
return this;
}
/**
* Spawns a scoped child
* @param {String} scope Namespace
* @return {I18nChild} I18nChild instance
*/
spawn(scope) {
return new I18nChild(this, scope);
}
/**
* Make sure you only have one instance of I18n in your global scope
* @return {I18n} the same instance every time
*
* @example
* const i18n = I18n.singleton;
*/
static get singleton() {
if (_global.i18n) {
return _global.i18n;
}
const i18n = new I18n();
try {
Object.defineProperty(_global, 'i18n', {
value: i18n,
writable: false,
enumerable: false,
configurable: false
});
} catch (e) {
_global.i18n = i18n;
}
return i18n;
}
}
/**
* @class I18nChild
* @extends I18n
* @classdesc A child with the same capabilities and access but which translation keys may be namespcaed
* @param {String} [$scope]
*/
class I18nChild extends I18n {
constructor(parent, $scope) {
super();
const scopeChain = [];
parent.$scope && scopeChain.push(parent.$scope);
$scope && scopeChain.push($scope);
this.$scope = scopeChain.join('.') || undefined;
this.parent = parent;
this[MISSING] = this.parent[MISSING];
this[EMPTY] = this.parent[EMPTY];
}
/**
* translations
* @return {Object} parent's translations object
*/
get translations() {
return this.parent.translations;
}
/**
* Passes the translations to the parent's store under the namespace
* @param {...Object} args Translation objects
*/
add(...args) {
if (this.$scope) {
this.parent.add(...args.map((arg) => {
const base = {};
this.$scope.split('.').reduce((base, item, index, array) => {
base[item] = index === array.length - 1 ? arg : {};
return base[item];
}, base);
return base;
}));
} else {
this.parent.add(...args);
}
return this;
}
}
module.exports = I18n;