handlebars-i18n 1.4.2 → 1.5.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/CHANGELOG.md +8 -0
- package/dist/handlebars-i18n.js +20 -10
- package/dist/handlebars-i18n.min.js +1 -1
- package/package.json +1 -1
- package/readme.md +9 -1
- package/test/handlebars-i18n.test.js +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# [1.5.0](https://github.com/fwalzel/handlebars-i18n/compare/1.4.2...1.5.0) (2022-10-13)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* init with second argument ready, compressed handlebars-i18n.min.js ([ea9e13b](https://github.com/fwalzel/handlebars-i18n/commit/ea9e13bc655e54d6000d437793a88fc555beb2a2))
|
|
7
|
+
* **release:** init with second argument ready, compressed handlebars-i18n.min.js ([249a703](https://github.com/fwalzel/handlebars-i18n/commit/249a70334fe04cf1f036b50f70bdb64d662e8f42))
|
|
8
|
+
|
|
1
9
|
## [1.4.2](https://github.com/fwalzel/handlebars-i18n/compare/1.4.1...1.4.2) (2022-07-21)
|
|
2
10
|
|
|
3
11
|
|
package/dist/handlebars-i18n.js
CHANGED
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
&& typeof hndlbrsOpts.hash === 'object'
|
|
107
107
|
&& Object.keys(hndlbrsOpts.hash).length > 0) {
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
let oh = hndlbrsOpts.hash;
|
|
110
110
|
|
|
111
111
|
// check against a custom format, if nonexistent
|
|
112
112
|
// return the options hash (= template configuration)
|
|
@@ -166,7 +166,7 @@
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
if ((customFormat !== null && typeof customFormat !== 'undefined' && typeof customFormat !== 'string')
|
|
169
|
-
|| customFormat
|
|
169
|
+
|| customFormat === '' || customFormat === ' ') {
|
|
170
170
|
console.error('@ handlebars-i18n.configure(): Invalid argument <' + customFormat + '> ' +
|
|
171
171
|
'Fourth argument (optional) must be a string naming your custom format configuration.');
|
|
172
172
|
return false;
|
|
@@ -255,18 +255,28 @@
|
|
|
255
255
|
/**
|
|
256
256
|
* init all handlebars helpers
|
|
257
257
|
*
|
|
258
|
-
* @param overrideHndlbrs | optional: pass an individual instance of handlebars
|
|
259
|
-
* to override the generic
|
|
258
|
+
* @param overrideHndlbrs | optional: pass an individual instance of handlebars object to the init() function
|
|
259
|
+
* to override the generic instance required in LINE 37
|
|
260
|
+
*
|
|
261
|
+
* @param overrideI18n | optional: pass an individual instance of handlebars i18next to the init() function
|
|
262
|
+
* to override the generic instance required in LINE 38
|
|
260
263
|
*
|
|
261
264
|
* @returns {*}
|
|
262
265
|
*/
|
|
263
|
-
init: function (overrideHndlbrs) {
|
|
266
|
+
init: function (overrideHndlbrs, overrideI18n) {
|
|
264
267
|
|
|
265
|
-
if (typeof overrideHndlbrs === 'object')
|
|
268
|
+
if (typeof overrideHndlbrs === 'object' && overrideHndlbrs !== null)
|
|
266
269
|
handlebars = overrideHndlbrs;
|
|
267
270
|
else if (typeof overrideHndlbrs !== 'undefined' && overrideHndlbrs !== null)
|
|
268
|
-
console.error('@ handlebars-i18n.init(): Invalid Argument given for overrideHndlbrs. ' +
|
|
269
|
-
'Argument must be the Handlebars Object. Using
|
|
271
|
+
console.error('@ handlebars-i18n.init(): Invalid Argument [1] given for overrideHndlbrs. ' +
|
|
272
|
+
'Argument must be the Handlebars Object. Using handlebars object on module instead.');
|
|
273
|
+
|
|
274
|
+
if (typeof overrideI18n === 'object' && overrideI18n !== null)
|
|
275
|
+
i18next = overrideI18n;
|
|
276
|
+
|
|
277
|
+
else if (typeof overrideI18n !== 'undefined' && overrideI18n !== null)
|
|
278
|
+
console.error('@ handlebars-i18n.init(): Invalid Argument [2] given for overrideI18n. ' +
|
|
279
|
+
'Argument must be the i18next Object. Using i18next object on module level instead.');
|
|
270
280
|
|
|
271
281
|
handlebars.registerHelper('__',
|
|
272
282
|
/**
|
|
@@ -340,14 +350,14 @@
|
|
|
340
350
|
}
|
|
341
351
|
else if (typeof dateInput === 'string') {
|
|
342
352
|
|
|
343
|
-
if (dateInput.charAt(0)
|
|
353
|
+
if (dateInput.charAt(0) === '[' && dateInput.slice(-1) === ']') {
|
|
344
354
|
// input as array represented as string such as "[2020, 11]"
|
|
345
355
|
dateInput = dateInput.substring(1, dateInput.length - 1).replace(/ /g, '');
|
|
346
356
|
var dateArr = dateInput.split(',');
|
|
347
357
|
var dateFactory = __applyToConstructor.bind(null, Date);
|
|
348
358
|
date = dateFactory(dateArr);
|
|
349
359
|
}
|
|
350
|
-
else if (dateInput.toLowerCase()
|
|
360
|
+
else if (dateInput.toLowerCase() === 'now' || dateInput.toLowerCase() === 'today') {
|
|
351
361
|
// input as word "now" or "today"
|
|
352
362
|
date = new Date();
|
|
353
363
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,r){if("object"==typeof exports&&"object"==typeof module){
|
|
1
|
+
!function(e,r){if("object"==typeof exports&&"object"==typeof module){let n=require("handlebars"),t=require("i18next"),a=require("intl");module.exports=r(n,t,a)}else"function"==typeof define&&define.amd?define(["Handlebars","i18next","Intl"],r):"object"==typeof e.Handlebars&&"object"==typeof e.i18next&&"object"==typeof e.Intl?e.HandlebarsI18n=r(e.Handlebars,e.i18next,e.Intl):console.error("@ handlebars-i18n: One or more dependencies are missing. Check for Handlebars, i18next and Intl.")}(this,function(e,r,n){"use strict";var t={DateTimeFormat:{standard:{},custom:{}},NumberFormat:{standard:{},custom:{}},PriceFormat:{standard:{all:{style:"currency",currency:"EUR"}},custom:{}}},a=JSON.parse(JSON.stringify(t));function o(e,r){var n=[null].concat(r);return new(e.bind.apply(e,n))}function i(e,r,n){if("object"==typeof e&&"object"==typeof e.hash&&Object.keys(e.hash).length>0){let t=e.hash;if(void 0===t.format)return t;if(void 0!==n.custom[t.format]&&void 0!==n.custom[t.format][r])return n.custom[t.format][r]}return void 0!==n.standard[r]?n.standard[r]:void 0!==n.standard.all?n.standard.all:{}}function u(e,r,n,t){return"string"!=typeof e?(console.error("@ handlebars-i18n.configure(): Invalid argument <"+e+'> First argument must be a string with language code such as "en".'),!1):"DateTimeFormat"!==r&&"NumberFormat"!==r&&"PriceFormat"!==r?(console.error("@ handlebars-i18n.configure(): Invalid argument <"+r+'>. Second argument must be a string with the options key. Use either "DateTimeFormat", "NumberFormat" or "PriceFormat".'),!1):"object"!=typeof n?(console.error("@ handlebars-i18n.configure(): Invalid argument <"+n+"> Third argument must be an object containing the configuration parameters."),!1):(null==t||"string"==typeof t)&&""!==t&&" "!==t||(console.error("@ handlebars-i18n.configure(): Invalid argument <"+t+"> Fourth argument (optional) must be a string naming your custom format configuration."),!1)}function s(e,r,n,t){return null!=t?(void 0===a[r].custom[t]&&(a[r].custom[t]={}),a[r].custom[t][e]=n):a[r].standard[e]=n,!0}return{configure:function(e,r,n,t=null){if("string"!=typeof e&&!Array.isArray(e))return console.error("@ handlebars-i18n.configure(): Invalid argument <"+e+'> First argument must be a string with language code such as "en" or an array with parameters.'),!1;if(Array.isArray(e)){if(e.length<1)return console.log("@ handlebars-i18n.configure(): You passed an empty array, no parameters taken."),!1;e.forEach(e=>{if(!u(e[0],e[1],e[2],e[3]))return!1;s(e[0],e[1],e[2],e[3])})}else{if(!u(e,r,n,t))return!1;s(e,r,n,t)}return!0},reset:function(){return a=JSON.parse(JSON.stringify(t)),!0},init:function(t,u){return"object"==typeof t&&null!==t?e=t:null!=t&&console.error("@ handlebars-i18n.init(): Invalid Argument [1] given for overrideHndlbrs. Argument must be the Handlebars Object. Using handlebars object on module instead."),"object"==typeof u&&null!==u?r=u:null!=u&&console.error("@ handlebars-i18n.init(): Invalid Argument [2] given for overrideI18n. Argument must be the i18next Object. Using i18next object on module level instead."),e.registerHelper("__",function(n,t){return new e.SafeString(void 0!==r?r.t(n,t.hash):n)}),e.registerHelper("_locale",function(){return r.language}),e.registerHelper("localeIs",function(e){return r.language===e}),e.registerHelper("_date",function(e,t){if("number"==typeof e)u=new Date(e);else if("string"==typeof e){if("["===e.charAt(0)&&"]"===e.slice(-1)){var u,s=(e=e.substring(1,e.length-1).replace(/ /g,"")).split(",");u=o.bind(null,Date)(s)}else u="now"===e.toLowerCase()||"today"===e.toLowerCase()?new Date:new Date(e)}else u=new Date;var l=i(t,r.language,a.DateTimeFormat);let c=new n.DateTimeFormat(r.language,l);return c.format(u)}),e.registerHelper("_num",function(e,t){var o=i(t,r.language,a.NumberFormat);let u=new n.NumberFormat(r.language,o);return u.format(e)}),e.registerHelper("_price",function(e,t){var o=i(t,r.language,a.PriceFormat);"string"!=typeof o.style&&"string"==typeof o.currency&&(o.style="currency");let u=new n.NumberFormat(r.language,o);return u.format(e)}),e}}});
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -370,7 +370,7 @@ Dismiss all existing configurations:
|
|
|
370
370
|
HandlebarsI18n.reset();
|
|
371
371
|
```
|
|
372
372
|
|
|
373
|
-
## Using custom instances of Handlebars
|
|
373
|
+
## Using custom instances of Handlebars and/or i18next
|
|
374
374
|
|
|
375
375
|
Sometimes you may want to use a Handlebars Object you have already modified before, or you may want to use multiple discrete instances of Handlebars. In this case you can pass you custom Handlebars instance to the init function to use it instead of the generic Handlebars object like so:
|
|
376
376
|
|
|
@@ -382,6 +382,14 @@ HandlebarsI18n.init(HandlebarsModified);
|
|
|
382
382
|
|
|
383
383
|
HandlebarsI18n will have your previously defined method **foo()** by now.
|
|
384
384
|
|
|
385
|
+
The same can be done for a custom instance of i18next. Pass it as the second argument to the init function.
|
|
386
|
+
|
|
387
|
+
```javascript
|
|
388
|
+
const i18nextCustom = require('i18next');
|
|
389
|
+
i18nextCustom.createInstance( /*... pass some params here */ );
|
|
390
|
+
HandlebarsI18n.init(null, i18nextCustom);
|
|
391
|
+
```
|
|
392
|
+
|
|
385
393
|
|
|
386
394
|
## Note
|
|
387
395
|
|
|
@@ -12,6 +12,7 @@ const expect = require('chai').expect;
|
|
|
12
12
|
const Handlebars = require('handlebars');
|
|
13
13
|
const i18next = require('i18next');
|
|
14
14
|
const HandlebarsI18n = require('../dist/handlebars-i18n');
|
|
15
|
+
//const HandlebarsModified = require("handlebars");
|
|
15
16
|
|
|
16
17
|
describe('handlebars-i18n Test', function() {
|
|
17
18
|
|
|
@@ -74,6 +75,13 @@ describe('handlebars-i18n Test', function() {
|
|
|
74
75
|
assert.isFunction(hI18nMod.helpers.foo);
|
|
75
76
|
});
|
|
76
77
|
|
|
78
|
+
it('after method call init(null, overrideI18n) with custom i18n Object, i18n object should have custom function foo', function() {
|
|
79
|
+
const i18nModified = require('i18next');
|
|
80
|
+
i18nModified.init({supportedLngs: ['de','en']});
|
|
81
|
+
const hI18nMod = HandlebarsI18n.init(null, i18nModified);
|
|
82
|
+
assert.isFunction(function(){}); // write a test here
|
|
83
|
+
});
|
|
84
|
+
|
|
77
85
|
|
|
78
86
|
// -- Tests for function _locale -- //
|
|
79
87
|
|