How to : Google Translate API With Perl

So, without boring introduction.

But you need to install some library for Perl as well by using cpan.

1
2
cpan install JSON
cpan install REST::Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl

use REST::Client;
use JSON;
use HTML::Entities qw(decode_entities);

while (1)
{
	print "Please enter English word: ";
	chop ( $_ = );

	my $client = REST::Client->new();
	$client->GET("https://www.googleapis.com/language/translate/v2?key=AIzaSyACJybEm6lyelnYHZzXfydtk-V6-Uz48bQ&en&target=fr&q=$_");
	$response = $client->responseContent();

	$json_text = from_json( $response );

	foreach my $french(@{$json_text->{data}->{translations}})
	{
		my %ep_hash = ();
		$ep_hash{translatedText} = "Translated to French: $french->{translatedText}";

# print french
		while (my($key, $value) = each (%ep_hash))
		{
			print decode_entities($value);
		}
	}
	print "\n";
}
Feb 17th, 2011

Comments