From 68c6f90b2cca9492299d618c1678e6ef170080b6 Mon Sep 17 00:00:00 2001 From: Jan Muehlemann Date: Tue, 12 Jan 2021 16:32:18 +0100 Subject: [PATCH] change reges to fix some issue with interpolation --- CHANGELOG.md | 3 +++ README.md | 6 +++--- dist/commonjs/index.js | 2 +- dist/es/index.js | 2 +- dist/umd/i18nextIntervalPluralPostProcessor.js | 2 +- dist/umd/i18nextIntervalPluralPostProcessor.min.js | 2 +- i18nextIntervalPluralPostProcessor.js | 2 +- i18nextIntervalPluralPostProcessor.min.js | 2 +- package.json | 2 +- src/index.js | 2 +- test/interval.spec.js | 14 +++++++------- 11 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b510f00 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +### 3.0.0 + +- **BREAKING** changes regex from `/\((\S*)\).*{((.|\n)*)}/` to `/\((\S*)\).*?\[((.|\n)*)\]/` -> from (condition){value} to (condition)[value] to fix [#206](https://github.com/i18next/i18next-intervalPlural-postProcessor/issues/206) and [#221](https://github.com/i18next/i18next-intervalPlural-postProcessor/issues/221) \ No newline at end of file diff --git a/README.md b/README.md index cf772ed..a0bacc7 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,10 @@ i18next // translation: { // key1: '{{count}} item', // key1_plural: '{{count}} items', -// key1_interval: '(1){one item};(2-7){a few items};(7-inf){a lot of items};', +// key1_interval: '(1)[one item];(2-7)[a few items];(7-inf)[a lot of items];', // key2: '{{count}} item', // key2_plural: '{{count}} items', -// key2_interval: '(1){one item};(2-7){a few items};' +// key2_interval: '(1)[one item];(2-7)[a few items];' // } i18next.t('key1_interval', { postProcess: 'interval', count: 1 }); // -> one item @@ -67,7 +67,7 @@ import intervalPlural from 'i18next-intervalplural-postprocessor'; intervalPlural.setOptions({ // these are the defaults intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, // pre 3.0 /\((\S*)\).*{((.|\n)*)}/, intervalSuffix: '_interval' }); diff --git a/dist/commonjs/index.js b/dist/commonjs/index.js index bef69b1..a93c445 100644 --- a/dist/commonjs/index.js +++ b/dist/commonjs/index.js @@ -29,7 +29,7 @@ exports.default = { options: { intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, intervalSuffix: '_interval' }, diff --git a/dist/es/index.js b/dist/es/index.js index c356967..30deda9 100644 --- a/dist/es/index.js +++ b/dist/es/index.js @@ -23,7 +23,7 @@ export default { options: { intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, intervalSuffix: '_interval' }, diff --git a/dist/umd/i18nextIntervalPluralPostProcessor.js b/dist/umd/i18nextIntervalPluralPostProcessor.js index c09e639..de76f96 100644 --- a/dist/umd/i18nextIntervalPluralPostProcessor.js +++ b/dist/umd/i18nextIntervalPluralPostProcessor.js @@ -168,7 +168,7 @@ var index = { options: { intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, intervalSuffix: '_interval' }, diff --git a/dist/umd/i18nextIntervalPluralPostProcessor.min.js b/dist/umd/i18nextIntervalPluralPostProcessor.min.js index cc21ff5..b731397 100644 --- a/dist/umd/i18nextIntervalPluralPostProcessor.min.js +++ b/dist/umd/i18nextIntervalPluralPostProcessor.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.i18nextIntervalPluralPostProcessor=e()}(this,function(){"use strict";function t(t,e){if(t.indexOf("-")>-1){var n=t.split("-");if("inf"===n[1])return e>=parseInt(n[0],10);var r=parseInt(n[0],10),o=parseInt(n[1],10);return e>=r&&e<=o}return parseInt(t,10)===e}!function(){function t(t){this.value=t}function e(e){function n(o,i){try{var s=e[o](i),a=s.value;a instanceof t?Promise.resolve(a.value).then(function(t){n("next",t)},function(t){n("throw",t)}):r(s.done?"return":"normal",s.value)}catch(t){r("throw",t)}}function r(t,e){switch(t){case"return":o.resolve({value:e,done:!0});break;case"throw":o.reject(e);break;default:o.resolve({value:e,done:!1})}(o=o.next)?n(o.key,o.arg):i=null}var o,i;this._invoke=function(t,e){return new Promise(function(r,s){var a={key:t,arg:e,resolve:r,reject:s,next:null};i?i=i.next=a:(o=i=a,n(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}();var e=Object.assign||function(t){for(var e=1;e-1&&(c=r.map(function(t){return t.replace(s.options.intervalSuffix,"")})),c&&(a=i.translate(c,u))}return a||n}}}); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.i18nextIntervalPluralPostProcessor=e()}(this,function(){"use strict";function t(t,e){if(t.indexOf("-")>-1){var n=t.split("-");if("inf"===n[1])return e>=parseInt(n[0],10);var r=parseInt(n[0],10),o=parseInt(n[1],10);return e>=r&&e<=o}return parseInt(t,10)===e}!function(){function t(t){this.value=t}function e(e){function n(o,i){try{var s=e[o](i),a=s.value;a instanceof t?Promise.resolve(a.value).then(function(t){n("next",t)},function(t){n("throw",t)}):r(s.done?"return":"normal",s.value)}catch(t){r("throw",t)}}function r(t,e){switch(t){case"return":o.resolve({value:e,done:!0});break;case"throw":o.reject(e);break;default:o.resolve({value:e,done:!1})}(o=o.next)?n(o.key,o.arg):i=null}var o,i;this._invoke=function(t,e){return new Promise(function(r,s){var a={key:t,arg:e,resolve:r,reject:s,next:null};i?i=i.next=a:(o=i=a,n(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}();var e=Object.assign||function(t){for(var e=1;e-1&&(c=r.map(function(t){return t.replace(s.options.intervalSuffix,"")})),c&&(a=i.translate(c,u))}return a||n}}}); diff --git a/i18nextIntervalPluralPostProcessor.js b/i18nextIntervalPluralPostProcessor.js index c09e639..de76f96 100644 --- a/i18nextIntervalPluralPostProcessor.js +++ b/i18nextIntervalPluralPostProcessor.js @@ -168,7 +168,7 @@ var index = { options: { intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, intervalSuffix: '_interval' }, diff --git a/i18nextIntervalPluralPostProcessor.min.js b/i18nextIntervalPluralPostProcessor.min.js index cc21ff5..b731397 100644 --- a/i18nextIntervalPluralPostProcessor.min.js +++ b/i18nextIntervalPluralPostProcessor.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.i18nextIntervalPluralPostProcessor=e()}(this,function(){"use strict";function t(t,e){if(t.indexOf("-")>-1){var n=t.split("-");if("inf"===n[1])return e>=parseInt(n[0],10);var r=parseInt(n[0],10),o=parseInt(n[1],10);return e>=r&&e<=o}return parseInt(t,10)===e}!function(){function t(t){this.value=t}function e(e){function n(o,i){try{var s=e[o](i),a=s.value;a instanceof t?Promise.resolve(a.value).then(function(t){n("next",t)},function(t){n("throw",t)}):r(s.done?"return":"normal",s.value)}catch(t){r("throw",t)}}function r(t,e){switch(t){case"return":o.resolve({value:e,done:!0});break;case"throw":o.reject(e);break;default:o.resolve({value:e,done:!1})}(o=o.next)?n(o.key,o.arg):i=null}var o,i;this._invoke=function(t,e){return new Promise(function(r,s){var a={key:t,arg:e,resolve:r,reject:s,next:null};i?i=i.next=a:(o=i=a,n(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}();var e=Object.assign||function(t){for(var e=1;e-1&&(c=r.map(function(t){return t.replace(s.options.intervalSuffix,"")})),c&&(a=i.translate(c,u))}return a||n}}}); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.i18nextIntervalPluralPostProcessor=e()}(this,function(){"use strict";function t(t,e){if(t.indexOf("-")>-1){var n=t.split("-");if("inf"===n[1])return e>=parseInt(n[0],10);var r=parseInt(n[0],10),o=parseInt(n[1],10);return e>=r&&e<=o}return parseInt(t,10)===e}!function(){function t(t){this.value=t}function e(e){function n(o,i){try{var s=e[o](i),a=s.value;a instanceof t?Promise.resolve(a.value).then(function(t){n("next",t)},function(t){n("throw",t)}):r(s.done?"return":"normal",s.value)}catch(t){r("throw",t)}}function r(t,e){switch(t){case"return":o.resolve({value:e,done:!0});break;case"throw":o.reject(e);break;default:o.resolve({value:e,done:!1})}(o=o.next)?n(o.key,o.arg):i=null}var o,i;this._invoke=function(t,e){return new Promise(function(r,s){var a={key:t,arg:e,resolve:r,reject:s,next:null};i?i=i.next=a:(o=i=a,n(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}();var e=Object.assign||function(t){for(var e=1;e-1&&(c=r.map(function(t){return t.replace(s.options.intervalSuffix,"")})),c&&(a=i.translate(c,u))}return a||n}}}); diff --git a/package.json b/package.json index bcb5ea5..3ff1e6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "i18next-intervalplural-postprocessor", - "version": "2.0.2", + "version": "3.0.0", "description": "postProcessor that allows interval based plurals", "main": "./index.js", "jsnext:main": "dist/es/index.js", diff --git a/src/index.js b/src/index.js index 2c80a33..c184557 100644 --- a/src/index.js +++ b/src/index.js @@ -22,7 +22,7 @@ export default { options: { intervalSeparator: ';', - intervalRegex: /\((\S*)\).*{((.|\n)*)}/, + intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/, intervalSuffix: '_interval' }, diff --git a/test/interval.spec.js b/test/interval.spec.js index 1b46e22..949f187 100644 --- a/test/interval.spec.js +++ b/test/interval.spec.js @@ -17,31 +17,31 @@ describe('interval plural', () => { i18next.addResourceBundle('en', 'test1', { key1: '{{count}} item', key1_plural: '{{count}} items', - key1_interval: '(1){one item};(2-7){a few items};(7-inf){a lot of items};', + key1_interval: '(1)[one item];(2-7)[a few items];(7-inf)[a lot of items];', key2: '{{count}} item', key2_plural: '{{count}} items', - key2_interval: '(1){one item};(2-7){a few items};' + key2_interval: '(1)[one item];(2-7)[a few items];' }); i18next.addResourceBundle('en', 'test2', { key3: '{{count}} item', key3_plural: '{{count}} items', - key3_interval: '(1){one item};(2-7){a few items};(7-inf){a lot of items};', + key3_interval: '(1)[one item];(2-7)[a few items];(7-inf)[a lot of items];', key4: '{{count}} item', key4_plural: '{{count}} items', - key4_interval: '(1){one item};(2-7){a few items};' + key4_interval: '(1)[one item];(2-7)[a few items];' }); i18next.addResourceBundle('en', 'test3', { key3: '{{count}} item', key3_plural: '{{count}} items', - key3_interval: '(1) {one item}; (2-7) {a few items}; (7-inf) {a lot of items};', + key3_interval: '(1) [one item]; (2-7) [a few items]; (7-inf) [a lot of items];', key4: '{{count}} item', key4_plural: '{{count}} items', - key4_interval: '(1){one item}; (2-7){a few items}; ' + key4_interval: '(1)[one item]; (2-7)[a few items]; ' }); i18next.addResourceBundle('en', 'test4', { LIKE_COUNT: '{{count}} person\nlikes this', LIKE_COUNT_plura: '{{count}} people\nlike this', - LIKE_COUNT_interval: "(0){No one\nlikes this};" + LIKE_COUNT_interval: "(0)[No one\nlikes this];" }); i18next.setDefaultNamespace('test1'); });