Javascript 设置 reCAPTCHA 版本 2 设置英语以外的另一种语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28769893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:22:17  来源:igfitidea点击:

Setting reCAPTCHA Version 2 set another language other than English

javascriptrecaptcha

提问by neo

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

我在 API 参考 -> Google Docs上的版本 2 下找不到相关信息以获取 reCAPTCHA

Additional information:

附加信息:

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

我在导轨上使用它,在gem "recaptcha"这里找到

回答by LuBre

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

您只需要在脚本的 url 中指定参数“ ?hl=”:

<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

在此处找到您的语言代码:https: //developers.google.com/recaptcha/docs/language

回答by Mauro Nidola

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

如果您使用的是recaptcha gem,则需要在recaptcha_tags 中提供hl参数。

Example:

例子:

<%= recaptcha_tags ssl: true, hl: 'it', display: { theme: 'white' } %>

回答by Ali Soltani

Simple solution

简单的解决方案

You can do it like this:

你可以这样做:

HTML

HTML

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

JS

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)

在线演示 (jsFiddle)

回答by Rudy Lopes

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.

是的,“hl=语言代码”方法效果很好。当然,问题是要<script src='https://www.google.com/recaptcha/api.js'></script>对页面上的每个实例执行此操作- 页头中的实例和正文中的实例。仅将 hl=... 放在正文中会导致不一致的结果。

回答by Luckylooke

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

谢谢@ali-soltani 剪断!搞事情了!:)

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

我为那些不使用 jQuery 的人提供我的“vanilla”版本,以节省一些罢工。

    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 + '&'));
      }
    }