Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change dynamically Recaptcha V2 language

I trying to implement Recaptcha V2 on an angular site.

I'm using angular translate and I don't know how to regenerate the reCaptcha object with the correct language provided from the $scope

There is any method ? or process to reload the object with the correct lang ?

Thanks

like image 509
Rtrompier Avatar asked Mar 04 '15 13:03

Rtrompier


People also ask

How to set the initial language of reCAPTCHA with JavaScript?

In this short article, I'll explain you how to easily set the initial language of ReCaptcha or change it dynamically with JavaScript only. To simply start ReCaptcha with a specific language, the only thing you need to do is to provide the locale parameter to the request JavaScript file of ReCaptcha.

How do I use reCAPTCHA V2?

reCAPTCHA v2 1 Automatically render the reCAPTCHA widget. The easiest method for rendering the reCAPTCHA widget on your page is to include the necessary JavaScript resource and a g-recaptcha tag. 2 Explicitly render the reCAPTCHA widget. ... 3 Configuration. ...

Would you like to set the language for Google’s V2 reCAPTCHA?

Would you like to set the language for Google’s v2 reCAPTCHA? Google will try and auto-detect the language on the page when using reCAPTCHA. In some cases, its detection doesn’t work and in other use cases, you may just want to force it to something different. With a small PHP snippet, you can easily set the language inside a filter.

How to change the language of the setcaptchalang method?

The setCaptchaLang method expects 2 arguments, as first, the DOM node of the element that contains the ReCaptcha box and as second parameter, a string with the language code. It should be easy to change it from the original language (Spanish) to another language like this: You can check the official list of codes on the ReCaptcha website here.


1 Answers

Based on the limitation of the API you can load the script dynamically:

Function to load script

(source)

function loadScript(url, callback)
{
    // Adding the script tag to the head as suggested before
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // Then bind the event to the callback function.
    // There are several events for cross browser compatibility.
    script.onreadystatechange = callback;
    script.onload = callback;

    // Fire the loading
    head.appendChild(script);

   //modification so we can remove this after
   return script;
}

Note: this is a regular JS function

Load script dynamically

in your Angular controller

 $scope.loadGoogleRecaptcha = function (id, lang) {
            /// using timeout not to use $apply
            $timeout(function () {
                //set default value
                var elemntID = id || false;
                var language = lang || 'en';

                if (!elemntID) {
                    console.warn('NO Id selected for re-captcha loading!')
                    return false;
                }

                var element = document.getElementById(elemntID)
                if (angular.element(element).length == 0) {
                    console.warn('provided id doesn\'t exist ', elemntID);
                    return false;
                }

                //Delete oldscript
                if ($rootScope.addedScript)
                    angular.element($rootScope.addedScript).remove();

                //clear current recaptcha container
                angular.element(element).empty();

                var url = "https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit&hl=" + lang;

                return $window.loadScript(url, function () {
                    console.log('Scipt loaded:', url);
                })

            });

        }

init captcha with current language using angular-translate-localStorage

$rootScope.addedScript = $scope.loadGoogleRecaptcha('g-recaptcha-register' ,$window.localStorage.getItem( 'NG_TRANSLATE_LANG_KEY'));

sample of function to switch language

$rootScope.changeLanguage = function (langKey) {

        $translate.use(langKey);
        $rootScope.selectedLanguage = langKey;
        $scope.loadGoogleRecaptcha('g-recaptcha-register' , langKey);
    };

Or you could use a watcher

 $scope.$watch(
            'selectedLanguage', function (n, o) {
              //putting if to prevent double fetch the first time
              if (!!n && n != o){
                $rootScope.addedScript = $scope.loadGoogleRecaptcha('g-recaptcha-register', n);
              }
            }
        );
like image 74
Jeffrey Nicholson Carré Avatar answered Sep 21 '22 12:09

Jeffrey Nicholson Carré