thiagocavaloti / recaptcha

Automatically exported from code.google.com/p/recaptcha
0 stars 0 forks source link

Java lib should allow custom translations #86

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Build properties for RecaptchaOptions (js)
---------------------
Properties levl1 = new Properties();
levl1.setProperty("theme", "white");

Properties levl2 = new Properties();
levl2.setProperty("instructions_visual", messages.get("captcha.enterwords"));           
levl2.setProperty("refresh_btn", messages.get("captcha.reload"));           
levl2.setProperty("audio_challenge", messages.get("captcha.getsound"));         
levl2.setProperty("visual_challenge", messages.get("captcha.getimage"));            
levl2.setProperty("help_btn", messages.get("help"));            
levl2.setProperty("play_again", messages.get("captcha.replay"));            
levl2.setProperty("cant_hear_this", messages.get("captcha.download"));          
levl2.setProperty("instructions_audio", messages.get("captcha.entersound"));            
levl2.setProperty("instructions_context", "instructions_context");          
levl2.setProperty("incorrect_try_again", "incorrect_try_again");    

levl1.put("custom_translations", levl2);
------------------------------

2. captcha().createRecaptchaHtml(null, levl1);
3. Review generated javascript

What is the expected output? What do you see instead?
Expected:
var RecaptchaOptions = 
{theme:'white',custom_translations:{audio_challenge:'Afspil 
lydfil',instructions_context:'instructions_context',cant_hear_this:'Problemer 
med afspilling? Hent som mp3.',play_again:'Hør lydfilen 
igen.',instructions_visual:'Indtast ordene ovenfor 
',instructions_audio:'Indtast de ord du hører',refresh_btn:'Prøv nye 
ord',help_btn:'Hjælp',visual_challenge:'Tilbage til 
tekst',incorrect_try_again:'incorrect_try_again'}}; 

I see: var RecaptchaOptions = {theme:'white',custom_translations:'null'};

What version of the product are you using? On what operating system?
v0.0.7

Please provide any additional information below.
The library does not account for complex/nested json object. Probably this has 
been introduced later - and the java lib not updated accordingly.

What I wanted to do was to extend ReCaptchaImpl and override fetchJSOptions. 
This is not possible as it's a private method. So I patched and rebuilt 
ReCaptchaImpl.

From patched ReCaptchaImpl
----
for (Enumeration e = properties.keys(); e.hasMoreElements(); ) {
    String property = (String) e.nextElement();

    Object v = properties.get(property);
    if(v instanceof String)
        jsOptions += property + ":'" + v +"'";
    else if(v instanceof Properties) {
        Properties sub = (Properties)v;
        String s = "{";
        Iterator<Entry<Object, Object>> it = sub.entrySet().iterator();
        while(it.hasNext()) {
            Entry<Object, Object> el = it.next();
            s += el.getKey() + ":'" + el.getValue() +"'";
            if(it.hasNext())
                s += ",";
        }
        s += "}";
        jsOptions += property + ":" + s;
    }

    if (e.hasMoreElements()) {
        jsOptions += ",";
    }   
}
----
(This is a crude implementation and could be improved further by adding 
automatic js string escapes)

Original issue reported on code.google.com by baard.ma...@gmail.com on 8 Oct 2010 at 8:00