Force language of Widget so it reacts to Language Switcher on Multi-lang sites
-
Hi Support
I have a couple of multilingual sites where, after the page has downloaded, the user can select another language with a language switcher pull down menu on the page. The page content then changes to the selected language. But the Recaptcha v2 widget does not change, ie remains with showing “I am not a Robot”
I have noticed that the widget does accept the browser configured preferred language, and so on initial page download it is in that language. So this is OK for a single language site in any language.
But for a multilingual site you need to be able to change the language of the page after initial download, so that person can view the site in a lang other than the browser preferred one, by the user choosing the language with an on page language switcher. For example I am using the multi-lang plugin WPML which has this. But the lang of the recaptcha v2 widget does not change
I notice that on the Google developer page for the recaptcha v2 ( https://developers.google.com/recaptcha/docs/display#config) you can force the language with url param ‘hl’ set to the desired language code.
And it looks like setting this to the value returned from the WP core function get_locale() seems to do the trick in all cases –
a) Just single lang website, default en_US
b) A single website in any lang – The admin can set the lang in the
WP backend > Settings > General
c) A multilang website – the locale gets updated after the user chooses
another lang with the in-page lang switcher – at least with plugin WPML,
but I have not tried other multilang plugins, eg Polylang etcSo I was wondering if this lang code param could be added in on a next version ? – It does not require any extra external config from the user ..
And you already have made the error message ‘Please verify that you are not a robot’ translation ready with gettext fn call esc_html__( ‘Please verify that you are not a robot.’, ‘wpcf7-recaptcha’ )
Meanwhile I have implemented this myself, by removing your action which adds in the google code, and defining and adding a custom action in my theme’s functions.php file, where this is exactly the same action function (copied from the original), but with the language param added in :
CODE START :
remove_action( 'wpcf7_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts' ); add_action( 'wpcf7_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts_custom' ); function iqfix_wpcf7_recaptcha_enqueue_scripts_custom() { $hl = get_locale(); // NEW $url = 'https://www.google.com/recaptcha/api.js'; $url = add_query_arg( array( 'hl' => $hl, // NEW 'onload' => 'recaptchaCallback', 'render' => 'explicit', ), $url ); wp_register_script( 'google-recaptcha', $url, array(), '2.0', true ); wp_localize_script( 'google-recaptcha', 'wpcf7iqfix', array( 'recaptcha_empty' => esc_html__( 'Please verify that you are not a robot.', 'wpcf7-recaptcha' ), ) ); }
Seems to work for me .. Any views ?
Regards aldebaran57
- The topic ‘Force language of Widget so it reacts to Language Switcher on Multi-lang sites’ is closed to new replies.