Home » Web Development » Auto Translate PO File with Google Translate v2

Doing translation in an application is a tough task.  I wrote the following perl script to help the process along a bit using google translates v2 api.  This script was written in 10 minutes and isn’t perfect but it did my whole project so I decided top share it here for others that may need it.

This perl script requires a few modules from CPAN listed here

For this script to be useful for you, you need to have a google api v2 API key from google. The new v2 api requires you to pay for translations.  The upside is, currently it doesn’t cost much.  About $20USD for 2,000,000 characters.  Using this script I was able to translate about 20k words in about 15min.

 

To get started you must have a google account with translate api access.  Go to the google api console Click on service an activate Translate API service.  Next click on API Access and generate a Server API key and give it your static IP for it to lock access to.  Once it generates an API key for you place that in the header of the following script in the $api_key var

 
[crayon lang=”perl”]
#!/usr/bin/perl

use strict;
use warnings;
use LWP::Curl;
use URI::Escape;
use JSON -support_by_pp;

use File::Slurp;

my $version = ‘0.1’;
my %opt;
my $api_key = ‘YOUR GOOGLE api v2 key here’;
my $googleTransLateURL = ‘https://www.googleapis.com/language/translate/v2’;

sub init(){
use Getopt::Std;
my $opt_string = ‘hVl:s:f:o:’;
getopts( “$opt_string”, \%opt ) or usage();
if($opt{V}){
print $version.”\n”;
exit;
}

usage() if $opt{h};
usage() if !$opt{f};
main();
}

sub usage(){
print STDERR <<"EOF"; Automated tool to take a PO file and use google translate to translate each string in the file. $0 [-V] -f

-f The po file you want to translate.
-l The language you want it translated to
-s Source Language of text, defaults to en
-h This Help screen
-V Version
EOF
exit;
}

init();

sub main(){

processFile($opt{f});
}

sub processFile(){
my $file = shift();

my @poFile = read_file($file);

my $i = 0;
foreach my $line (@poFile){
if($line =~ m/^msgid “”$/){
print $i;
}elsif($line =~ m/^msgid “(.*)”$/){
my $translateString = ‘msgstr “‘.transLate($1).'”‘.”\n”;
$i++;
print $line;
print $translateString;
}elsif($line =~ m/^msgstr/){
#skipp empty string, get it from above
next();
}else{
# normal line print it as usual
print $line;
}
}
}

sub transLate(){
my $string = shift();

my ($sLang, $targetLang, $translatedString);

if(!$opt{s}){
$sLang = ‘en’
}else{
$sLang = $opt{s};
}

if(!$opt{l}){
print “ERROR: No target language was specified.\n”;
usage();
}else{
$targetLang = $opt{l};
}

my $lwpcurl = LWP::Curl->new();

my $args = ‘?key=’.$api_key.’&source=’.$sLang.’&target=’.$targetLang.’&q=’.uri_escape($string);
#for now we are testing our loop logic
my $content = $lwpcurl->get($googleTransLateURL.$args);
my $json = new JSON;

my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);

foreach my $trans (@{$json_text->{data}->{translations}}){
#ok, get the translated string now
$translatedString = $trans->{translatedText};
}

return($translatedString);
}
[/crayon]
 

If you run the script without any arguments it will give you the following usage

Automated tool to take a PO file and use google translate to translate each string in the file.

./googleTranslate.po.pl [-V] -f

-f The po file you want to translate.
-l The language you want it translated to
-s Source Language of text, defaults to en
-h This Help screen
-V Version

Here is how I use the script to generate a spanish version of my gettext po file

./googleTranslate.po.pl -f default.po -l ‘es’ > es.po

Leave a Reply

Your email address will not be published. Required fields are marked *

</Random> is Stephen Fry proof thanks to caching by WP Super Cache