Unofficial Google Translate API
API, Translate November 17th, 2006 - By HaochiThe other day Philipp Lenssen predicted that Google will have an API for Google Translate by 2009. “[Spanish of this blog was] automatically translated with the terrific Google Translation API. Several native speakers/ readers have told me the translations are absolutely fluent, except for the occasional weirdness due to cultural differences, or some very rare grammar hiccups.” he said.
I think that creating a Google Translate API would not just be cool, but extremely useful. Two days later, I start coding, clackety-clack, clackety-clack… Here I present you the beta version of the uOGT API, short for unOfficial Google Translate API.
Unlike others, uOGT allows users to translate selections of text by including them in a SPAN tag and with class “t”. For example,<span class="t">something to translate</span>. Of course, like most of Google’s web APIs(though this is unofficial), this one requires you to include a JavaScript file to the pages that you want to translate as well(you will also need to upload a server-side file). So is it going to take longer to download the pages that included with the JS file? No, it won’t, because the file is so small - less than 800 bytes, in my example’s setting… (Google’s homepage is 10,000 bytes as today, including images)
To run uOGT, your server needs to have PHP version >=4.3 or 5 installed. The client-side script will work on most of the modern browsers(Firefox, Opera, etc…), except Internet Explorer(I am NOT anti-IE, I don’t know why it doesn’t work… I am working to resolve this issue).
You can download the file here[zip]. After extracting the files on the computer, you can edit the value of LP and URL in file “api.js”: LP is the language pairs to be translate, and the URL property should be the relative URL(which works better) for the server-side file, “translation.php”. Upload all the files, and run “example.html” in a browser, such as Firefox.
You can find a demonstration on the project page, as well as a simple example. This is pretty much it… Tell me what you think. :D
[inspiration and the image from Google Blogoscoped, Google logo is a trademark of Google, Inc]
UPDATE: The development of this project is no longer active, if anyone wants to continue the development, let me know.
UPDATE 2: There’s an official Translate API, but with a lot of restrictions. Also, Amos Vryhof is now responsible for the development of this unofficial API. You can find the latest fix on Google Code. Thanks Amos. :)



