Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix incompatiblites to l20n's beta 1 version #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions build/react-l20n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -72,32 +72,50 @@ 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];
// By dependency [email protected]; 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),
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);
}
/*
Since there are now two call, I made it a member function.
*/

}, {
key: 'stripFromBlacklistedChars',
value: function stripFromBlacklistedChars(message) {
var blacklistedCharCodes = [8296, 8297]; // What are these chars? Cannot print them

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 } });
}
}, {
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) {
Expand All @@ -123,7 +141,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, [{
Expand Down
28 changes: 24 additions & 4 deletions src/react-l20n.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,36 @@ class L20n
return this.getRaw(key, props, this.defaultLocale);
}

var [ message, errors ] = ctx.format(template, props);
// By dependency [email protected]; 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 [ 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)
Expand Down