Home

Noppanit

17 Feb 2011

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.

cpan install JSON
cpan install REST::Client
#!/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";
}

Til next time,
noppanit at 00:00

scribble