{"id":3086,"date":"2012-05-31T22:32:08","date_gmt":"2012-06-01T05:32:08","guid":{"rendered":"http:\/\/www.analogrithems.com\/rant\/?p=3086"},"modified":"2012-05-31T22:41:48","modified_gmt":"2012-06-01T05:41:48","slug":"auto-translate-po-file-with-google-translate-v2","status":"publish","type":"post","link":"https:\/\/www.analogrithems.com\/rant\/auto-translate-po-file-with-google-translate-v2\/","title":{"rendered":"Auto Translate PO File with Google Translate v2"},"content":{"rendered":"<p>Doing translation in an application is a tough task. \u00c2\u00a0I wrote the following perl script to help the process along a bit using google translates v2 api. \u00c2\u00a0This script was written in 10 minutes and isn&#8217;t perfect but it did my whole project so I decided top share it here for others that may need it.<\/p>\n<p>This perl script requires a few modules from <a href=\"http:\/\/search.cpan.org\">CPAN<\/a> listed here<\/p>\n<ul>\n<li><a href=\"http:\/\/search.cpan.org\/~lorn\/LWP-Curl-0.10\/lib\/LWP\/Curl.pm\">LWP::Curl<\/a><\/li>\n<li><a href=\"http:\/\/search.cpan.org\/~gaas\/URI-1.60\/URI\/Escape.pm\">URI::Escape<\/a><\/li>\n<li><a href=\"http:\/\/search.cpan.org\/~makamaka\/JSON-2.53\/lib\/JSON.pm\">JSON<\/a><\/li>\n<\/ul>\n<p>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. \u00c2\u00a0The upside is, currently it doesn&#8217;t cost much. \u00c2\u00a0About $20USD for 2,000,000 characters. \u00c2\u00a0Using this script I was able to translate about 20k words in about 15min.<\/p>\n<p>&nbsp;<\/p>\n<p>To get started you must have a google account with translate api access. \u00c2\u00a0Go to the <a href=\"https:\/\/code.google.com\/apis\/console\/#\">google api console<\/a>\u00c2\u00a0Click on service an activate\u00c2\u00a0<a href=\"https:\/\/code.google.com\/apis\/console\/#project:90701216208:stats:translate\">Translate API<\/a>\u00c2\u00a0service. \u00c2\u00a0Next click on API Access and generate a Server API key and give it your static IP for it to lock access to. \u00c2\u00a0Once it generates an API key for you place that in the header of the following script in the $api_key var<\/p>\n<p>&nbsp;<br \/>\n[crayon lang=&#8221;perl&#8221;]<br \/>\n#!\/usr\/bin\/perl<\/p>\n<p>use strict;<br \/>\nuse warnings;<br \/>\nuse LWP::Curl;<br \/>\nuse URI::Escape;<br \/>\nuse JSON -support_by_pp;<\/p>\n<p>use File::Slurp;<\/p>\n<p>my $version = &#8216;0.1&#8217;;<br \/>\nmy %opt;<br \/>\nmy $api_key = &#8216;YOUR GOOGLE api v2 key here&#8217;;<br \/>\nmy $googleTransLateURL = &#8216;https:\/\/www.googleapis.com\/language\/translate\/v2&#8217;;<\/p>\n<p>sub init(){<br \/>\n        use Getopt::Std;<br \/>\n        my $opt_string = &#8216;hVl:s:f:o:&#8217;;<br \/>\n        getopts( &#8220;$opt_string&#8221;, \\%opt ) or usage();<br \/>\n        if($opt{V}){<br \/>\n                print $version.&#8221;\\n&#8221;;<br \/>\n                exit;<br \/>\n        }<\/p>\n<p>        usage() if $opt{h};<br \/>\n        usage() if !$opt{f};<br \/>\n        main();<br \/>\n}<\/p>\n<p>sub usage(){<br \/>\n        print STDERR <<\"EOF\";\nAutomated tool to take a PO file and use google translate to translate each string in the file.\n\n$0 [-V] -f <textFile><\/p>\n<p>-f      The po file you want to translate.<br \/>\n-l\tThe language you want it translated to<br \/>\n-s\tSource Language of text, defaults to en<br \/>\n-h      This Help screen<br \/>\n-V      Version<br \/>\nEOF<br \/>\nexit;<br \/>\n}<\/p>\n<p>init();<\/p>\n<p>sub main(){<\/p>\n<p>        processFile($opt{f});<br \/>\n}<\/p>\n<p>sub processFile(){<br \/>\n\tmy $file = shift();<\/p>\n<p>\tmy @poFile = read_file($file);<\/p>\n<p>\tmy $i = 0;<br \/>\n\tforeach my $line (@poFile){<br \/>\n\t\tif($line =~ m\/^msgid &#8220;&#8221;$\/){<br \/>\n\t\t\tprint $i;<br \/>\n\t\t}elsif($line =~ m\/^msgid &#8220;(.*)&#8221;$\/){<br \/>\n\t\t\tmy $translateString = &#8216;msgstr &#8220;&#8216;.transLate($1).'&#8221;&#8216;.&#8221;\\n&#8221;;<br \/>\n\t\t$i++;<br \/>\n\t\t\tprint $line;<br \/>\n\t\t\tprint $translateString;<br \/>\n\t\t}elsif($line  =~ m\/^msgstr\/){<br \/>\n\t\t\t#skipp empty string, get it from above<br \/>\n\t\t\tnext();<br \/>\n\t\t}else{<br \/>\n\t\t\t# normal line print it as usual<br \/>\n\t\t\tprint $line;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<\/p>\n<p>sub transLate(){<br \/>\n\tmy $string = shift();<\/p>\n<p>\tmy ($sLang, $targetLang, $translatedString);<\/p>\n<p>\tif(!$opt{s}){<br \/>\n\t\t$sLang = &#8216;en&#8217;<br \/>\n\t}else{<br \/>\n\t\t$sLang = $opt{s};<br \/>\n\t}<\/p>\n<p>\tif(!$opt{l}){<br \/>\n\t\tprint &#8220;ERROR: No target language was specified.\\n&#8221;;<br \/>\n\t\tusage();<br \/>\n\t}else{<br \/>\n\t\t$targetLang = $opt{l};<br \/>\n\t}<\/p>\n<p>\tmy $lwpcurl = LWP::Curl->new();<\/p>\n<p>\tmy $args = &#8216;?key=&#8217;.$api_key.&#8217;&#038;source=&#8217;.$sLang.&#8217;&#038;target=&#8217;.$targetLang.&#8217;&#038;q=&#8217;.uri_escape($string);<br \/>\n\t#for now we are testing our loop logic<br \/>\n\tmy $content = $lwpcurl->get($googleTransLateURL.$args);<br \/>\n\tmy $json = new JSON;<\/p>\n<p>\tmy $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);<\/p>\n<p>\tforeach my $trans  (@{$json_text->{data}->{translations}}){<br \/>\n\t\t#ok, get the translated string now<br \/>\n\t\t$translatedString = $trans->{translatedText};<br \/>\n\t}<\/p>\n<p>\treturn($translatedString);<br \/>\n}<br \/>\n[\/crayon]<br \/>\n&nbsp;<\/p>\n<p>If you run the script without any arguments it will give you the following usage<\/p>\n<blockquote><p>Automated tool to take a PO file and use google translate to translate each string in the file.<\/p>\n<p>.\/googleTranslate.po.pl [-V] -f <textFile><\/p>\n<p>-f      The po file you want to translate.<br \/>\n-l\tThe language you want it translated to<br \/>\n-s\tSource Language of text, defaults to en<br \/>\n-h      This Help screen<br \/>\n-V      Version\n<\/p><\/blockquote>\n<p>Here is how I use the script to generate a spanish version of my gettext po file<\/p>\n<blockquote><p>.\/googleTranslate.po.pl -f default.po -l &#8216;es&#8217; > es.po<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Doing translation in an application is a tough task. \u00c2\u00a0I wrote the following perl script to help the process along a bit using google translates v2 api. \u00c2\u00a0This script was written in 10 minutes and isn&#8217;t perfect but it did my whole project so I decided top share it here for others that may need [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[59,57,58,60],"class_list":["post-3086","post","type-post","status-publish","format-standard","hentry","category-web-development","tag-gettext","tag-i18n","tag-po","tag-translation"],"_links":{"self":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/3086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/comments?post=3086"}],"version-history":[{"count":6,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3092,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/3086\/revisions\/3092"}],"wp:attachment":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/media?parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/categories?post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/tags?post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}