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
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.
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? 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.
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.
Based on the limitation of the API you can load the script dynamically:
(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
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);
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With