November 18th, 2006 at 12:47 am
There are errors when I double-click (firefox) on the links within the “example page” :
Erreur : [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]” nsresult: “0×80040111 (NS_ERROR_NOT_AVAILABLE)” location: “JS frame :: http://www.googlified.com/api/api.js :: anonymous :: line 1″ data: no]
Fichier source : http://www.googlified.com/api/api.js
Ligne : 1
Erreur : uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]” nsresult: “0xc1f30001 (NS_ERROR_NOT_INITIALIZED)” location: “JS frame :: http://www.googlified.com/api/api.js :: g :: line 1″ data: no]
November 18th, 2006 at 7:05 am
You can’t run the file locally… It must be on a web server…
November 18th, 2006 at 9:59 am
OK I understand now, I click too quickly, just as a double click.
November 18th, 2006 at 10:14 am
lol. :D
November 25th, 2006 at 4:54 am
The API isn’t working with non-latian languages, (eg. Arabic (ar) ) ?
November 25th, 2006 at 8:28 am
That’s one of the limitation… It works with Chinese the first time I test it… Well, these limitation should be overcome by the next release. :D
December 12th, 2006 at 9:31 am
Hey Haochi - very cool work. I read Philipp’s article and was thinking the exact same thing. I’ll download and give it a go - and report back how well it works with English to Russian (and vise versa).
Cheers
Nick
December 27th, 2006 at 1:39 am
Regarding non-latin Language support I rewrote your file translate.php to support Japanese. Additions will need to be added for other languages but from this I think you will get the idea of how to do it.
function translate_language($from, $to, $text) {
$lp = strtolower($from).”|”.strtolower($to);
if ($from = ‘ja’) {
$in = mb_convert_encoding($text, “SJIS”, “UTF-8″);
}
$in = file_get_contents(”http://google.com/translate_t?langpair=”.urlencode($lp).”&text=”.urlencode($text));
if ($to = ‘ja’) {
$in = mb_convert_encoding($in, “UTF-8″, “SJIS”);
}
$out = ereg_replace(”.*<div id=result_box dir=ltr>”,”,$in);
$out = ereg_replace(”</div>.*”,”,$out);
return $out;
}
// $lp is now broken into two parts you can sent that way or use explode to break it up…
translate_language($_GET['from']), $_GET['to']), $_GET['text']))
Peace,
- Uili
December 27th, 2006 at 1:40 am
OOPS!!
change the line
$out = ereg_replace(â€</div>.*â€,’’,$out);
to
$out = ereg_replace(â€</div>.*â€,’’,$out);
December 27th, 2006 at 9:45 am
Thanks you VERY much. I will test it out. :D
December 27th, 2006 at 10:01 am
I got it to work in IE by replacing:
const URL = "translate.php";
with:
var URL = "translate.php";
I also modified your AJAX code a little to check for Msxml2.XMLHTTP also. Thanks a lot for the code though, it was just what I was looking for!
December 27th, 2006 at 3:35 pm
Well, looks like I have to mess with the code a little bit before releasing a working non-Latin version since I have never worked with other language before. lol. Just be patient if you need it. :) Subscribe to Googlified to keep track of it. <- Not so evil advertising the site. hehe
January 11th, 2007 at 9:13 am
Andy,
Did you make any other changes to get this working on IE? I changed the const to var, and still no dice. Works fine in FF, with and without the change.
Thanks,
JRA
April 24th, 2007 at 7:09 am
Guys can any one help me with this?
i tried the same process with java used standard request response mechanism sense i wanted every line to be separately translated (number of request = number of line)
and google most probably for this reason isnt allowing me to use
String url = “http://translate.google.com/translate_t”;
PostMethod post = new PostMethod(url);
post.setRequestBody(”text=”+enc+”&langpair=ja|en”); any more
returning string “empty” as result.
and when i try accessing it from web url it says they detected a virus of some sort on my pc …. so is there any way i can talk to them and explain that is just for this stupid document i need and i mean seriously mean NEED this process to be completed with out being kicked out.
:(
April 27th, 2007 at 7:05 am
Interesting stuff! Two small things:
1. I think it’s better to skip the use of the “const” keyword. Someone mentioned this caused troubles for Internet Explorer, but it also causes parse errors in Safari. I just replaced it with “var”.
2. I’d advice against using PHPs “short open tags”, since it’s not enabled by default. I just changed <? with <?php and it worked on my local test server as well.
April 27th, 2007 at 7:08 am
By the way, Hamdan. As far as I can see, this JavaScript solution uses a GET request to talk to Google’s translation service. From what I can see in your code, it seems you use POST, which could be why you get no answer from Google.
June 7th, 2007 at 7:02 pm
Hey dude…
This is way too cool…God bless developers like you…
Feel free to add me to your mailing list.
Cheerio.
June 19th, 2007 at 8:54 am
Hello! Thanks for the codebit!
I used a part of it to perform a text-translation without the need of ajax.
$lp = “fr|en”;
$in = $file_get_contents(”http://google.com/translate_t?langpair=”.urlencode($lp).”&text=”.urlencode($text_to_translate)) or die(”false”);
preg_match(’/(.*?)/’, $in, $out);
$text_translated = utf8_encode($out[1]);
It works nicely on my localhost but not on a distant web server. It seems that it can’t get the contents of the file from Google since it dies ‘false’.
Have you got any ideas why it doesn’t work?
Thanks in advance and keep up the good work!
June 19th, 2007 at 10:13 am
My bad, I forgot to enable allow_url_fopen in php.ini (which is not a good idea for security reasons). Unfortunately, there’s no other way to use this API, I guess :(
June 28th, 2007 at 3:04 am
Translated.net has a translation API both for human and machine translation, charset convertion, translation memory access, terminology extraction and language identification (some not documented but supported).
http://www.translated.net/en/hts.php
The machine translation has a cost around 0,01 USD per translation.
July 3rd, 2007 at 6:22 am
Does this API supporting translate Chinese, Korean or others non-latin language from/to english ?
July 6th, 2007 at 10:44 am
Yes,
a sample call to say Hello World in Korean
http://www.translated.net/hts/?f=mt&s=en&t=ko&text=hello+world&cid=htsdemo&p=htsdemo5
(all in one line…)
it returns
1 (works well)
OK (err mesg)
492 (credits)
???? ?? (Machine translated text)
July 6th, 2007 at 11:44 am
Hey Marco,
Your API seems not free, right ?
Does Internet has a Free translation API ?
August 22nd, 2007 at 7:26 am
Thanks guys!
I have another kind of need.
I have several webpages(.html or other) on my local PC, and I
want to use the script to translate them by google translator.
The uOGT here just can translate the ‘text’.
So how to modify these scripts to meet my need?
September 4th, 2007 at 4:29 pm
Hi,,
i have “false” everytime :(
???
September 17th, 2007 at 11:52 pm
This is brilliant!
RD
September 26th, 2007 at 7:44 pm
Since Development is officially no longer active, I have asked for SVN access, and have been patching and fixing code.
Please use the code in SVN at http://code.google.com/p/uogt/
UILI, I can’t seem to get your patch to work, something seems to have gone wrong when you pasted it in here.
Do you have a link to it in a TXT or PHPS file that I can download and look at. I would like to get Eastern Character support working but I’m not yet as familiar with character encodings as I wish to be.
October 14th, 2007 at 5:48 am
Hi,
Is it necessary to ask for permission to develop a api like this?
October 14th, 2007 at 12:21 pm
Well, I didn’t ask. :)
October 16th, 2007 at 3:06 pm
ok, this is my web application, gooTrad:
http://www.ieschandomonte.edu.es/traducindote/probas/google/
I hope this service will be useful for you!
Cheers
November 20th, 2007 at 9:18 pm
Hi,this work is excelent but I have “false” everytime if the text is longer than 5 words.
Someone have an idea?
Thanx
December 18th, 2007 at 5:11 am
It is a great idea…I too got the same idea but didn’t started this work..Great work..Try like this..Keep a select box with google translator supported languages in u’r Website and when user changes the language change the whole website with that language ( Use Google URI translator..).I will try this..If any one got this..Plz post here or mail to saisuman2001@gmail.com.
Thanks in advance,
Sai Suman Gubba.
December 18th, 2007 at 5:53 am
I did tht.. I hope it works But need to test in a public site.
Any one need code mail me to saisuman2001@gmail.com
December 18th, 2007 at 2:05 pm
Hey guys, here is what I have come up with a solution for any language. Its a little cumbersome. I will try to upload it to the code.google site.
<?php
$langin = ‘en’;
$langout = ‘ja’;
function get_encoding($from, $to){
$lp = strtolower($from).”|”.strtolower($to);
file_get_contents(”http://google.com/translate_t?langpair=”.urlencode($lp).”&text=test”);
$encoding = str_replace(’Content-Type: text/html; charset=’,”,$http_response_header[1]);
return $encoding;
}
function translate_language($from, $to, $text) {
$lp = strtolower($from).”|”.strtolower($to);
$in = file_get_contents(”http://google.com/translate_t?langpair=”.urlencode($lp).”&text=”.urlencode($text));
$out = ereg_replace(”.*”,”",$in);
$out = ereg_replace(”.*”,”",$out);
return $out;
}
?>
<meta http-equiv=”content-type” content=”text/html”; charset=”" />
December 19th, 2007 at 4:33 am
umm… it seems like this is being executed or something because a lot of the tags disappeared. I posted it here if you like: http://designbreak.org/files/project/designbreak/googleapi.php.zip
January 21st, 2008 at 9:24 am
You can find a .Net API here:
http://www.codeplex.com/mgt
February 13th, 2008 at 12:12 am
I seem to be doing something wrong, it is not working for me, when i click on the underlined text, or use the code in the svn the text just disappears. help?
February 13th, 2008 at 12:47 am
OK< WooHooo! I have figured the issue out, Google has changed many things, i will be packaging a mini script together that will fix it and upload to the the Google code page.
February 13th, 2008 at 10:16 am
Zak H - Any luck getting that to work? Did you update?
February 16th, 2008 at 12:52 pm
sorry Adam, i have yet to update it, and yes i got it to work perfectly, i am creating a full blown API with the ability to not only translate section but translate the entire site.
March 5th, 2008 at 12:05 pm
i think you should replace
div id=result_box dir=”ltr”>
with
div id=result_box dir=\”ltr\”>
March 21st, 2008 at 4:07 pm
Careful how much you use this thing… If you make too many requests to their server, they’ll block you off.
May 16th, 2008 at 12:09 am
Hey guys, wow, been a while. Well anyway, BlastedLabs is going to publish the next version i got going on. Full PHP and JS library with the following features.
Full Page Translation
Country Flags with AJAX translation on click
Continuous translation once selected using flags
Anyway, if anyone would like to contribute, im going to add this source forge as well as update the google code project.
drop me a line zak [at] cyhite [dot] com
May 16th, 2008 at 7:10 am
if you dont want to use js/ajax you can use it as a php function
<?
function translate($text){
$lp = “nl|en”;
$in = file_get_contents(”http://google.com/translate_t?langpair=”.urlencode($lp).”&text=”.urlencode($text)) or die(”false”);
preg_match(’/(.*?)/’, $in, $out);
return utf8_encode($out[1]);
}
?>
May 17th, 2008 at 11:43 am
Right, but the good thing about using it with the ajax is the multiple features that can be added, which i am working on now.
May 30th, 2008 at 3:36 am
Does anyone know if there are limitations to the amount of volume you can translate with this, and if there is a way to ‘get’ (paid or unpaid) unlimited access to Google Translate interface?
June 18th, 2008 at 5:47 pm
Ciao, i don’t think there is a paid way to get unlimited volume, but i think a simple function can do the dirth tinhgs for you: Split in smaller pieces the wole text, translate the pieces with a loop, “compose” the result text into a new var.
quite simple
August 12th, 2008 at 3:45 am
Here’s a jQuery plugin that can translate an array and there’s no limit: http://code.google.com/p/jquery-translate/
December 12th, 2008 at 2:00 pm
This would be useful… Thanks Carl for posting the link.
January 11th, 2009 at 4:26 am
With this unofficial translation api, will we be able to hide the ip address of the user while translating a site for him/her?
i.e. Will it be completely anonymous?
i.e. Will http://tracemyip.com return different IP’s if visited with and without translation?
I am searching for a custom implementation of google translation, google cache or any other service of google to achieve results similar to that of an anonymous proxy server.
mj
January 27th, 2009 at 4:55 pm
Hey Friends
I was looking for google translate api and found out this blog, then another question popped up in my mind, let’s say if i get my website transalted into Japenese, will google appreciate that or google will understand, oh this is my own transalation, why i should consider it?