handlebars-i18n 1.6.4 → 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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "handlebars-i18n",
3
- "version": "1.6.4",
4
- "description": "handlebars-i18n adds internationalization to handlebars.js using i18next and Intl",
3
+ "version": "1.7.0",
4
+ "description": "handlebars-i18n adds internationalization to handlebars.js using i18next and Intl.",
5
5
  "main": "dist/handlebars-i18n.js",
6
6
  "scripts": {
7
- "test": "nyc mocha",
8
- "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
7
+ "test": "NODE_ENV=TEST nyc mocha",
8
+ "coveralls": "NODE_ENV=TEST istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
9
+ "compress": "gulp compress",
9
10
  "example:js": "cd examples/node-example && node simple-example.js",
10
11
  "example:ts": "cd examples/typescript && tsc && node index.js"
11
12
  },
@@ -23,6 +24,7 @@
23
24
  "Localization",
24
25
  "Globalization",
25
26
  "Date Formatting",
27
+ "Relative Dates",
26
28
  "Currency Formatting",
27
29
  "Number Formatting",
28
30
  "Handlebars",
@@ -52,6 +54,7 @@
52
54
  "gulp-rename": "2.0.0",
53
55
  "gulp-uglify": "3.0.2",
54
56
  "gulp-uglify-es": "3.0.0",
57
+ "html-entities": "^2.5.2",
55
58
  "husky": "7.0.4",
56
59
  "istanbul": "0.4.5",
57
60
  "mocha": "9.2.2",
@@ -61,7 +64,8 @@
61
64
  "dependencies": {
62
65
  "handlebars": "^4.7.7",
63
66
  "i18next": "^21.6.14",
64
- "intl": "^1.2.5"
67
+ "intl": "^1.2.5",
68
+ "relative-time-format": "^1.1.6"
65
69
  },
