Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to specify display language of widget #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/Captcha/reCAPTCHA/V2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,37 @@ web application.

Creates a new instance of Captcha::reCAPTCHA::V2.

my $rc = Captcha::reCAPTCHA::V2->new;
my $rc = Captcha::reCAPTCHA::V2->new(hl => en);

Parameters:

=over 4

=item * C<$hl>

Specific language to render widget. Default value is C<'en'>.
Language options see L<Language codes|https://developers.google.com/recaptcha/docs/language>.

=back

=back

=cut

sub new {
my $class = shift;
my ($class, %options) = @_;
my $self = bless {}, $class;

my $hl = $options{hl} || 'en';

# Initialize the user agent object
$self->{ua} = HTTP::Tiny->new(
agent => 'Captcha::reCAPTCHA::V2/'.
($Captcha::reCAPTCHA::V2::VERSION || 0) . ' (Perl)'
);

$self->{widget_api} = 'https://www.google.com/recaptcha/api.js?'.
'onload=onloadCallback&render=explicit';
'onload=onloadCallback&render=explicit&hl='.$hl;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Why not only concatenate $hl only if defined so you let captcha widget guess the language?
Like:
'onload=onloadCallback&render=explicit' . defined($hl) ? "&hl=$hl" : '';


$self->{verify_api} = 'https://www.google.com/recaptcha/api/siteverify';

Expand Down
15 changes: 14 additions & 1 deletion t/01-html.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ my $html = $rc2->html(
}
);

my $captcha_widget_html = $grecaptcha_script . q^<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" type="text/javascript"></script><div id="recaptcha_ThisIsASit"></div>^;
my $captcha_widget_html = $grecaptcha_script . q^<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit&hl=en" type="text/javascript"></script><div id="recaptcha_ThisIsASit"></div>^;

is( $html, $captcha_widget_html, 'get correct html to render the widget' );
like( $html, qr/hl=en/, 'get correct default display language is "en"' );

$rc2 = Captcha::reCAPTCHA::V2->new(hl => 'th');
$html = $rc2->html(
'ThisIsASitekey',
{
theme => 'light',
type => 'image',
size => 'normal',
}
);

like( $html, qr/hl=th/, 'get specific language to render widget is "th"' );

done_testing