From c7d733541e091cc053b8861bf9539020e2f7f53e Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 13 Feb 2017 11:52:32 +0100 Subject: [PATCH 1/2] fix incompatiblites to l20n's beta 1 version --- build/react-l20n.js | 32 +++++++++++++++++++++----------- src/react-l20n.js | 26 ++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/build/react-l20n.js b/build/react-l20n.js index 8b9cc44..00339a7 100644 --- a/build/react-l20n.js +++ b/build/react-l20n.js @@ -54,7 +54,7 @@ var L20n = function () { }, { key: 'getRaw', value: function getRaw(key, props) { - var locale = arguments.length <= 2 || arguments[2] === undefined ? this.defaultLocale : arguments[2]; + var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale; var ctx = this.contexts.get(locale); if (!ctx) { @@ -72,24 +72,34 @@ var L20n = function () { return this.getRaw(key, props, this.defaultLocale); } - var _ctx$format = ctx.format(template, props); - - var _ctx$format2 = _slicedToArray(_ctx$format, 2); - - var message = _ctx$format2[0]; - var errors = _ctx$format2[1]; + if (props === null && typeof template === "string") return this.stripFromBlacklistedChars(template); + var _ctx$format = ctx.format(template, props), + _ctx$format2 = _slicedToArray(_ctx$format, 2), + message = _ctx$format2[0], + errors = _ctx$format2[1]; if (errors.length > 0) { return undefined; } - return message.replace(String.fromCharCode(8296), '').replace(String.fromCharCode(8297), ''); + return this.stripFromBlacklistedChars(message); + } + }, { + key: 'stripFromBlacklistedChars', + value: function stripFromBlacklistedChars(message) { + var blacklistedCharCodes = [8296, 8297]; + + blacklistedCharCodes.forEach(function (charCode) { + message = message.replace(String.fromCharCode(charCode), ''); + }); + + return message; } }, { key: 'get', value: function get(key, props) { - var locale = arguments.length <= 2 || arguments[2] === undefined ? this.defaultLocale : arguments[2]; + var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale; var message = this.getRaw(key, props, locale); return _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: message } }); @@ -97,7 +107,7 @@ var L20n = function () { }, { key: 'getContext', value: function getContext() { - var locale = arguments.length <= 0 || arguments[0] === undefined ? this.defaultLocale : arguments[0]; + var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.defaultLocale; var ctx = this.contexts.get(locale); if (!ctx) { @@ -123,7 +133,7 @@ var L20nElement = exports.L20nElement = function (_React$Component) { function L20nElement(props) { _classCallCheck(this, L20nElement); - return _possibleConstructorReturn(this, Object.getPrototypeOf(L20nElement).call(this, props)); + return _possibleConstructorReturn(this, (L20nElement.__proto__ || Object.getPrototypeOf(L20nElement)).call(this, props)); } _createClass(L20nElement, [{ diff --git a/src/react-l20n.js b/src/react-l20n.js index 71adb00..3013d65 100644 --- a/src/react-l20n.js +++ b/src/react-l20n.js @@ -50,16 +50,34 @@ class L20n return this.getRaw(key, props, this.defaultLocale); } - var [ message, errors ] = ctx.format(template, props); + // By dependency l20n@4.0.0-beta.1; ctx.format() may return other types than + // the object expected, so no parameters message, errors can be extracted. + // This will intercept the attempt by checking if only a string was passed, + // returning it the usual way. + if (props === null && typeof template === "string") + return this.stripFromBlacklistedChars(template); + + var [ message, errors ] = ctx.format(template, props); if (errors.length > 0) { return undefined; } - return message - .replace(String.fromCharCode(8296), '') - .replace(String.fromCharCode(8297), '') + return this.stripFromBlacklistedChars(message); } + /* + Since there are now two call, I made it a member function. + */ + stripFromBlacklistedChars(message) + { + const blacklistedCharCodes = [8296, 8297]; // What are these chars? Cannot print them + + blacklistedCharCodes.forEach(charCode => { + message = message.replace(String.fromCharCode(charCode), '') + }); + + return message; + } get(key, props, locale = this.defaultLocale) { var message = this.getRaw(key, props, locale) From e35635331f89245252ba46a835b3a06b7bb1ec29 Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 13 Feb 2017 14:04:22 +0100 Subject: [PATCH 2/2] fix bug where no match could be found for entity --- build/react-l20n.js | 12 ++++++++++-- src/react-l20n.js | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/build/react-l20n.js b/build/react-l20n.js index 00339a7..b6d2864 100644 --- a/build/react-l20n.js +++ b/build/react-l20n.js @@ -72,7 +72,11 @@ var L20n = function () { return this.getRaw(key, props, this.defaultLocale); } - if (props === null && typeof template === "string") return this.stripFromBlacklistedChars(template); + // By dependency l20n@4.0.0-beta.1; ctx.format() may return other types than + // the object expected, so no parameters message, errors can be extracted. + // This will intercept the attempt by checking if only a string was passed, + // returning it the usual way. + if (!template) return undefined;else if (props === null && typeof template === "string") return this.stripFromBlacklistedChars(template); var _ctx$format = ctx.format(template, props), _ctx$format2 = _slicedToArray(_ctx$format, 2), @@ -85,10 +89,14 @@ var L20n = function () { return this.stripFromBlacklistedChars(message); } + /* + Since there are now two call, I made it a member function. + */ + }, { key: 'stripFromBlacklistedChars', value: function stripFromBlacklistedChars(message) { - var blacklistedCharCodes = [8296, 8297]; + var blacklistedCharCodes = [8296, 8297]; // What are these chars? Cannot print them blacklistedCharCodes.forEach(function (charCode) { message = message.replace(String.fromCharCode(charCode), ''); diff --git a/src/react-l20n.js b/src/react-l20n.js index 3013d65..70fcb64 100644 --- a/src/react-l20n.js +++ b/src/react-l20n.js @@ -54,7 +54,9 @@ class L20n // the object expected, so no parameters message, errors can be extracted. // This will intercept the attempt by checking if only a string was passed, // returning it the usual way. - if (props === null && typeof template === "string") + if (!template) + return undefined; + else if (props === null && typeof template === "string") return this.stripFromBlacklistedChars(template); var [ message, errors ] = ctx.format(template, props);