Change reCaptcha language dynamically in Angular
How to setup reCaptcha in Angular using PrimeNg
This article is the continuation of an article I wrote on reCaptcha.
There I used ng-recaptcha library to setup recaptcha. This libraray is great, but the issue with it is that, the reCaptcha language change does not work all the time.
Recently I found reCaptcha support is present in Primeng library.
PrimeNg makes it very simple to setup reCaptcha.
Steps to setup reCaptcha using PrimeNg
-
Install
primeNgusing below commands,npm install primeng --save npm install primeicons --save -
Add below style in
angular.jsonas below,"styles": [ "node_modules/primeicons/primeicons.css", "node_modules/primeng/resources/themes/lara-light-blue/theme.css", "node_modules/primeng/resources/primeng.min.css", ... ] -
Once
primeNgis installed we need can addreCaptcha,-
Import below module in your
component.tsfileimport {CaptchaModule} from 'primeng/captcha'; -
Add
reCaptchatag in your component html file<p-captcha siteKey="YOUR_SITE_KEY" (onResponse)="showResponse($event)"></p-captcha> -
Add below script in index.html file
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=initRecaptcha" async defer></script> -
The generated
reCaptchacode will be available inshowResponsemethod. This code needs to sent to your backend API to complete therecaptchasecurity. -
We can change language easily by using
languageattribute as shown below,<p-captcha siteKey="YOUR_SITE_KEY" language="en" (onResponse)="showResponse($event)"></p-captcha>
That’s it!. That’s how easy it is to setup
reCaptchausingPrimeNgFor more information on
PrimeNg reCaptcharefer documentation at http://primefaces.org/primeng/captcha -