jquery.i18n icon indicating copy to clipboard operation
jquery.i18n copied to clipboard

Could you update jquery.i18n.parser.js on line 33 for $ problem

Open BG-Kim opened this issue 4 years ago • 4 comments

HTML
<div data-i18n="HOME_FOO"></div>

JSON en "HOME_FOO" : "Give me $2"

This is make warning follows :
jQuery.Deferred exception: Cannot read property '1' of undefined TypeError: Cannot read property '1' of undefined

Problem is jquery.i18n.parser.js simpleParse() on line 33

return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; => return parameters && parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; or return parameters !== undefined && parameters[ index ] !== undefined ? parameters[ index ] : '$' + match;

I guess missing to check parameter also undefined or not.

Thank you.

BG-Kim avatar Jul 27 '20 10:07 BG-Kim

In your message, where is $1? Jquery.i18n assumes placeholders in its linear order. So of you have a placeholder value "10", that is 1st placeholder indicated by $1. It does not make sense to assume it as $2.

This is the reason for the assumption of $1 in parameters array. If I add extra checks there, I am afraid it will break in other places because this isa fundamental assumption about message parameters.

santhoshtr avatar Jul 27 '20 11:07 santhoshtr

@santhoshtr : Hello, I want to write symbol '$' itself. That is not variable.

I want to write the message ‘’Korean Grandma Living On $2 A Day”. But, It made a warning with a similar error level.

I try \\$ But It’s not work. So, I open code and fix it.

PS. You know, Normally language file is written by not a programmer. And a special symbol can make errors. It’s a problem.

BG-Kim avatar Jul 29 '20 03:07 BG-Kim

@BG-Kim using the $ sign as-is inside the message would confuse the assumptions made in the system regarding variables, and as @santhoshtr points out, it's not very safe to undo this assumption.

Instead, you can use the HTML code for the $ sign (&#36;) in your message, which would render in HTML as the sign without resembling a variable. From your example, your message can be Korean Grandma Living On &#36;2 A Day which would render (even in this reply box) as "Korean Grandma Living On $2 A Day"

mooeypoo avatar Jul 29 '20 03:07 mooeypoo

@santhoshtr @mooeypoo

Hum… I think null and undefined checks are more written solid code. So, I guess the author missed it. Obviously, below code has danger when parameters is null or undefined.

return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match;

BG-Kim avatar Jul 30 '20 01:07 BG-Kim