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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "handlebars-i18n",
3
- "version": "1.6.3",
4
- "description": "handlebars-i18n adds internationlization 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
  },
@@ -17,16 +18,16 @@
17
18
  "url": "https://github.com/aller-couleur/handlebars-i18n.git"
18
19
  },
19
20
  "keywords": [
20
- "handlebars-i18n",
21
+ "Translation",
22
+ "Multi-language",
21
23
  "Internationalization",
22
24
  "Localization",
23
25
  "Globalization",
24
- "Translation",
25
26
  "Date Formatting",
27
+ "Relative Dates",
26
28
  "Currency Formatting",
27
29
  "Number Formatting",
28
30
  "Handlebars",
29
- "Handlebars.js",
30
31
  "i18next",
31
32
  "i18n",
32
33
  "Intl",
@@ -53,6 +54,7 @@
53
54
  "gulp-rename": "2.0.0",
54
55
  "gulp-uglify": "3.0.2",
55
56
  "gulp-uglify-es": "3.0.0",
57
+ "html-entities": "^2.5.2",
56
58
  "husky": "7.0.4",
57
59
  "istanbul": "0.4.5",
58
60
  "mocha": "9.2.2",
@@ -62,7 +64,8 @@
62
64
  "dependencies": {
63
65
  "handlebars": "^4.7.7",
64
66
  "i18next": "^21.6.14",
65
- "intl": "^1.2.5"
67
+ "intl": "^1.2.5",
68
+ "relative-time-format": "^1.1.6"
66
69
  },
67
70
  "husky": {
68
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
 
@@ -13,10 +16,10 @@ Handlebars-i18n is listed amongst i18next’s [framework helpers](https://www.i1
13
16
  ![npm](https://img.shields.io/npm/dt/handlebars-i18n)
14
17
  ![npm](https://img.shields.io/npm/dm/handlebars-i18n)
15
18
 
16
-
17
19
  ## License
18
20
 
19
- Copyright (c) 2020 Florian Walzel,
21
+ Copyright (c) 2020–24 Florian Walzel,
22
+
20
23
  MIT License
21
24
 
22
25
  If you use handlebars-i18n in a professional context, you could
@@ -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
 
@@ -122,51 +126,60 @@ Finally use in template:
122
126
  ```
123
127
  <p> {{__ "phrase1"}} </p>
124
128
  ```
129
+
125
130
  * returns for "en" &#x2192; **What is good?**
126
131
 
127
132
  ```
128
133
  <p> {{__ "phrase2" thing=myItem}} </p>
129
134
  ```
135
+
130
136
  * returns for "en" &#x2192; **handlebars-i18n is good.**
131
137
 
132
138
  ```
133
139
  <p> {{_date myDate}} </p>
134
140
  ```
141
+
135
142
  * returns for "en" &#x2192; **March 11, 2020, 4:24 AM**
136
143
 
137
144
  ```
138
145
  <p> {{_price myPrice}} </p>
139
146
  ```
147
+
140
148
  * returns for "en" &#x2192; **$1,200.99**
141
149
 
142
150
  ## Further examples
143
151
 
144
152
  :point_right: See the *examples folder* in the repo for more use cases and details.
145
153
 
154
+ - Open `examples/browser-example/index.html` in your Web browser to see an implementation with a simple UI
155
+ - Run `$ npm run example:js` in the console to get a very basic node example logged
156
+ - Run `$ npm run example:ts` to compile and log a typescript example
146
157
 
147
- ## NEW! :star_struck: Additional CLI Helper for Handlebars-i18n
158
+ ## Additional CLI Helper for Handlebars-i18n available :metal:
148
159
 
149
- Handlebars-i18n has its own command line interface [handlebars-i18n-cli](https://www.npmjs.com/package/handlebars-i18n-cli) by now.
160
+ Handlebars-i18n has its own command line
161
+ interface [handlebars-i18n-cli](https://www.npmjs.com/package/handlebars-i18n-cli).
150
162
 
151
163
  ```bash
152
164
  $ npm i handlebars-i18n-cli --save-dev
153
165
  ```
154
166
 
155
167
  Automatically extract translation strings from handlebars templates and generate i18next conform json files from it.
156
- Handlebars-i18n-cli also helps to keep your translations up to date when changes are made in the templates over time.
157
-
168
+ Handlebars-i18n-cli also helps to keep[](https://) your translations up to date when changes are made in the templates
169
+ over time.
158
170
 
159
171
  ## API
160
172
 
161
-
162
173
  ### __
163
174
 
164
- 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.
165
177
  The primary key can be passed hard encoded in the template when written in quotes:
166
178
 
167
179
  ```
168
180
  {{__ "keyToTranslationPhrase"}}
169
181
  ```
182
+
170
183
  … or it can be referenced via a handlebars variable:
171
184
 
172
185
  ```
@@ -184,9 +197,13 @@ Template usage:
184
197
  The i18next resource:
185
198
 
186
199
  ```javascript
187
- "en" : {
200
+ "en"
201
+ :
202
+ {
188
203
  translation : {
189
- "whatIsWhat": "{{a}} is {{b}}."
204
+ "whatIsWhat"
205
+ :
206
+ "{{a}} is {{b}}."
190
207
  }
191
208
  }
192
209
  ```
@@ -198,12 +215,19 @@ The i18next resource:
198
215
  ```
199
216
 
200
217
  ```javascript
201
- "en" : {
218
+ "en"
219
+ :
220
+ {
202
221
  translation : {
203
- "keyWithCount" : "{{count}} item",
204
- "keyWithCount_plural" : "{{count}} items"
222
+ "keyWithCount"
223
+ :
224
+ "{{count}} item",
225
+ "keyWithCount_plural"
226
+ :
227
+ "{{count}} items"
205
228
  }
206
- }, ...
229
+ }
230
+ , ...
207
231
  ```
208
232
 
209
233
  **Override globally selected language**
@@ -216,15 +240,14 @@ Will output the contents for "**de**" even though other language is selected.
216
240
 
217
241
  ---
218
242
 
219
-
220
-
221
243
  ### _locale
222
244
 
223
- 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.
224
246
 
225
247
  ```
226
248
  {{_locale}}
227
249
  ```
250
+
228
251
  ---
229
252
 
230
253
  ### localeIs
@@ -234,6 +257,7 @@ Checks a string against i18next’s currently selected language. Returns **true*
234
257
  ```
235
258
  {{#if (localeIs "en")}} ... {{/if}}
236
259
  ```
260
+
237
261
  ---
238
262
 
239
263
  ### _date
@@ -244,7 +268,10 @@ Outputs a formatted date according to the language specific conventions.
244
268
  {{_date}}
245
269
  ```
246
270
 
247
- 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.
248
275
 
249
276
  **Date argument given as date string:**
250
277
 
@@ -264,7 +291,8 @@ or
264
291
  {{_date 1583922952743}}
265
292
  ```
266
293
 
267
- **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]]]]]]:
268
296
 
269
297
  ```
270
298
  {{_date "[2012, 11, 20, 3, 0, 0]"}}
@@ -272,16 +300,62 @@ or
272
300
 
273
301
  **Additional arguments for formatting**
274
302
 
275
- 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).
276
307
 
277
308
  ```
278
309
  {{_date 1583922952743 year="2-digit" day="2-digit" timeZone="America/Los_Angeles"}}
279
310
  ```
311
+
312
+ ---
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" &#x2192; **in 7 hours**
323
+
324
+ ```
325
+ {{_dateRel -7 unit="hour"}}
326
+ ```
327
+
328
+ Will output for "en" &#x2192; **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" &#x2192; **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
+
280
353
  ---
281
354
 
282
355
  ### _num
283
356
 
284
- 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**".
285
359
 
286
360
  ```
287
361
  {{_num 4100000.8314 }}
@@ -289,19 +363,23 @@ Outputs a formatted number according to the language specific conventions of num
289
363
 
290
364
  **Additional arguments for formatting**
291
365
 
292
- 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).
293
370
 
294
371
  ```
295
372
  {{_num 3.14159 maximumFractionDigits=2}}
296
373
  ```
374
+
297
375
  Will output **3.14** for "**en**", but **3,14** for "**de**".
298
376
 
299
377
  ---
300
378
 
301
379
  ### _price
302
380
 
303
- 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**".
304
-
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**".
305
383
 
306
384
  ```
307
385
  {{_price 9999.99}}
@@ -309,7 +387,10 @@ Outputs a formatted currency string according to the language specific conventio
309
387
 
310
388
  **Additional arguments for formatting**
311
389
 
312
- 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).
313
394
 
314
395
  ```
315
396
  {{_price 1000 currency="JPY" minimumFractionDigits=2}}
@@ -321,17 +402,34 @@ You can add multiple arguments for individual currency formatting. See [Intl Num
321
402
 
322
403
  ### Generic language format settings
323
404
 
324
- 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.
325
407
 
326
408
  ```javascript
327
409
  HandlebarsI18n.configure("all", "DateTimeFormat", {timeZone: "America/Los_Angeles"});
328
410
  ```
329
411
 
330
- 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
+ ```
331
429
 
332
430
  ### Custom language format subsets
333
431
 
334
- 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:
335
433
 
336
434
  - **2020** (year-only)
337
435
  - **11.3.2020** (standard-date)
@@ -341,9 +439,9 @@ To do this, define a 4th parameter with a custom name:
341
439
 
342
440
  ```javascript
343
441
  HandlebarsI18n.configure([
344
- ["en", "DateTimeFormat", {year:"numeric"}, "year-only"],
345
- ["en", "DateTimeFormat", {year:"numeric", month:"numeric", day:"numeric"}, "standard-date"],
346
- ['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"]
347
445
  ]);
348
446
  ```
349
447
 
@@ -357,8 +455,10 @@ Call a subset in template with the parameter format="custom-name", like:
357
455
 
358
456
  The general lookup cascade is:
359
457
 
360
- - **1st Priority**: The argument given in the template for custom configurations by the key "format", i.e. `{{_date format="my-custom-format"}}`
361
- - **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"}}`
362
462
  - **3rd Priority**: The global setting configured for the current language, such as "**en**"
363
463
  - **4th Priority**: The global setting configured for **all** languages
364
464
  - **Default**: The **Intl** default setting
@@ -371,7 +471,8 @@ This defines that all prices for all languages are represented in Dollar:
371
471
  HandlebarsI18n.configure("all", "PriceFormat", {currency: "USD"});
372
472
  ```
373
473
 
374
- 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:
375
476
 
376
477
  ```javascript
377
478
  HandlebarsI18n.configure([
@@ -380,7 +481,7 @@ HandlebarsI18n.configure([
380
481
  ]);
381
482
  ```
382
483
 
383
- ### Reset an existing configuration
484
+ ### Reset existing configuration
384
485
 
385
486
  Dismiss all existing configurations:
386
487
 
@@ -396,7 +497,9 @@ it instead of the generic Handlebars object like so:
396
497
 
397
498
  ```javascript
398
499
  const HandlebarsModified = require("handlebars");
399
- HandlebarsModified.registerHelper("foo", function() { return "what you want" });
500
+ HandlebarsModified.registerHelper("foo", function () {
501
+ return "what you want"
502
+ });
400
503
  HandlebarsI18n.init(HandlebarsModified);
401
504
  ```
402
505
 
@@ -406,7 +509,7 @@ The same can be done for a custom instance of i18next. Pass it as the second arg
406
509
 
407
510
  ```javascript
408
511
  const i18nextCustom = require("i18next");
409
- i18nextCustom.createInstance( /* pass some params here ... */ );
512
+ i18nextCustom.createInstance( /* pass some params here ... */);
410
513
  HandlebarsI18n.init(null, i18nextCustom);
411
514
  ```
412
515
 
@@ -418,8 +521,10 @@ $ npm test
418
521
 
419
522
  ## Merci à vous
420
523
 
421
- 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).
422
526
 
423
527
  ## Note
424
528
 
425
- There is a *different* package named [handlebars-i18next](https://www.npmjs.com/package/handlebars-i18next) by [Julian Gonggrijp](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!