weixunjie / jquery-i18n-properties

Automatically exported from code.google.com/p/jquery-i18n-properties
0 stars 0 forks source link

Keys with multiple placeholders fails silently in IE8 #3

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
From Sergei:

One more issue in the code – function parseData uses array.indexOf to find 
out whether the argument is already in use or not. In IE (at least, IE8 and I 
guess in all lower versions) array doesn’t support indexOf function, so 
you’ll have to prototype it like it’s described here:
http://www.tutorialspoint.com/javascript/array_indexof.htm

Otherwise, it will fail silently when you have more than 1 placeholder and 
you’ll spend a lot of time trying to figure that out why the file is not 
loaded, just like I did 

Original issue reported on code.google.com by nfgr...@gmail.com on 22 May 2011 at 11:33

GoogleCodeExporter commented 8 years ago
Just bitten by this issue too (IE9). Since we already depend on jQuery, it is 
also possible to use $.inArray() here.

Original comment by d.de...@gmail.com on 24 Oct 2011 at 12:59

GoogleCodeExporter commented 8 years ago
I'm using the library and it works fine in all browsers except IE9. The error 
is SCRIPT5007: Unable to get value of the property 'howdoesitworks': object is 
null or undefined 

I've read some issues related this problem but I didnt solve yet. Now looking 
this issue, I think it's related. The 'howdoesitworks' in the error means the 
first line of my property file.

I really don't know what's happing. Any one could help me?

Thanks.

Elias.

Original comment by elias.qu...@gmail.com on 25 Apr 2012 at 3:36

GoogleCodeExporter commented 8 years ago
Yep, that's not a very good part of this plugin. Sry guys.. ;)

There are two main problems
 - IE<9 has no support for Array.indexof so all calls using this will fail => use jQuery.inArray instead
 - IE has a problem that property's parts matches the "list of keywords" will fail

I changes "checkKeyNamespace" using the array-access style instead of the 
previous dot-usages. Additionally, the function returns the complete 
"replacement" as a string.

This follows a guided patch

About l. 307, find
    if(regPlaceHolder.test(parts[p]) && (usedArgs.length == 0 || usedArgs.indexOf(parts[p]) == -1)) {

Replace with
  if(regPlaceHolder.test(parts[p]) && (usedArgs.length == 0 || $.inArray(parts[p], usedArgs))) {

Later, about l. 314, find
  parsed += name + '=function(' + fnArgs + '){';

replace with
  parsed += checkKeyNamespace(name) + '=function(' + fnArgs + '){';

Later, about l. 321, find
  parsed += name+'="'+value+'";';

replace with
  parsed += checkKeyNamespace(name)+'="'+value+'";';

Later, about l. 338 find
function checkKeyNamespace(key) {
    var regDot = /\./;
    if(regDot.test(key)) {
        var fullname = '';
        var names = key.split( /\./ );
        for(var i=0; i<names.length; i++) {
            if(i>0) {fullname += '.';}
            fullname += names[i];
            if(eval('typeof '+fullname+' == "undefined"')) {
                eval(fullname + '={};');
            }
        }
    }
}

Replace with 
function checkKeyNamespace(key) {
    var regDot = /\./;
    var fullname = '';
    if(regDot.test(key)) {
        var names = key.split( /\./ );
        for(var i=0; i<names.length; i++) {
            if(i>0) {
                fullname += '["'+names[i]+'"]';
            } else {
                fullname += names[i];
            }
            if(eval('typeof '+fullname+' == "undefined"')) {
                eval(fullname + '={};');
            }
        }
    } else {
        fullname = key;
    }

    return fullname;
}

Original comment by knallisw...@googlemail.com on 16 May 2012 at 11:09