52

enter image description here

How can I set this in another language, ie:. French

I've tried:

var RecaptchaOptions = {
     lang : 'fr',
  };

Found above here

Which does nothing.

I couldn't find relevant info under API Reference -> Version 2 on Google Docs for reCAPTCHA

Additional information:

I'm using this on rails, with gem "recaptcha" Found here

JEM
  • 68
  • 1
  • 7
neo
  • 4,078
  • 4
  • 25
  • 41

5 Answers5

145

You just need to specify the parameter "?hl=" in the script's url:

<script src='https://www.google.com/recaptcha/api.js?hl=fr'></script>

Not very well documented, indeed!

find your language code here: https://developers.google.com/recaptcha/docs/language

Community
  • 1
  • 1
LuBre
  • 1,789
  • 2
  • 13
  • 9
  • [This seems to be the list of available language codes](https://developers.google.com/recaptcha/docs/language). – Uwe Keim May 23 '16 at 05:57
  • @ Syed - Where do you ge the language code from? Database? Query? And what language are you working with... PHP, ASP, ... – LuBre Jul 05 '17 at 17:19
15

If you are using the recaptcha gem you need to provide the hl param in recaptcha_tags.

Example:

<%= recaptcha_tags ssl: true, hl: 'it', display: { theme: 'white' } %>
Mauro Nidola
  • 468
  • 4
  • 14
5

Simple solution

You can do it like this:

HTML

<div id="captcha_container"></div>
<select id="ddllanguageListsGoogleCaptcha"></select>

JS

// Update language captcha 
function updateGoogleCaptchaLanguage(selectedLanguage) {

    // Get GoogleCaptcha iframe
    var iframeGoogleCaptcha = $('#captcha_container').find('iframe');

    // Get language code from iframe
    var language = iframeGoogleCaptcha.attr("src").match(/hl=(.*?)&/).pop();

    // Get selected language code from drop down
    var selectedLanguage = $('#ddllanguageListsGoogleCaptcha').val();

    // Check if language code of element is not equal by selected language, we need to set new language code
    if (language !== selectedLanguage) {
        // For setting new language 
        iframeGoogleCaptcha.attr("src", iframeGoogleCaptcha.attr("src").replace(/hl=(.*?)&/, 'hl=' + selectedLanguage + '&'));
    }
}

Online demo (jsFiddle)

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
3

Yes, the "hl=language code" approach works well. The catch, of course, is to do this to EVERY instance of <script src='https://www.google.com/recaptcha/api.js'></script> on the page - both the one in the page head AND the one in the body. Only putting hl=... in the body leads to inconsistent results.

Rudy Lopes
  • 31
  • 5
1

Thank you @ali-soltani for snipped! Did the thing! :)

I am providing my "vanilla" version for those who do not use jQuery, to save the few strikes.

    function setCaptchaLang(lang) {

      const container = document.getElementById('captcha_container');

      // Get GoogleCaptcha iframe
      const iframeGoogleCaptcha = container.querySelector('iframe');

      // Get language code from iframe
      const actualLang = iframeGoogleCaptcha.getAttribute("src").match(/hl=(.*?)&/).pop();

      // For setting new language
      if (actualLang !== lang) {
        iframeGoogleCaptcha.setAttribute("src", iframeGoogleCaptcha.getAttribute("src").replace(/hl=(.*?)&/, 'hl=' + lang + '&'));
      }
    }
Luckylooke
  • 4,061
  • 4
  • 36
  • 49