更改 ReCaptcha 语言 OnClick

Change ReCaptcha language OnClick

我意识到通过将 "hl" 选项添加到 api.js 来更改 Recaptcha 语言是微不足道的。

https://www.google.com/recaptcha/api.js?hl=fr

我想做的是在有人点击语言选择器时更改 Recaptcha 语言,该语言选择器通过 QueryString 参数(例如“?lang=fr”)公开我有 js 可以解析参数但我似乎无法在 head 标签中重新加载脚本以包含参数。

我查看了所有条件 IF...ELSE javascript 加载文章。有什么方法可以用版本 2 加载 Recaptcha 选项吗?

我认为目前无法通过 javascript 直接设置 recaptcha 语言。正如您所说,但是可以在脚本加载期间使用参数 'hl' 。

如果您需要在不重新加载页面的情况下动态更改应用程序的语言,您可以通过从 head 部分删除 recaptcha 脚本 link 来实现,而是直接通过 javascript。当您的用户通过单击按钮更改语言时,您现在可以通过再次调用加载函数以新语言重新加载验证码。

在我的例子中,recaptcha 元素显示在模式中,用户响应通过 ajax 发送到服务器,并在服务器端根据 Google 进行验证。 像这样的东西就可以了:

/* Clears recaptcha HTML element of all content, clears 
 * recaptcha element id so that the code would know to create 
 * the new HTML. Reloads recaptcha code with the new specified 
 * language using jQuery */
var captchaReload = function(langCode) {
    $('#recaptchaDiv').empty();
    recaptchaElement = null;
    var url = "https://www.google.com/recaptcha/api.js?render=explicit&hl=" + langCode;
    $.getScript(url);
};

/* Called by recaptcha when the user solves the puzzle.
 * The incoming parameter 'response' is generated by the recaptcha 
 * and needs to be validated against google separately, which 
 * is not shown here */
var captchaValidate = function(response){
    console.log('Doing captcha response: ' + response);
    grecaptcha.reset();
    $('#captchaModal').modal('hide');
    // TODO: Add server side call for validating the client response
};

/* Variable for keeping track if recaptcha has already been created */
var recaptchaElement = null;

/* Called for initializing the recaptcha element and opening the modal */
var captchaShow = function () {
    // Initialize recaptcha if it is not present yet
    if(recaptchaElement === null){
        recaptchaElement = grecaptcha.render('recaptchaDiv', {
            'sitekey'   : 'YoUrReCaPtChAsItEkEy',
            'theme'     : 'light',
            'callback'  : captchaValidate
        });
    }
    // Open captcha modal
    window.setTimeout(function() {
        $('#captchaModal').modal('show');
    },300);
};

现在,在页面加载或用户选择新语言时,您可以:

captchaReload('fr');

它应该从页面中删除现有的 recaptcha 对象并以正确的语言重新加载一个。之后,您可以使用以下方式打开模态:

captchaShow();

简单的解决方案

你可以这样做:

HTML

<div id="captcha_container"></div>
<select id="ddllanguageListsGoogleCaptcha"></select>
<input id="btnApplyLanguage" type="button" value="Apply Selected Language" />

JS

// Button for updating captcha language
$('#btnApplyLanguage').click(function () {
    updateGoogleCaptchaLanguage($('#ddllanguageListsGoogleCaptcha').val());
});

// 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)