handlebars-i18n 1.6.3 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +15 -15
- package/LICENSE +2 -1
- package/dist/handlebars-i18n.d.ts +187 -3
- package/dist/handlebars-i18n.js +229 -88
- package/dist/handlebars-i18n.min.js +1 -1
- package/examples/browser-example/index.html +109 -86
- package/examples/node-example/simple-example.js +89 -35
- package/examples/typescript/index.ts +11 -8
- package/examples/typescript/test.hbs +25 -7
- package/package.json +11 -8
- package/readme.md +150 -45
- package/test/handlebars-i18n.test.js +467 -53
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* Tests for handlebars-i18n.js
|
|
3
3
|
*
|
|
4
4
|
* usage:
|
|
5
|
-
* $ cd test
|
|
6
5
|
* $ npm run test
|
|
7
6
|
*/
|
|
8
7
|
|
|
@@ -12,78 +11,93 @@ const expect = require('chai').expect;
|
|
|
12
11
|
const Handlebars = require('handlebars');
|
|
13
12
|
const i18next = require('i18next');
|
|
14
13
|
const HandlebarsI18n = require('../dist/handlebars-i18n');
|
|
15
|
-
//const HandlebarsModified = require("handlebars");
|
|
16
14
|
|
|
17
|
-
describe('handlebars-i18n
|
|
15
|
+
describe('handlebars-i18n Tests', function() {
|
|
18
16
|
|
|
19
17
|
const i18nInitObj = {
|
|
20
|
-
resources
|
|
21
|
-
'en'
|
|
22
|
-
translation
|
|
18
|
+
resources: {
|
|
19
|
+
'en': {
|
|
20
|
+
translation: {
|
|
23
21
|
'key1': 'What is good?',
|
|
24
22
|
'key2': '{{what}} is {{adverb}}.'
|
|
25
23
|
}
|
|
26
24
|
},
|
|
27
|
-
'de'
|
|
25
|
+
'de': {
|
|
28
26
|
translation: {
|
|
29
27
|
'key1': 'Was ist gut?',
|
|
30
28
|
'key2': '{{what}} ist {{adverb}}.'
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
},
|
|
34
|
-
lng
|
|
32
|
+
lng: 'en'
|
|
35
33
|
};
|
|
36
34
|
|
|
37
35
|
const hI18n = HandlebarsI18n.init();
|
|
38
36
|
|
|
39
|
-
// -- Tests for method init() -- //
|
|
40
37
|
|
|
41
|
-
|
|
38
|
+
/****************************************
|
|
39
|
+
Tests against method init()
|
|
40
|
+
****************************************/
|
|
41
|
+
|
|
42
|
+
it('after method call init() should return an object (HandlebarsEnvironment)', function () {
|
|
42
43
|
assert.isObject(hI18n);
|
|
43
44
|
});
|
|
44
45
|
|
|
45
|
-
it('after method call init() HandlebarsEnvironment object should have a function __', function() {
|
|
46
|
+
it('after method call init() HandlebarsEnvironment object should have a function __', function () {
|
|
46
47
|
assert.isFunction(hI18n.helpers.__);
|
|
47
48
|
});
|
|
48
49
|
|
|
49
|
-
it('after method call init() HandlebarsEnvironment object should have a function _locale', function() {
|
|
50
|
+
it('after method call init() HandlebarsEnvironment object should have a function _locale', function () {
|
|
50
51
|
assert.isFunction(hI18n.helpers._locale);
|
|
51
52
|
});
|
|
52
53
|
|
|
53
|
-
it('after method call init() HandlebarsEnvironment object should have a function localeIs', function() {
|
|
54
|
+
it('after method call init() HandlebarsEnvironment object should have a function localeIs', function () {
|
|
54
55
|
assert.isFunction(hI18n.helpers.localeIs);
|
|
55
56
|
});
|
|
56
57
|
|
|
57
|
-
it('after method call init() HandlebarsEnvironment object should have a function _date', function() {
|
|
58
|
+
it('after method call init() HandlebarsEnvironment object should have a function _date', function () {
|
|
58
59
|
assert.isFunction(hI18n.helpers._date);
|
|
59
60
|
});
|
|
60
61
|
|
|
61
|
-
it('after method call init() HandlebarsEnvironment object should have a function
|
|
62
|
+
it('after method call init() HandlebarsEnvironment object should have a function _dateRel', function () {
|
|
63
|
+
assert.isFunction(hI18n.helpers._dateRel);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('after method call init() HandlebarsEnvironment object should have a function _dateDiff', function () {
|
|
67
|
+
assert.isFunction(hI18n.helpers._dateDiff);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('after method call init() HandlebarsEnvironment object should have a function _num', function () {
|
|
62
71
|
assert.isFunction(hI18n.helpers._num);
|
|
63
72
|
});
|
|
64
73
|
|
|
65
|
-
it('after method call init() HandlebarsEnvironment object should have a function _price', function() {
|
|
74
|
+
it('after method call init() HandlebarsEnvironment object should have a function _price', function () {
|
|
66
75
|
assert.isFunction(hI18n.helpers._price);
|
|
67
76
|
});
|
|
68
77
|
|
|
69
78
|
// -- Tests for method init() with override Argument -- //
|
|
70
79
|
|
|
71
|
-
it('after method call init(overrideHndlbrs) with custom handlebars Object, HandlebarsEnvironment object should have custom function foo', function() {
|
|
80
|
+
it('after method call init(overrideHndlbrs) with custom handlebars Object, HandlebarsEnvironment object should have custom function foo', function () {
|
|
72
81
|
const HandlebarsModified = require('handlebars');
|
|
73
|
-
HandlebarsModified.registerHelper('foo', function() {
|
|
82
|
+
HandlebarsModified.registerHelper('foo', function () {
|
|
83
|
+
return true
|
|
84
|
+
});
|
|
74
85
|
const hI18nMod = HandlebarsI18n.init(HandlebarsModified);
|
|
75
86
|
assert.isFunction(hI18nMod.helpers.foo);
|
|
76
87
|
});
|
|
77
88
|
|
|
78
|
-
it('after method call init(null, overrideI18n) with custom i18n Object, i18n object should have custom function foo', function() {
|
|
89
|
+
it('after method call init(null, overrideI18n) with custom i18n Object, i18n object should have custom function foo', function () {
|
|
79
90
|
const i18nModified = require('i18next');
|
|
80
|
-
i18nModified.init({supportedLngs: ['de','en']});
|
|
91
|
+
i18nModified.init({supportedLngs: ['de', 'en']});
|
|
81
92
|
const hI18nMod = HandlebarsI18n.init(null, i18nModified);
|
|
82
|
-
assert.isFunction(function(){
|
|
93
|
+
assert.isFunction(function () {
|
|
94
|
+
}); // write a test here
|
|
83
95
|
});
|
|
84
96
|
|
|
85
97
|
|
|
86
|
-
|
|
98
|
+
/****************************************
|
|
99
|
+
Tests against function _locale
|
|
100
|
+
****************************************/
|
|
87
101
|
|
|
88
102
|
it('expecting function _locale to be [undefined] as long as no language was set with i18next.init', function() {
|
|
89
103
|
i18next.init(); // empty init
|
|
@@ -104,7 +118,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
104
118
|
});
|
|
105
119
|
|
|
106
120
|
|
|
107
|
-
|
|
121
|
+
/****************************************
|
|
122
|
+
Tests against function isLocale
|
|
123
|
+
****************************************/
|
|
108
124
|
|
|
109
125
|
it('function isLocale should return TRUE when current language is set to "en" and given "en" as parameter', function() {
|
|
110
126
|
i18next.changeLanguage('en');
|
|
@@ -119,9 +135,11 @@ describe('handlebars-i18n Test', function() {
|
|
|
119
135
|
});
|
|
120
136
|
|
|
121
137
|
|
|
122
|
-
|
|
138
|
+
/****************************************
|
|
139
|
+
Tests against function __
|
|
140
|
+
****************************************/
|
|
123
141
|
|
|
124
|
-
it('
|
|
142
|
+
it('expect __ to throw error when called with no parameter', function() {
|
|
125
143
|
expect(function() { hI18n.helpers.__() }).to.throw();
|
|
126
144
|
});
|
|
127
145
|
|
|
@@ -154,13 +172,15 @@ describe('handlebars-i18n Test', function() {
|
|
|
154
172
|
});
|
|
155
173
|
|
|
156
174
|
|
|
157
|
-
|
|
175
|
+
/****************************************
|
|
176
|
+
Tests against function _date
|
|
177
|
+
****************************************/
|
|
158
178
|
|
|
159
179
|
it('expect function _date to throw error when called with invalid date parameter', function() {
|
|
160
180
|
expect(function() { hI18n.helpers._date('someStrangeString') }).to.throw("Invalid valid date passed to format");
|
|
161
181
|
});
|
|
162
182
|
|
|
163
|
-
it('function _date should return today
|
|
183
|
+
it('function _date should return today’s date in Intl default format when called without parameter', function() {
|
|
164
184
|
i18next.changeLanguage('en');
|
|
165
185
|
const today = new Date();
|
|
166
186
|
const todayFormated = new Intl.DateTimeFormat().format(today);
|
|
@@ -169,17 +189,18 @@ describe('handlebars-i18n Test', function() {
|
|
|
169
189
|
assert.equal(todayFormated, res);
|
|
170
190
|
});
|
|
171
191
|
|
|
172
|
-
it('function _date should return today
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
192
|
+
it('function _date should return today’s date in Intl default format when called with parameter, "Today" or "Now" no matter of upper or lower case writing',
|
|
193
|
+
function() {
|
|
194
|
+
i18next.changeLanguage('en');
|
|
195
|
+
const today = new Date();
|
|
196
|
+
const todayFormated = new Intl.DateTimeFormat().format(today);
|
|
176
197
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
198
|
+
assert.equal(todayFormated, hI18n.helpers._date("today"));
|
|
199
|
+
assert.equal(todayFormated, hI18n.helpers._date("Today"));
|
|
200
|
+
assert.equal(todayFormated, hI18n.helpers._date("TODAY"));
|
|
201
|
+
assert.equal(todayFormated, hI18n.helpers._date("now"));
|
|
202
|
+
assert.equal(todayFormated, hI18n.helpers._date("Now"));
|
|
203
|
+
assert.equal(todayFormated, hI18n.helpers._date("NOW"));
|
|
183
204
|
});
|
|
184
205
|
|
|
185
206
|
it('function _date should return "1/1/1970" (Intl default format) when called with parameter 1 as number ', function() {
|
|
@@ -231,7 +252,216 @@ describe('handlebars-i18n Test', function() {
|
|
|
231
252
|
});
|
|
232
253
|
|
|
233
254
|
|
|
234
|
-
|
|
255
|
+
/****************************************
|
|
256
|
+
Tests against function _dateRel
|
|
257
|
+
****************************************/
|
|
258
|
+
|
|
259
|
+
it('expect function _dateRel to throw error when called without parameter', function() {
|
|
260
|
+
expect(function() { hI18n.helpers._dateRel() }).to.throw('Invalid "number" argument: NaN');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('expect function _dateRel to throw error when called with invalid date parameter', function() {
|
|
264
|
+
expect(function() { hI18n.helpers._dateRel('someStrangeString') }).to.throw('Invalid "number" argument: NaN');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('expect function _dateRel to throw error when called with non-existent language shortcode', function() {
|
|
268
|
+
i18next.changeLanguage('invalid');
|
|
269
|
+
expect(function() {
|
|
270
|
+
hI18n.helpers._dateRel(1, { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit:"day" }})
|
|
271
|
+
}).to.throw('No locale data passed');
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('expect function _dateRel to return \'in 1 hour\' when called with \'en\' and first parameter being 1', function() {
|
|
275
|
+
i18next.changeLanguage('en');
|
|
276
|
+
const res = hI18n.helpers._dateRel(1);
|
|
277
|
+
assert.equal('in 1 hour', res);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('expect function _dateRel to return \'1 hour ago\' when called with \'en\' and first parameter being -1', function() {
|
|
281
|
+
i18next.changeLanguage('en');
|
|
282
|
+
const res = hI18n.helpers._dateRel(-1);
|
|
283
|
+
assert.equal('1 hour ago', res);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('expect function _dateRel to return \'in 1 second\' when called with \'en\' and first parameter being 1 and according options', function() {
|
|
287
|
+
i18next.changeLanguage('en');
|
|
288
|
+
const res = hI18n.helpers._dateRel(1, { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit:"seconds" }});
|
|
289
|
+
assert.equal('in 1 second', res);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('expect function _dateRel to return \'in 1 Tag\' when called with \'de\' and paramter 1 and according options', function() {
|
|
293
|
+
i18next.changeLanguage('de');
|
|
294
|
+
const res = hI18n.helpers._dateRel(1, { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit:"day" }});
|
|
295
|
+
assert.equal('in 1 Tag', res);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
/****************************************
|
|
300
|
+
Tests against function _dateDiff
|
|
301
|
+
****************************************/
|
|
302
|
+
|
|
303
|
+
it('function _dateDiff should return null when called with no parameter', function() {
|
|
304
|
+
i18next.changeLanguage('en');
|
|
305
|
+
const res = hI18n.helpers._dateDiff();
|
|
306
|
+
assert.equal(null, res);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('expect function _dateDiff to throw error when called with invalid 1. date parameter', function() {
|
|
310
|
+
expect(function() { hI18n.helpers._dateDiff('someStrangeString', '1995-12-17T03:24:00') })
|
|
311
|
+
.to.throw('Invalid "number" argument: NaN');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('expect function _dateDiff to throw error when called with invalid 2. date parameter', function() {
|
|
315
|
+
expect(function() { hI18n.helpers._dateDiff('1995-12-17T03:24:00', 'someStrangeString') })
|
|
316
|
+
.to.throw('Invalid "number" argument: NaN');
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
/*it('expect function _dateDiff to return the first date as "in 227,543 hours", when no second param given', function() {
|
|
320
|
+
i18next.changeLanguage('en');
|
|
321
|
+
const res = hI18n.helpers._dateDiff('1995-12-17T00:00:00');
|
|
322
|
+
assert.equal('in 227,543 hours', res);
|
|
323
|
+
});*/
|
|
324
|
+
|
|
325
|
+
it('expect function _dateDiff to return the second date as "in 227,543 hours", when first param is empty', function() {
|
|
326
|
+
i18next.changeLanguage('en');
|
|
327
|
+
const res = hI18n.helpers._dateDiff('', '1995-12-17T00:00:00');
|
|
328
|
+
assert.equal('in 227,543 hours', res);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('expect function _dateDiff to return "in 0 hours", when dates are identical', function() {
|
|
332
|
+
i18next.changeLanguage('en');
|
|
333
|
+
const res = hI18n.helpers._dateDiff('1995-12-17T00:00:00', '1995-12-17T00:00:00');
|
|
334
|
+
assert.equal('in 0 hours', res);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// -- Test year -- //
|
|
338
|
+
|
|
339
|
+
it('expect function _dateDiff to return "in 1 year"', function() {
|
|
340
|
+
i18next.changeLanguage('en');
|
|
341
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: "year" } };
|
|
342
|
+
const res = hI18n.helpers._dateDiff('1996-12-17T00:00:00', '1995-12-17T00:00:00', hash);
|
|
343
|
+
assert.equal('in 1 year', res);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it('expect function _dateDiff to return "1 year ago"', function() {
|
|
347
|
+
i18next.changeLanguage('en');
|
|
348
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: "year" } };
|
|
349
|
+
const res = hI18n.helpers._dateDiff('1995-12-17T00:00:00', '1996-12-17T00:00:00', hash);
|
|
350
|
+
assert.equal('1 year ago', res);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// -- Test quarter -- //
|
|
354
|
+
|
|
355
|
+
it('expect function _dateDiff to return "in 1 quarter"', function() {
|
|
356
|
+
i18next.changeLanguage('en');
|
|
357
|
+
const unit = 'quarter';
|
|
358
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
359
|
+
const res = hI18n.helpers._dateDiff('1996-12-17T00:00:00', '1996-09-16T00:00:00', hash);
|
|
360
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('expect function _dateDiff to return "1 quarter ago"', function() {
|
|
364
|
+
i18next.changeLanguage('en');
|
|
365
|
+
const unit = 'quarter';
|
|
366
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
367
|
+
const res = hI18n.helpers._dateDiff('1996-09-16T00:00:00', '1996-12-17T00:00:00', hash);
|
|
368
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// -- Test month -- //
|
|
372
|
+
|
|
373
|
+
it('expect function _dateDiff to return "in 1 month"', function() {
|
|
374
|
+
i18next.changeLanguage('en');
|
|
375
|
+
const unit = 'month';
|
|
376
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
377
|
+
const res = hI18n.helpers._dateDiff('1996-12-17T00:00:00', '1996-11-16T00:00:00', hash);
|
|
378
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('expect function _dateDiff to return "1 month ago"', function() {
|
|
382
|
+
i18next.changeLanguage('en');
|
|
383
|
+
const unit = 'month';
|
|
384
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
385
|
+
const res = hI18n.helpers._dateDiff('1996-11-16T00:00:00', '1996-12-17T00:00:00', hash);
|
|
386
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
// -- Test week -- //
|
|
390
|
+
|
|
391
|
+
it('expect function _dateDiff to return "in 1 week"', function() {
|
|
392
|
+
i18next.changeLanguage('en');
|
|
393
|
+
const unit = 'week';
|
|
394
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
395
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:00:00', '1996-12-01T00:00:00', hash);
|
|
396
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('expect function _dateDiff to return "1 week ago"', function() {
|
|
400
|
+
i18next.changeLanguage('en');
|
|
401
|
+
const unit = 'week';
|
|
402
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
403
|
+
const res = hI18n.helpers._dateDiff('1996-12-01T00:00:00', '1996-12-08T00:00:00', hash);
|
|
404
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// -- Test day -- //
|
|
408
|
+
|
|
409
|
+
it('expect function _dateDiff to return "in 1 day"', function() {
|
|
410
|
+
i18next.changeLanguage('en');
|
|
411
|
+
const unit = 'day';
|
|
412
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
413
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:00:00', '1996-12-07T00:00:00', hash);
|
|
414
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it('expect function _dateDiff to return "1 day ago"', function() {
|
|
418
|
+
i18next.changeLanguage('en');
|
|
419
|
+
const unit = 'day';
|
|
420
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
421
|
+
const res = hI18n.helpers._dateDiff('1996-12-07T00:00:00', '1996-12-08T00:00:00', hash);
|
|
422
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
// -- Test minute -- //
|
|
426
|
+
|
|
427
|
+
it('expect function _dateDiff to return "in 1 minute"', function() {
|
|
428
|
+
i18next.changeLanguage('en');
|
|
429
|
+
const unit = 'minute';
|
|
430
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
431
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:01:00', '1996-12-08T00:00:00', hash);
|
|
432
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('expect function _dateDiff to return "1 minute ago"', function() {
|
|
436
|
+
i18next.changeLanguage('en');
|
|
437
|
+
const unit = 'minute';
|
|
438
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
439
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:00:00', '1996-12-08T00:01:00', hash);
|
|
440
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// -- Test second -- //
|
|
444
|
+
|
|
445
|
+
it('expect function _dateDiff to return "in 1 second"', function() {
|
|
446
|
+
i18next.changeLanguage('en');
|
|
447
|
+
const unit = 'second';
|
|
448
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
449
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:00:01', '1996-12-08T00:00:00', hash);
|
|
450
|
+
assert.equal(`in 1 ${unit}`, res);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it('expect function _dateDiff to return "1 second ago"', function() {
|
|
454
|
+
i18next.changeLanguage('en');
|
|
455
|
+
const unit = 'second';
|
|
456
|
+
const hash = { hash: { localeMatcher: "best fit", numeric: "always", style: "long", unit: unit } };
|
|
457
|
+
const res = hI18n.helpers._dateDiff('1996-12-08T00:00:00', '1996-12-08T00:00:01', hash);
|
|
458
|
+
assert.equal(`1 ${unit} ago`, res);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
/****************************************
|
|
463
|
+
Tests against function _num
|
|
464
|
+
****************************************/
|
|
235
465
|
|
|
236
466
|
it('function _num should return comma separated triples of decimals when language is "en"', function() {
|
|
237
467
|
i18next.changeLanguage('en');
|
|
@@ -258,7 +488,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
258
488
|
});
|
|
259
489
|
|
|
260
490
|
|
|
261
|
-
|
|
491
|
+
/****************************************
|
|
492
|
+
Tests against function _price
|
|
493
|
+
****************************************/
|
|
262
494
|
|
|
263
495
|
it('function _currency should return price in € written in comma separated triples of decimals and 2 fraction digits with leading currency symbol', function() {
|
|
264
496
|
i18next.changeLanguage('en');
|
|
@@ -286,7 +518,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
286
518
|
});
|
|
287
519
|
|
|
288
520
|
|
|
289
|
-
|
|
521
|
+
/****************************************
|
|
522
|
+
Tests against method configure()
|
|
523
|
+
****************************************/
|
|
290
524
|
|
|
291
525
|
it('method configure() should return false if called without argument', function() {
|
|
292
526
|
const configure = HandlebarsI18n.configure();
|
|
@@ -333,15 +567,22 @@ describe('handlebars-i18n Test', function() {
|
|
|
333
567
|
assert.isNotOk(configure);
|
|
334
568
|
});
|
|
335
569
|
|
|
570
|
+
it('method configure() should return true if called with arguments "en", "RelativeTimeFormat", { localeMatcher: "best fit", numeric: "always", style: "long" }', function() {
|
|
571
|
+
const configure = HandlebarsI18n.configure('en', 'RelativeTimeFormat', { localeMatcher: "best fit", numeric: "always", style: "long" } );
|
|
572
|
+
assert.isOk(configure);
|
|
573
|
+
});
|
|
574
|
+
|
|
336
575
|
|
|
337
|
-
|
|
576
|
+
/****************************************
|
|
577
|
+
Tests against method reset()
|
|
578
|
+
****************************************/
|
|
338
579
|
|
|
339
580
|
it('method reset() should return TRUE if called', function() {
|
|
340
581
|
const res = HandlebarsI18n.reset();
|
|
341
582
|
assert.isOk(res);
|
|
342
583
|
});
|
|
343
584
|
|
|
344
|
-
it('function _num should return Intl standard format (no fraction digits) after reset()
|
|
585
|
+
it('function _num should return Intl standard format (no fraction digits) after reset() being called', function() {
|
|
345
586
|
HandlebarsI18n.configure('en', 'NumberFormat', { minimumFractionDigits:4 } );
|
|
346
587
|
i18next.changeLanguage('en');
|
|
347
588
|
HandlebarsI18n.reset();
|
|
@@ -350,7 +591,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
350
591
|
});
|
|
351
592
|
|
|
352
593
|
|
|
353
|
-
|
|
594
|
+
/********************************************************************
|
|
595
|
+
Tests for custom format configurations for function _date
|
|
596
|
+
********************************************************************/
|
|
354
597
|
|
|
355
598
|
it('function _date when called after configure() with defined custom format (year:2-digit) should return ' +
|
|
356
599
|
'date "95" when language is "en"', function() {
|
|
@@ -360,9 +603,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
360
603
|
assert.equal('95', res);
|
|
361
604
|
});
|
|
362
605
|
|
|
363
|
-
it('function _date when called after configure() with defined custom format (year:
|
|
606
|
+
it('function _date when called after configure() with defined custom format (year:numeric) given as ARRAY should return ' +
|
|
364
607
|
'date "12/17/95" when language is "en"', function() {
|
|
365
|
-
HandlebarsI18n.configure(['en', 'DateTimeFormat', { year:"
|
|
608
|
+
HandlebarsI18n.configure(['en', 'DateTimeFormat', { year:"numeric" }, 'my-custom-format']);
|
|
366
609
|
i18next.changeLanguage('en');
|
|
367
610
|
const res = hI18n.helpers._date('December 17, 1995 03:24:00', { hash: { format: 'my-custom-format'} });
|
|
368
611
|
assert.equal('95', res);
|
|
@@ -375,12 +618,12 @@ describe('handlebars-i18n Test', function() {
|
|
|
375
618
|
['en', 'DateTimeFormat', { year:"2-digit" }, 'my-custom-format']
|
|
376
619
|
]);
|
|
377
620
|
i18next.changeLanguage('en');
|
|
378
|
-
const res = hI18n.helpers._date('
|
|
621
|
+
const res = hI18n.helpers._date('[1995,11,17]', { hash: { format: 'my-custom-format'} });
|
|
379
622
|
assert.equal('95', res);
|
|
380
623
|
});
|
|
381
624
|
|
|
382
625
|
it('function _date when called after configure() with defined custom format (year:2-digit) should override ' +
|
|
383
|
-
'standard configuration also when
|
|
626
|
+
'standard configuration also when being defined first', function() {
|
|
384
627
|
HandlebarsI18n.configure([
|
|
385
628
|
['en', 'DateTimeFormat', { year:"2-digit" }, 'my-custom-format'],
|
|
386
629
|
['en', 'DateTimeFormat', { year:"numeric" }]
|
|
@@ -391,7 +634,7 @@ describe('handlebars-i18n Test', function() {
|
|
|
391
634
|
});
|
|
392
635
|
|
|
393
636
|
it('function _date when called after configure() should fall back to generic language format "en" when custom format is unknown' +
|
|
394
|
-
'standard configuration also when
|
|
637
|
+
'standard configuration also when being defined first', function() {
|
|
395
638
|
HandlebarsI18n.configure([
|
|
396
639
|
['en', 'DateTimeFormat', { year:"2-digit" }, 'my-custom-format'],
|
|
397
640
|
['en', 'DateTimeFormat', { year:"numeric" }]
|
|
@@ -402,7 +645,7 @@ describe('handlebars-i18n Test', function() {
|
|
|
402
645
|
});
|
|
403
646
|
|
|
404
647
|
it('function _date when called after configure() should fall back to generic language format "all" when custom format is unknown' +
|
|
405
|
-
'standard configuration also when
|
|
648
|
+
'standard configuration also when being defined first', function() {
|
|
406
649
|
HandlebarsI18n.configure([
|
|
407
650
|
['all', 'DateTimeFormat', { year:"2-digit" }, 'my-custom-format'],
|
|
408
651
|
['en', 'DateTimeFormat', { year:"numeric" }]
|
|
@@ -413,7 +656,7 @@ describe('handlebars-i18n Test', function() {
|
|
|
413
656
|
});
|
|
414
657
|
|
|
415
658
|
it('function _date when called after configure() should fall back to Intl default format when custom format is unknown' +
|
|
416
|
-
'standard configuration also when
|
|
659
|
+
'standard configuration also when being defined first', function() {
|
|
417
660
|
HandlebarsI18n.reset();
|
|
418
661
|
HandlebarsI18n.configure([
|
|
419
662
|
['en', 'DateTimeFormat', { year:"2-digit" }, 'my-custom-format']
|
|
@@ -422,9 +665,65 @@ describe('handlebars-i18n Test', function() {
|
|
|
422
665
|
const res = hI18n.helpers._date('December 17, 1995 03:24:00', { hash: { format: 'my-unknown-format'} });
|
|
423
666
|
assert.equal('12/17/1995', res);
|
|
424
667
|
});
|
|
425
|
-
|
|
426
668
|
|
|
427
|
-
|
|
669
|
+
|
|
670
|
+
/********************************************************************
|
|
671
|
+
Tests for custom format configurations for _dateRel / _dateDiff
|
|
672
|
+
********************************************************************/
|
|
673
|
+
|
|
674
|
+
it('function _dateRel called after configure() with defined "all" (style: "long", unit: "second") should return ' +
|
|
675
|
+
'"in 12 seconds" when language is "en"', function() {
|
|
676
|
+
HandlebarsI18n.configure('all', 'RelativeTimeFormat', { style: "long", unit: "second" });
|
|
677
|
+
i18next.changeLanguage('en');
|
|
678
|
+
const res = hI18n.helpers._dateRel('12');
|
|
679
|
+
assert.equal('in 12 seconds', res);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('function _dateRel called after configure() with defined custom format (style: "long", unit: "year") should return ' +
|
|
683
|
+
'"in 12 Jahren" when language is "de"', function() {
|
|
684
|
+
HandlebarsI18n.configure('de', 'RelativeTimeFormat', { style: "long", unit: "year" }, 'date-rel-custom');
|
|
685
|
+
i18next.changeLanguage('de');
|
|
686
|
+
const res = hI18n.helpers._dateRel('12', { hash: { format: 'date-rel-custom'} });
|
|
687
|
+
assert.equal('in 12 Jahren', res);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it('function _dateRel called after configure() with defined custom format { style: "short", unit: "minutes" } should override ' +
|
|
691
|
+
'standard configuration when language is "en"', function() {
|
|
692
|
+
HandlebarsI18n.configure([
|
|
693
|
+
['en', 'RelativeTimeFormat', { style: "short", unit: "day" }, 'date-rel-spec'],
|
|
694
|
+
['en', 'RelativeTimeFormat', { style: "long", unit: "second" }]
|
|
695
|
+
]);
|
|
696
|
+
i18next.changeLanguage('en');
|
|
697
|
+
const res = hI18n.helpers._dateRel('12', { hash: { format: 'date-rel-spec'} });
|
|
698
|
+
assert.equal('in 12 days', res);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it('function _dateRel called after configure() with defined custom format { style: "short", unit: "minutes" } should override ' +
|
|
702
|
+
'standard configuration when language is "en" and output: \'in 12 days\'', function() {
|
|
703
|
+
HandlebarsI18n.configure([
|
|
704
|
+
['en', 'RelativeTimeFormat', { style: "short", unit: "day" }, 'date-rel-spec'],
|
|
705
|
+
['en', 'RelativeTimeFormat', { style: "long", unit: "second" }]
|
|
706
|
+
]);
|
|
707
|
+
i18next.changeLanguage('en');
|
|
708
|
+
const res = hI18n.helpers._dateRel('12', { hash: { format: 'date-rel-spec'} });
|
|
709
|
+
assert.equal('in 12 days', res);
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it('function _dateDiff called after configure() with defined custom format { style: "short", unit: "minutes" } should override ' +
|
|
713
|
+
'standard configuration when language is "en" and output: \'in in 1 yr.\'', function() {
|
|
714
|
+
HandlebarsI18n.configure([
|
|
715
|
+
['en', 'RelativeTimeFormat', { style: "short", unit: "year" }, 'date-rel-spec'],
|
|
716
|
+
['en', 'RelativeTimeFormat', { style: "long", unit: "day" }]
|
|
717
|
+
]);
|
|
718
|
+
i18next.changeLanguage('en');
|
|
719
|
+
const res = hI18n.helpers._dateDiff('1996-12-17T00:00:00', '1995-12-17T00:00:00',{ hash: { format: 'date-rel-spec'} });
|
|
720
|
+
assert.equal('in 1 yr.', res);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
/********************************************************************
|
|
725
|
+
Tests for custom format configurations for function _num
|
|
726
|
+
********************************************************************/
|
|
428
727
|
|
|
429
728
|
it('function _num when called after configure() with defined custom format (minimumFractionDigits:4) should return ' +
|
|
430
729
|
'comma separated triples of decimals and 4 fraction of digits when language is "en"', function() {
|
|
@@ -495,7 +794,9 @@ describe('handlebars-i18n Test', function() {
|
|
|
495
794
|
});
|
|
496
795
|
|
|
497
796
|
|
|
498
|
-
|
|
797
|
+
/********************************************************************
|
|
798
|
+
Tests for custom format configurations for function _price
|
|
799
|
+
********************************************************************/
|
|
499
800
|
|
|
500
801
|
it('function _price when called after configure() with defined custom format (minimumFractionDigits:4) should return ' +
|
|
501
802
|
'comma separated triples of decimals and 4 fraction of digits when language is "en"', function() {
|
|
@@ -572,4 +873,117 @@ describe('handlebars-i18n Test', function() {
|
|
|
572
873
|
assert.equal('$2.00', res);
|
|
573
874
|
});
|
|
574
875
|
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
describe('handlebars-i18n Private helper Function Tests (in production not exported)', () => {
|
|
879
|
+
|
|
880
|
+
/********************************************************************
|
|
881
|
+
Tests for private function applyToConstructor
|
|
882
|
+
********************************************************************/
|
|
883
|
+
|
|
884
|
+
// Mock constructor function
|
|
885
|
+
function TestConstructor(a, b) {
|
|
886
|
+
this.a = a;
|
|
887
|
+
this.b = b;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
it('should return an instance of the constructor with provided arguments', () => {
|
|
891
|
+
const args = [1, 2];
|
|
892
|
+
const instance = HandlebarsI18n.private.applyToConstructor(TestConstructor, args);
|
|
893
|
+
expect(instance).to.be.an.instanceof(TestConstructor);
|
|
894
|
+
expect(instance.a).to.equal(1);
|
|
895
|
+
expect(instance.b).to.equal(2);
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
/* it('should handle no arguments', () => {
|
|
899
|
+
const instance = HandlebarsI18n.private.applyToConstructor(TestConstructor, []);
|
|
900
|
+
expect(instance).to.be.an.instanceof(TestConstructor);
|
|
901
|
+
expect(instance.a).to.equal(null);
|
|
902
|
+
expect(instance.b).to.equal(undefined); // because 'undefined' is passed as second argument
|
|
903
|
+
});*/
|
|
904
|
+
|
|
905
|
+
it('should handle constructor with no arguments', () => {
|
|
906
|
+
function ConstructorWithNoArgs() {
|
|
907
|
+
this.value = 10;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
const instance = HandlebarsI18n.private.applyToConstructor(ConstructorWithNoArgs, []);
|
|
911
|
+
expect(instance).to.be.an.instanceof(ConstructorWithNoArgs);
|
|
912
|
+
expect(instance.value).to.equal(10);
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
it('should handle constructor with complex arguments', () => {
|
|
916
|
+
class ComplexArgument {
|
|
917
|
+
constructor(value) {
|
|
918
|
+
this.value = value;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
const args = [new ComplexArgument(5)];
|
|
923
|
+
function ConstructorWithComplexArg(arg) {
|
|
924
|
+
this.arg = arg;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
const instance = HandlebarsI18n.private.applyToConstructor(ConstructorWithComplexArg, args);
|
|
928
|
+
expect(instance).to.be.an.instanceof(ConstructorWithComplexArg);
|
|
929
|
+
expect(instance.arg).to.be.an.instanceof(ComplexArgument);
|
|
930
|
+
expect(instance.arg.value).to.equal(5);
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
/********************************************************************
|
|
935
|
+
Tests for private function applyToConstructor
|
|
936
|
+
********************************************************************/
|
|
937
|
+
|
|
938
|
+
const hndlbrsOpts = {
|
|
939
|
+
hash: {
|
|
940
|
+
format: 'customFormat',
|
|
941
|
+
// Add other properties if needed for specific test cases
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
const lang = 'en';
|
|
945
|
+
const OCFormat = {
|
|
946
|
+
standard: {
|
|
947
|
+
en: { /* Standard configuration for English */ },
|
|
948
|
+
all: { /* Universal configuration for all languages */ }
|
|
949
|
+
},
|
|
950
|
+
custom: {
|
|
951
|
+
customFormat: {
|
|
952
|
+
en: { /* Custom configuration for English */ }
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
/*it('should return template configuration when options object with content is provided', () => {
|
|
958
|
+
const result = HandlebarsI18n.private.configLookup(hndlbrsOpts, lang, OCFormat);
|
|
959
|
+
expect(result).to.deep.equal(hndlbrsOpts.hash);
|
|
960
|
+
});*/
|
|
961
|
+
|
|
962
|
+
it('should return custom configuration when custom format and language are provided', () => {
|
|
963
|
+
const result = HandlebarsI18n.private.configLookup(hndlbrsOpts, lang, OCFormat);
|
|
964
|
+
expect(result).to.deep.equal(OCFormat.custom.customFormat[lang]);
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
it('should return standard language configuration when no custom format is provided', () => {
|
|
968
|
+
const hndlbrsOptsWithoutFormat = {
|
|
969
|
+
hash: {
|
|
970
|
+
// Add other properties if needed for specific test cases
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
const result = HandlebarsI18n.private.configLookup(hndlbrsOptsWithoutFormat, lang, OCFormat);
|
|
974
|
+
expect(result).to.deep.equal(OCFormat.standard[lang]);
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
it('should return universal configuration when no language-specific configuration is provided', () => {
|
|
978
|
+
const langWithoutConfig = 'fr'; // Assuming French configuration is not provided
|
|
979
|
+
const result = HandlebarsI18n.private.configLookup(hndlbrsOpts, langWithoutConfig, OCFormat);
|
|
980
|
+
expect(result).to.deep.equal(OCFormat.standard.all);
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
it('should return an empty object when no configuration is provided at all', () => {
|
|
984
|
+
const result = HandlebarsI18n.private.configLookup({}, lang, OCFormat);
|
|
985
|
+
expect(result).to.deep.equal({});
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
|
|
575
989
|
});
|