wikimedia / jquery.i18n

🌐 jQuery based internationalization library
GNU General Public License v2.0
704 stars 144 forks source link

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

Open BG-Kim opened 4 years ago

BG-Kim commented 4 years ago

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.

santhoshtr commented 4 years ago

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.

BG-Kim commented 4 years ago

@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.

mooeypoo commented 4 years ago

@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"

BG-Kim commented 4 years ago

@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;