66
70
  "husky": {
67
71
  "hooks": {
package/readme.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # handlebars-i18n
2
2
 
3
- `handlebars-i18n` adds the internationalization features of [i18next](https://www.i18next.com/) to [handlebars.js](https://handlebarsjs.com/). It also provides **date**, **number**, and **currency formatting** via [Intl](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Intl). Use as node module or in the web browser. Supports Typescript.
3
+ `handlebars-i18n` adds the internationalization features of [i18next](https://www.i18next.com/)
4
+ to [handlebars.js](https://handlebarsjs.com/). It also provides **date**, **number**, and **currency formatting**
5
+ via [Intl](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Intl). Use as node module or in
6
+ the web browser. Supports Typescript.
4
7
 
5
8
  Handlebars-i18n is listed amongst i18next’s [framework helpers](https://www.i18next.com/overview/supported-frameworks).
6
9
 
@@ -62,6 +65,7 @@ With ES6 import syntax:
62
65
 
63
66
  ```typescript
64
67
  import * as HandlebarsI18n from "handlebars-i18n";
68
+
65
69
  HandlebarsI18n.init();
66
70
  ```
67
71
 
@@ -73,21 +77,21 @@ Initialize i18next with your language strings and default settings:
73
77
  const i18next = require("i18next");
74
78
 
75
79
  i18next.init({
76
- resources : {
77
- "en" : {
78
- translation : {
80
+ resources: {
81
+ "en": {
82
+ translation: {
79
83
  "phrase1": "What is good?",
80
84
  "phrase2": "{{thing}} is good."
81
85
  }
82
86
  },
83
- "de" : {
87
+ "de": {
84
88
  translation: {
85
89
  "phrase1": "Was ist gut?",
86
90
  "phrase2": "{{thing}} ist gut."
87
91
  }
88
92
  }
89
93
  },
90
- lng : "en"
94
+ lng: "en"
91
95
  });
92
96
  ```
93
97
 
@@ -153,20 +157,23 @@ Finally use in template:
153
157
 
154
158
  ## Additional CLI Helper for Handlebars-i18n available :metal:
155
159
 
156
- Handlebars-i18n has its own command line interface [handlebars-i18n-cli](https://www.npmjs.com/package/handlebars-i18n-cli).
160
+ Handlebars-i18n has its own command line
161
+ interface [handlebars-i18n-cli](https://www.npmjs.com/package/handlebars-i18n-cli).
157
162
 
158
163
  ```bash
159
164
  $ npm i handlebars-i18n-cli --save-dev
160
165
  ```
161
166
 
162
167
  Automatically extract translation strings from handlebars templates and generate i18next conform json files from it.
163
- Handlebars-i18n-cli also helps to keep[](https://) your translations up to date when changes are made in the templates over time.
168
+ Handlebars-i18n-cli also helps to keep[](https://) your translations up to date when changes are made in the templates
169
+ over time.
164
170
 
165
171
  ## API
166
172
 
167
173
  ### __
168
174
 
169
- Returns the phrase associated with the given key for the selected language. __ will take all options i18next’s [t-function](https://www.i18next.com/overview/api#t) would take.
175
+ Returns the phrase associated with the given key for the selected language. __ will take all options
176
+ i18next’s [t-function](https://www.i18next.com/overview/api#t) would take.
170
177
  The primary key can be passed hard encoded in the template when written in quotes:
171
178
 
172
179
  ```
@@ -190,9 +197,13 @@ Template usage:
190
197
  The i18next resource:
191
198
 
192
199
  ```javascript
193
- "en" : {
200
+ "en"
201
+ :
202
+ {
194
203
  translation : {
195
- "whatIsWhat": "{{a}} is {{b}}."
204
+ "whatIsWhat"
205
+ :
206
+ "{{a}} is {{b}}."
196
207
  }
197
208
  }
198
209
  ```
@@ -204,12 +215,19 @@ The i18next resource:
204
215
  ```
205
216
 
206
217
  ```javascript
207
- "en" : {
218
+ "en"
219
+ :
220
+ {
208
221
  translation : {
209
- "keyWithCount" : "{{count}} item",
210
- "keyWithCount_plural" : "{{count}} items"
222
+ "keyWithCount"
223
+ :
224
+ "{{count}} item",
225
+ "keyWithCount_plural"
226
+ :
227
+ "{{count}} items"
211
228
  }
212
- }, ...
229
+ }
230
+ , ...
213
231
  ```
214
232
 
215
233
  **Override globally selected language**
@@ -224,7 +242,7 @@ Will output the contents for "**de**" even though other language is selected.
224
242
 
225
243
  ### _locale
226
244
 
227
- Returns the shortcode of i18next’s currently selected language such as "**en**", "**de**", "**fr**", "**fi**" … etc.
245
+ Returns the shortcode of i18next’s currently selected language such as "**en**", "**de**", "**fi**", "**ja**" … etc.
228
246
 
229
247
  ```
230
248
  {{_locale}}
@@ -250,7 +268,10 @@ Outputs a formatted date according to the language specific conventions.
250
268
  {{_date}}
251
269
  ```
252
270
 
253
- If called without argument the current date is returned. Any other input date can be passed as a conventional date string, a number (timestamp in milliseconds), or a date array. _date accepts all arguments Javascript’s [new Date()](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date) constructor would accept.
271
+ If called without argument the current date is returned. Any other input date can be passed as a conventional date
272
+ string, a number (timestamp in milliseconds), or a date array. _date accepts all arguments
273
+ Javascript’s [new Date()](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date)
274
+ constructor would accept.
254
275
 
255
276
  **Date argument given as date string:**
256
277
 
@@ -270,7 +291,8 @@ or
270
291
  {{_date 1583922952743}}
271
292
  ```
272
293
 
273
- **Date argument given as javascript date array** [year, monthIndex [, day [, hour [, minutes [, seconds [, milliseconds]]]]]]:
294
+ **Date argument given as javascript date array** [year, monthIndex [, day [, hour [, minutes [,
295
+ seconds [, milliseconds]]]]]]:
274
296
 
275
297
  ```
276
298
  {{_date "[2012, 11, 20, 3, 0, 0]"}}
@@ -278,7 +300,10 @@ or
278
300
 
279
301
  **Additional arguments for formatting**
280
302
 
281
- You can add multiple arguments for individual formatting. See [Intl DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) for your option arguments.
303
+ You can add multiple arguments for individual formatting.
304
+ See [Intl DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)
305
+ for your option arguments. Alternatively check this repo’s TS types
306
+ in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
282
307
 
283
308
  ```
284
309
  {{_date 1583922952743 year="2-digit" day="2-digit" timeZone="America/Los_Angeles"}}
@@ -286,9 +311,51 @@ You can add multiple arguments for individual formatting. See [Intl DateTimeForm
286
311
 
287
312
  ---
288
313
 
314
+ ### _dateRel :tada: new in 1.7
315
+
316
+ Outputs a string with a relative date statement, formatted according to the language specific conventions.
317
+
318
+ ```
319
+ {{_dateRel 7 unit="hour"}}
320
+ ```
321
+
322
+ Will output for "en" → **in 7 hours**
323
+
324
+ ```
325
+ {{_dateRel -7 unit="hour"}}
326
+ ```
327
+
328
+ Will output for "en" → **7 hours ago**
329
+
330
+ A positive number argument leads to a future event statement, a negative refers to a past date. Possible units
331
+ are `"second"` | `"minute"` | `"hour"` | `"day"` | `"week"` | `"month"` | `"year"` (default is `"hour"`). For a complete
332
+ set of options (such as `numberingSystem` or `localeMatcher`)
333
+ see [Intl.RelativeTimeFormat Constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat).
334
+ Alternatively check this repo’s TS types
335
+ in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
336
+
337
+ ---
338
+
339
+ ### _dateDiff :tada: new in 1.7
340
+
341
+ Outputs the time difference between two given dates.
342
+
343
+ ```
344
+ {{_dateDiff "1996-12-17T00:00:00", "1995-12-17T00:00:00" unit="year"}}
345
+ ```
346
+
347
+ Will output for "en" → **in 1 year**
348
+
349
+ The second date argument is subtracted from the first. If the difference is a positive value, a future event statement
350
+ is made. A negative value refers to a past date. Allowed date input formats are similar to *_date*, options equal
351
+ **_dateRel**.
352
+
353
+ ---
354
+
289
355
  ### _num
290
356
 
291
- Outputs a formatted number according to the language specific conventions of number representation, e.g. **4,100,000.8314** for "**en**", but **4.100.000,8314** for "**de**".
357
+ Outputs a formatted number according to the language specific conventions of number representation, e.g. *
358
+ *4,100,000.8314** for "**en**", but **4.100.000,8314** for "**de**".
292
359
 
293
360
  ```
294
361
  {{_num 4100000.8314 }}
@@ -296,7 +363,10 @@ Outputs a formatted number according to the language specific conventions of num
296
363
 
297
364
  **Additional arguments for formatting**
298
365
 
299
- You can add multiple arguments for individual formatting. See [Intl NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) for your option arguments.
366
+ You can add multiple arguments for individual formatting.
367
+ See [Intl NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
368
+ for your option arguments. Alternatively check this repo’s TS types
369
+ in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
300
370
 
301
371
  ```
302
372
  {{_num 3.14159 maximumFractionDigits=2}}
@@ -308,7 +378,8 @@ Will output **3.14** for "**en**", but **3,14** for "**de**".
308
378
 
309
379
  ### _price
310
380
 
311
- Outputs a formatted currency string according to the language specific conventions of price representation, e.g. **€9,999.99** for "**en**", but **9.999,99 €** for "**de**".
381
+ Outputs a formatted currency string according to the language specific conventions of price representation, e.g. *
382
+ *€9,999.99** for "**en**", but **9.999,99 €** for "**de**".
312
383
 
313
384
  ```
314
385
  {{_price 9999.99}}
@@ -316,7 +387,10 @@ Outputs a formatted currency string according to the language specific conventio
316
387
 
317
388
  **Additional arguments for formatting**
318
389
 
319
- You can add multiple arguments for individual currency formatting. See [Intl NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) for your option arguments.
390
+ You can add multiple arguments for individual currency formatting.
391
+ See [Intl NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
392
+ for your option arguments. Alternatively check this repo’s TS types
393
+ in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
320
394
 
321
395
  ```
322
396
  {{_price 1000 currency="JPY" minimumFractionDigits=2}}
@@ -328,17 +402,34 @@ You can add multiple arguments for individual currency formatting. See [Intl Num
328
402
 
329
403
  ### Generic language format settings
330
404
 
331
- Instead of defining the formatting options for each date, number or price anew, you can configure global settings for all languages or only for specific languages.
405
+ Instead of defining the formatting options for each date, number or price anew, you can configure global settings for
406
+ all languages or only for specific languages.
332
407
 
333
408
  ```javascript
334
409
  HandlebarsI18n.configure("all", "DateTimeFormat", {timeZone: "America/Los_Angeles"});
335
410
  ```
336
411
 
337
- First argument is the language shortcode or "**all**" for all languages. Second is the format option you want to address (DateTimeFormat, NumberFormat, or PriceFormat). Third argument ist the options object with the specific settings.
412
+ First argument is the language shortcode or "**all**" for all languages. Second is the format option you want to
413
+ address (`DateTimeFormat`, `RelativeTimeFormat`, `NumberFormat`, or `PriceFormat`). Third argument is the options object
414
+ with the specific settings.
415
+
416
+ Examples for generic settings:
417
+
418
+ ```javascript
419
+ HandlebarsI18n.configure("all", "RelativeTimeFormat", {style: "long", unit: "second"});
420
+ ```
421
+
422
+ ```javascript
423
+ HandlebarsI18n.configure("all", "NumberFormat", {numberingSystem: "latn", maximumFractionDigits: 0});
424
+ ```
425
+
426
+ ```javascript
427
+ HandlebarsI18n.configure("all", "PriceFormat", {currency: "HKD", currencyDisplay: "code"});
428
+ ```
338
429
 
339
430
  ### Custom language format subsets
340
431
 
341
- You can define specific subsets to be used in the template, i.e. if you want the date in different formats such as:
432
+ You can also define specific subsets to be used in the template, i.e. if you want the date in different formats such as:
342
433
 
343
434
  - **2020** (year-only)
344
435
  - **11.3.2020** (standard-date)
@@ -348,9 +439,9 @@ To do this, define a 4th parameter with a custom name:
348
439
 
349
440
  ```javascript
350
441
  HandlebarsI18n.configure([
351
- ["en", "DateTimeFormat", {year:"numeric"}, "year-only"],
352
- ["en", "DateTimeFormat", {year:"numeric", month:"numeric", day:"numeric"}, "standard-date"],
353
- ['en', 'DateTimeFormat', {hour:"numeric", minute:"numeric", second:"numeric", hour12:false}, "time-only"]
442
+ ["en", "DateTimeFormat", {year: "numeric"}, "year-only"],
443
+ ["en", "DateTimeFormat", {year: "numeric", month: "numeric", day: "numeric"}, "standard-date"],
444
+ ['en', 'DateTimeFormat', {hour: "numeric", minute: "numeric", second: "numeric", hour12: false}, "time-only"]
354
445
  ]);
355
446
  ```
356
447
 
@@ -364,8 +455,10 @@ Call a subset in template with the parameter format="custom-name", like:
364
455
 
365
456
  The general lookup cascade is:
366
457
 
367
- - **1st Priority**: The argument given in the template for custom configurations by the key "format", i.e. `{{_date format="my-custom-format"}}`
368
- - **2nd Priority**: The extra argument(s) given in the template, e.g. `{{_date timeZone="America/Los_Angeles" year="2-digit"}}`
458
+ - **1st Priority**: The argument given in the template for custom configurations by the key "format",
459
+ i.e. `{{_date format="my-custom-format"}}`
460
+ - **2nd Priority**: The extra argument(s) given in the template,
461
+ e.g. `{{_date timeZone="America/Los_Angeles" year="2-digit"}}`
369
462
  - **3rd Priority**: The global setting configured for the current language, such as "**en**"
370
463
  - **4th Priority**: The global setting configured for **all** languages
371
464
  - **Default**: The **Intl** default setting
@@ -378,7 +471,8 @@ This defines that all prices for all languages are represented in Dollar:
378
471
  HandlebarsI18n.configure("all", "PriceFormat", {currency: "USD"});
379
472
  ```
380
473
 
381
- This defines that all prices for all languages are represented in Dollar, but that for the language French the currency is Euro:
474
+ This defines that all prices for all languages are represented in Dollar, but that for the language French the currency
475
+ is Euro:
382
476
 
383
477
  ```javascript
384
478
  HandlebarsI18n.configure([
@@ -387,7 +481,7 @@ HandlebarsI18n.configure([
387
481
  ]);
388
482
  ```
389
483
 
390
- ### Reset an existing configuration
484
+ ### Reset existing configuration
391
485
 
392
486
  Dismiss all existing configurations:
393
487
 
@@ -403,7 +497,9 @@ it instead of the generic Handlebars object like so:
403
497
 
404
498
  ```javascript
405
499
  const HandlebarsModified = require("handlebars");
406
- HandlebarsModified.registerHelper("foo", function() { return "what you want" });
500
+ HandlebarsModified.registerHelper("foo", function () {
501
+ return "what you want"
502
+ });
407
503
  HandlebarsI18n.init(HandlebarsModified);
408
504
  ```
409
505
 
@@ -413,7 +509,7 @@ The same can be done for a custom instance of i18next. Pass it as the second arg
413
509
 
414
510
  ```javascript
415
511
  const i18nextCustom = require("i18next");
416
- i18nextCustom.createInstance( /* pass some params here ... */ );
512
+ i18nextCustom.createInstance( /* pass some params here ... */);
417
513
  HandlebarsI18n.init(null, i18nextCustom);
418
514
  ```
419
515
 
@@ -425,8 +521,10 @@ $ npm test
425
521
 
426
522
  ## Merci à vous
427
523
 
428
- For your contribution, I would like to thank [@MickL](https://github.com/MickL), [@dargmuesli](https://github.com/dargmuesli), and [@DiefBell](DiefBell).
524
+ For your contribution, I would like to
525
+ thank [@MickL](https://github.com/MickL), [@dargmuesli](https://github.com/dargmuesli), and [@DiefBell](DiefBell).
429
526
 
430
527
  ## Note
431
528
 
432
- There is a *different* package named [handlebars-i18next](https://www.npmjs.com/package/handlebars-i18next) by [@jgonggrijp](https://github.com/jgonggrijp) which might also suit your needs. Cheers!
529
+ There is a *different* package named [handlebars-i18next](https://www.npmjs.com/package/handlebars-i18next)
530
+ by [@jgonggrijp](https://github.com/jgonggrijp) which might also suit your needs. Cheers!