handlebars-i18n 1.7.1 → 1.8.1
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/dist/handlebars-i18n.js +66 -1
- package/dist/handlebars-i18n.min.js +1 -1
- package/examples/browser-example/index.html +17 -9
- package/examples/node-example/simple-example.js +87 -71
- package/examples/typescript/test.hbs +13 -7
- package/package.json +1 -1
- package/readme.md +72 -68
- package/test/handlebars-i18n.test.js +236 -81
package/readme.md
CHANGED
|
@@ -8,13 +8,14 @@ the web browser. Supports Typescript.
|
|
|
8
8
|
Handlebars-i18n is listed amongst i18next’s [framework helpers](https://www.i18next.com/overview/supported-frameworks).
|
|
9
9
|
|
|
10
10
|
[](https://opensource.org/licenses/MIT)
|
|
11
|
-

|
|
12
12
|
[](https://github.com/fwalzel/handlebars-i18n/actions/workflows/node.js.yml/badge.svg)
|
|
13
13
|
[](https://coveralls.io/github/Aller-Couleur/handlebars-i18n?branch=master)
|
|
14
14
|
[](https://codeclimate.com/github/Aller-Couleur/handlebars-i18n)
|
|
15
15
|
[](https://snyk.io/test/github/Aller-Couleur/handlebars-i18n/badge.svg)
|
|
16
16
|

|
|
17
17
|

|
|
18
|
+

|
|
18
19
|
|
|
19
20
|
## License
|
|
20
21
|
|
|
@@ -28,28 +29,27 @@ If you use handlebars-i18n in a professional context, you could
|
|
|
28
29
|
|
|
29
30
|
## Install
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
$ npm i handlebars-i18n
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
For older versions do:
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
$ npm i handlebars-i18n handlebars@4.7.6 i18next@20.2.1 intl@1.2.5
|
|
32
|
+
```sh
|
|
33
|
+
npm i handlebars-i18n
|
|
41
34
|
```
|
|
42
35
|
|
|
43
|
-
##
|
|
36
|
+
## Import
|
|
44
37
|
|
|
45
|
-
|
|
38
|
+
Import as commonJS within node environment:
|
|
46
39
|
|
|
47
40
|
```javascript
|
|
48
41
|
const HandlebarsI18n = require("handlebars-i18n");
|
|
49
42
|
HandlebarsI18n.init();
|
|
50
43
|
```
|
|
51
44
|
|
|
52
|
-
|
|
45
|
+
With ES6 import syntax:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import * as HandlebarsI18n from "handlebars-i18n";
|
|
49
|
+
HandlebarsI18n.init();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Usage in web browser (old school):
|
|
53
53
|
|
|
54
54
|
```javascript
|
|
55
55
|
<script src="handlebars.js"></script>
|
|
@@ -61,12 +61,9 @@ Usage in web browser:
|
|
|
61
61
|
</script>
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
import * as HandlebarsI18n from "handlebars-i18n";
|
|
68
|
-
|
|
69
|
-
HandlebarsI18n.init();
|
|
64
|
+
Via jsDelivr CDN:
|
|
65
|
+
```javascript
|
|
66
|
+
<script src="https://cdn.jsdelivr.net/npm/handlebars-i18n@1.8.0/dist/handlebars-i18n.min.js"></script>
|
|
70
67
|
```
|
|
71
68
|
|
|
72
69
|
## Quick example
|
|
@@ -147,21 +144,21 @@ Finally use in template:
|
|
|
147
144
|
|
|
148
145
|
* returns for "en" → **$1,200.99**
|
|
149
146
|
|
|
150
|
-
##
|
|
147
|
+
## Detailed examples
|
|
151
148
|
|
|
152
149
|
:point_right: See the *examples folder* in the repo for more use cases and details.
|
|
153
150
|
|
|
154
|
-
- Open `examples/browser-example/index.html` in your Web browser to see an implementation with a simple UI
|
|
155
|
-
- Run
|
|
156
|
-
- Run
|
|
151
|
+
- Open `examples/browser-example/index.html` in your Web browser to see an implementation with a simple UI.
|
|
152
|
+
- Run `npm run example:js` in the console to get a very basic node example logged.
|
|
153
|
+
- Run `npm run example:ts` to compile and log a typescript example.
|
|
157
154
|
|
|
158
155
|
## Additional CLI Helper for Handlebars-i18n available :metal:
|
|
159
156
|
|
|
160
157
|
Handlebars-i18n has its own command line
|
|
161
158
|
interface [handlebars-i18n-cli](https://www.npmjs.com/package/handlebars-i18n-cli).
|
|
162
159
|
|
|
163
|
-
```
|
|
164
|
-
|
|
160
|
+
```sh
|
|
161
|
+
npm i handlebars-i18n-cli --save-dev
|
|
165
162
|
```
|
|
166
163
|
|
|
167
164
|
Automatically extract translation strings from handlebars templates and generate i18next conform json files from it.
|
|
@@ -197,13 +194,9 @@ Template usage:
|
|
|
197
194
|
The i18next resource:
|
|
198
195
|
|
|
199
196
|
```javascript
|
|
200
|
-
"en"
|
|
201
|
-
:
|
|
202
|
-
{
|
|
197
|
+
"en" : {
|
|
203
198
|
translation : {
|
|
204
|
-
"whatIsWhat"
|
|
205
|
-
:
|
|
206
|
-
"{{a}} is {{b}}."
|
|
199
|
+
"whatIsWhat" : "{{a}} is {{b}}."
|
|
207
200
|
}
|
|
208
201
|
}
|
|
209
202
|
```
|
|
@@ -215,19 +208,12 @@ The i18next resource:
|
|
|
215
208
|
```
|
|
216
209
|
|
|
217
210
|
```javascript
|
|
218
|
-
"en"
|
|
219
|
-
:
|
|
220
|
-
{
|
|
211
|
+
"en" : {
|
|
221
212
|
translation : {
|
|
222
|
-
"keyWithCount"
|
|
223
|
-
|
|
224
|
-
"{{count}} item",
|
|
225
|
-
"keyWithCount_plural"
|
|
226
|
-
:
|
|
227
|
-
"{{count}} items"
|
|
213
|
+
"keyWithCount" : "{{count}} item",
|
|
214
|
+
"keyWithCount_plural" : "{{count}} items"
|
|
228
215
|
}
|
|
229
|
-
}
|
|
230
|
-
, ...
|
|
216
|
+
},
|
|
231
217
|
```
|
|
232
218
|
|
|
233
219
|
**Override globally selected language**
|
|
@@ -311,51 +297,67 @@ in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
|
|
|
311
297
|
|
|
312
298
|
---
|
|
313
299
|
|
|
314
|
-
###
|
|
300
|
+
### _dateAdd :tada: new in 1.8
|
|
315
301
|
|
|
316
|
-
|
|
302
|
+
Adds a time offset in a given unit to a date, returns the modified date.
|
|
317
303
|
|
|
318
304
|
```
|
|
319
|
-
{{
|
|
305
|
+
{{_dateAdd "1996-12-17" 24 unit="hour"}}
|
|
320
306
|
```
|
|
321
307
|
|
|
322
|
-
Will output for "en" → **
|
|
308
|
+
Will output for "en" → **12/18/1996**
|
|
309
|
+
|
|
310
|
+
The first argument is a date (see function **_date** for valid date inputs). The second argument is a time amount given
|
|
311
|
+
as number. The option **unit** specifies the time amount. Possible units
|
|
312
|
+
are `"second"` | `"minute"` | `"hour"` | `"day"` | `"week"` | `"month"` | `"quarter"` |`"year"` (default is `"hour"`).
|
|
313
|
+
Further options as for function **_date** can be applied.
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
### _dateDiff
|
|
318
|
+
|
|
319
|
+
Outputs the relative time difference between two given dates.
|
|
323
320
|
|
|
324
321
|
```
|
|
325
|
-
{{
|
|
322
|
+
{{_dateDiff "1996-12-17T00:00:00" "1995-12-17T00:00:00" unit="year"}}
|
|
326
323
|
```
|
|
327
324
|
|
|
328
|
-
Will output for "en" → **
|
|
325
|
+
Will output for "en" → **in 1 year**
|
|
329
326
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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).
|
|
327
|
+
The second date argument is subtracted from the first. If the difference is a positive value, a future event statement
|
|
328
|
+
is made. A negative value refers to a past date. (If no second argument is given, the default date is the present moment).
|
|
329
|
+
Allowed date input formats are similar to **_date**, options equal **_dateRel**. Default unit is `"hour"`.
|
|
336
330
|
|
|
337
331
|
---
|
|
338
332
|
|
|
339
|
-
###
|
|
333
|
+
### _dateRel
|
|
340
334
|
|
|
341
|
-
Outputs
|
|
335
|
+
Outputs a string with a relative date statement, formatted according to the language specific conventions.
|
|
342
336
|
|
|
343
337
|
```
|
|
344
|
-
{{
|
|
338
|
+
{{_dateRel 7 unit="hour"}}
|
|
345
339
|
```
|
|
346
340
|
|
|
347
|
-
Will output for "en" → **in
|
|
341
|
+
Will output for "en" → **in 7 hours**
|
|
348
342
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
343
|
+
```
|
|
344
|
+
{{_dateRel -7 unit="hour"}}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
Will output for "en" → **7 hours ago**
|
|
348
|
+
|
|
349
|
+
A positive number argument leads to a future event statement, a negative refers to a past date. Possible units
|
|
350
|
+
are `"second"` | `"minute"` | `"hour"` | `"day"` | `"week"` | `"month"` | `"quarter"` |`"year"` (default is `"hour"`).
|
|
351
|
+
For a complete set of options (such as `numberingSystem` or `localeMatcher`)
|
|
352
|
+
see [Intl.RelativeTimeFormat Constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat).
|
|
353
|
+
Alternatively check this repo’s TS types in [handlebars-i18n.d.ts](./dist/handlebars-i18n.d.ts).
|
|
352
354
|
|
|
353
355
|
---
|
|
354
356
|
|
|
355
357
|
### _num
|
|
356
358
|
|
|
357
|
-
Outputs a formatted number according to the language specific conventions of number representation, e.g.
|
|
358
|
-
|
|
359
|
+
Outputs a formatted number according to the language specific conventions of number representation, e.g.
|
|
360
|
+
**4,100,000.8314** for "**en**", but **4.100.000,8314** for "**de**".
|
|
359
361
|
|
|
360
362
|
```
|
|
361
363
|
{{_num 4100000.8314 }}
|
|
@@ -378,8 +380,8 @@ Will output **3.14** for "**en**", but **3,14** for "**de**".
|
|
|
378
380
|
|
|
379
381
|
### _price
|
|
380
382
|
|
|
381
|
-
Outputs a formatted currency string according to the language specific conventions of price representation, e.g.
|
|
382
|
-
|
|
383
|
+
Outputs a formatted currency string according to the language specific conventions of price representation, e.g.
|
|
384
|
+
**€9,999.99** for "**en**", but **9.999,99 €** for "**de**".
|
|
383
385
|
|
|
384
386
|
```
|
|
385
387
|
{{_price 9999.99}}
|
|
@@ -451,6 +453,8 @@ Call a subset in template with the parameter format="custom-name", like:
|
|
|
451
453
|
{{_date myDate format="year-only"}}
|
|
452
454
|
```
|
|
453
455
|
|
|
456
|
+
Subsets must be defined per language, a subset for "all" is invalid.
|
|
457
|
+
|
|
454
458
|
### The lookup cascade
|
|
455
459
|
|
|
456
460
|
The general lookup cascade is:
|
|
@@ -515,8 +519,8 @@ HandlebarsI18n.init(null, i18nextCustom);
|
|
|
515
519
|
|
|
516
520
|
## Run tests
|
|
517
521
|
|
|
518
|
-
```
|
|
519
|
-
|
|
522
|
+
```sh
|
|
523
|
+
npm test
|
|
520
524
|
```
|
|
521
525
|
|
|
522
526
|
## Merci à vous
|