Support » Plugin: Contact Form 7 » How to set the reCaptcha language?

Viewing 13 replies - 31 through 43 (of 43 total)
  • I use WPML, this code works smoothly on my website (functions.php)

    add_action( 'wpcf7_enqueue_scripts', 'custom_recaptcha_enqueue_scripts', 11 );
    
    function custom_recaptcha_enqueue_scripts() {
        $current_lang = ICL_LANGUAGE_CODE;
    	wp_deregister_script( 'google-recaptcha' );
    
    	$url = 'https://www.google.com/recaptcha/api.js';
    	$url = add_query_arg( array(
    		'onload' => 'recaptchaCallback',
    		'render' => 'explicit',
    	 	'hl' => '$current_lang' ), $url );
    
    	wp_register_script( 'google-recaptcha', $url, array(), '2.0', true );
    }

    PS
    corrected

    add_action( 'wpcf7_enqueue_scripts', 'custom_recaptcha_enqueue_scripts', 11 );
    
    function custom_recaptcha_enqueue_scripts() {
        $current_lang = ICL_LANGUAGE_CODE;
    	wp_deregister_script( 'google-recaptcha' );
    
    	$url = 'https://www.google.com/recaptcha/api.js';
    	$url = add_query_arg( array(
    		'onload' => 'recaptchaCallback',
    		'render' => 'explicit',
    	 	'hl' => $current_lang ), $url );
    
    	wp_register_script( 'google-recaptcha', $url, array(), '2.0', true );
    }

    @takayuki Miyoshi

    Thank you very much! That was exactly what I have been looking for; relieved that I could leave the plugin’s code undisturbed.

    Cheers,
    Anna

    @kaspars

    Thanks for your plugin it works. Even with qtranslate. You have to set the parameter to:
    [:en]en[:de]de[:es]es[:]

    Then the recaptcha language will be adjusted to the current qtranslate language of your site. In my case, I have three language: english, german, and spanish.

    Best regards
    Josef

    @takayuki Miyoshi

    Thank you very much! I did not wanted to touch the plugin…
    But in any case it would be a nice option to include a language switcher.

    Thanks!
    Alain

    @zhallana

    Hi, Your code for WPML seems to work for 90% of the time, for us only the Dutch (Netherlands) part isn’t working, which is a bit odd and slightly annoying since a large part of the customer base is from the netherlands as well as the company behind the website.

    Any clue why this isn’t working ? for the other 9 languages including Chinese, Russian, French and German it is working fine.

    @ximie90
    Hi,ximie90! I’m sorry for making you waiting my reply.

    It’s very sad that this code doesn’t work with Dutch language at your website.

    I’d be happy to help you to find the reason why it is so.

    It seems that Dutch is the main language of your website (you mentioned that a large part of the customer base is from Netherlands).
    Is it true?

    What Language URL format have you chosen: domain, directory or parameter?

    Hi,
    my solution: put following code in your template functions.php to avoid problems after update. If you use polylang plugin like me, leave as it is, otherwise replace the pll_current_language() by your ‘language code’ or any variable detecting the current language.

    /*wpcf7 recapcha hooks*/

    function my_wpcf7_recaptcha_enqueue_scripts() {

    $url = ‘https://www.google.com/recaptcha/api.js’;
    $url = add_query_arg( array(
    ‘onload’ => ‘recaptchaCallback’,
    ‘render’ => ‘explicit’,
    ‘hl’ => pll_current_language()), $url );

    wp_deregister_script(‘google-recaptcha’);
    wp_register_script( ‘my-google-recaptcha’, $url, array(), ‘2.0’, true );
    wp_enqueue_script( ‘my-google-recaptcha’ );

    }

    function my_wpcf7_recaptcha_callback_script() {

    if ( ! wp_script_is( ‘my-google-recaptcha’, ‘enqueued’ ) ) {
    return;
    }

    ?>
    <script type=”text/javascript”>
    var recaptchaWidgets = [];
    var recaptchaCallback = function() {
    var forms = document.getElementsByTagName(‘form’);
    var pattern = /(^|\s)g-recaptcha(\s|$)/;

    for (var i = 0; i < forms.length; i++) {
    var divs = forms[i].getElementsByTagName(‘div’);

    for (var j = 0; j < divs.length; j++) {
    var sitekey = divs[j].getAttribute(‘data-sitekey’);

    if (divs[j].className && divs[j].className.match(pattern) && sitekey) {
    var widget_id = grecaptcha.render(divs[j], {
    ‘sitekey’: sitekey,
    ‘theme’: divs[j].getAttribute(‘data-theme’),
    ‘type’: divs[j].getAttribute(‘data-type’),
    ‘size’: divs[j].getAttribute(‘data-size’),
    ‘tabindex’: divs[j].getAttribute(‘data-tabindex’),
    ‘callback’: divs[j].getAttribute(‘data-callback’),
    ‘expired-callback’: divs[j].getAttribute(‘data-expired-callback’)
    });

    recaptchaWidgets.push(widget_id);
    break;
    }
    }
    }
    }
    </script>
    <?php
    }

    add_action( ‘wpcf7_enqueue_scripts’, ‘my_wpcf7_recaptcha_enqueue_scripts’ );
    add_action( ‘wpcf7_enqueue_scripts’, ‘my_wpcf7_recaptcha_callback_script’ );

    Contact Form 7 Controls does the trick
    https://wordpress.org/plugins/contact-form-7-extras/

    +1 vote for language code to be set on a form basis by adding a optional code that would work for most cases. i.e: [recaptcha size:compact theme:dark lang:pt-BR]
    ยป https://developers.google.com/recaptcha/docs/language

    Thanks a lot for keep updating CF7, I’ve been using it for several years and I love it. Congrats for the great work.

    Hi takayuki,

    Could you please get me how should i add filter to functions.php for render script

    +1. Please add an option to set a language. Even if auto-detect detects the browser language correctly, I don’t see a situation where it would make sense to have the reCaptcha language being different than the site language.
    Or at least let us have a way to edit the recaptcha.php code persistently, please.

    I’ve got also need to set the language per site language (multilanguage site).

    Please add WordPress filter for reCAPTCHA api arguments.
    Deregistering and registering the script again with custom argumets does the job but leaves more work to maintain a site.

    Thanks

    Alright here is alternative way to add the hl URL argument to recaptcha api script url.
    Use WordPress script_loader_src filter which is in WP_Scripts::do_item() method.

    This way you are not dependent if WPCF plugin updates unless they change the script handle from google-recaptcha to something else. You could do additional checks to the script $src or if this was a script added by WPCF7 or what ever.
    This is a simple example, put into your theme functions.php:

    
    function mytheme_wpcf7_recaptcha_set_language($src, $handle) {
        
        if ('google-recaptcha' == $handle) {
            $current_language = 'de'; 
            $src = add_query_arg('hl', $current_language, $src);
        }
     
        return $src;
    }
    add_filter( 'script_loader_src', 'mytheme_wpcf7_recaptcha_set_language', 1, 2 );
    

    Supported language codes here https://developers.google.com/recaptcha/docs/language

Viewing 13 replies - 31 through 43 (of 43 total)
  • The topic ‘How to set the reCaptcha language?’ is closed to new replies.