Translation via Google

This post is more than 2 years old.

Sorry for the lack of postings lately. Things are very busy at work lately so it will most likely be a quiet week. I did run into an interesting article via DZone today: Google Translation API: Translate on server side using PHP The author shows an example of hitting the Google Translation service with PHP. This is very unofficial as the service was meant for AJAX, but it does actually work via server side code. Anything written in PHP can be done in ColdFusion of course. Here is my version:

<cffunction name="translate" output="false" returnType="string"> <cfargument name="str" type="string" required="true" hint="Text to translate."> <cfargument name="langfrom" type="string" required="true" hint="Language code of the original text."> <cfargument name="langTo" type="string" required="true" hint="Language code to translate the text to...">
&lt;cfset var langPair = arguments.langFrom & "|" & arguments.langTo&gt;
&lt;cfset var theURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" & urlEncodedFormat(arguments.str) & "&langpair=" & urlEncodedFormat(langPair)&gt;
&lt;cfset var result = ""&gt;
&lt;cfset var data = ""&gt;

&lt;cfhttp url="#theURL#" result="result"&gt;

&lt;cfset data = deserializeJSON(result.fileContent)&gt;

&lt;cfif data.responseStatus neq 200&gt;
	&lt;cfreturn ""&gt;
&lt;/cfif&gt;

&lt;cfreturn data.responseData.translatedText&gt;	

</cffunction>

To call it, you just supply the string and the language from and to arguments:

<cfoutput>#translate("Pardon me, but do you have any expensive, pretentious yellow mustard?", "en", "fr")#</cfoutput>

This returns:

Excusez-moi, mais avez-vous cher, prétentieux jaune moutarde?

I don't know French, but that certainly looks right to me. Again, as this is unofficial, I wouldn't expect it to always work, and for this reason I won't submit the code to CFLib. Hopefully though it will be use to someone.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Jean posted on 10/14/2008 at 9:59 AM

Almost there but not quite.

Excusez moi, mais avez vous de la moutarde chère prétentieuse et jaune?

Comment 2 by Evagoras Charalambous posted on 10/14/2008 at 12:37 PM

I had tried something similar in the past to translate from English to Greek, but the results were not that great, so I gave up on the idea. The issue that I had is that it could not properly translate words into their proper singular/plural or male/female equivalent of a word. Greek is tough to translate anyway, but I wonder if it's the same for other languages which have such rules - such as Spanish.
Your text above results in:
"????????, ???? ????? ??????????? ??????, ?????????? ??????? ?????????;"
which is understandable but not quite there - same problem.

Comment 3 by Evagoras Charalambous posted on 10/14/2008 at 12:40 PM

I guess you are not set up for non-Latin characters :o)

Comment 4 by Chris posted on 10/14/2008 at 3:49 PM

Automatic translation services will never be perfect and should not be relied on for exact word-for-word translations - they don't understand context and fall down completely on colloquialisms.

I do use Google Translate almost everyday at work (not via the API), and it does help. I'm translating articles so I can write a brief summary in English - Google Translate, Babelfish, etc. generally get enough words right that I'm able to use their translations.

Comment 5 by Frederic Fortier posted on 10/14/2008 at 5:39 PM

Well even if it is unofficial, it's cool.

It prevents having to add the google source script on the page you want to use the translation feature. And if you want the Ajax it.. you can always call the function with jQuery

Thanks Ray

Comment 6 by Cyril Hanquez posted on 10/15/2008 at 3:04 PM

I made a small example using the Google Language Detection API to guess the language of a Word document using Flex, ColdFusion and Java POI. http://cyrilhanquez.com/blo...

I will extend it to support more document type and eventually do live translation.

PS : Can add me to the coldfusionbloggers feed too ? :-)

Comment 7 by Raymond Camden posted on 10/15/2008 at 3:06 PM

@Cyril - send me a direct email please with your details.

Comment 8 by Adrian posted on 1/21/2010 at 5:44 AM

I was looking for an example how to do this with java. I found out that google has a jar file you can deploy since its that easy with CF.
After some playing around with it. Real EASY!
<cfset test = CreateObject("Java","com.google.api.translate.Translate")>
<cfset language = CreateObject("Java","com.google.api.translate.Language")>
<cfset site = CreateObject("Java","com.google.api.translate.Translate").setHttpReferrer("localhost")>
<cfdump var="#test.translate("How are you?",language.ENGLISH, language.SPANISH)#">

Its really that easy to translate with the deployed jar.

Comment 9 by Raymond Camden posted on 1/21/2010 at 7:14 AM

Where did you find this?

Comment 10 by adrian posted on 3/10/2010 at 4:21 AM

http://code.google.com/p/go... is where this jar file is found

Comment 11 by Raymond Camden posted on 3/10/2010 at 4:26 AM

Cool - but note that now I know where the REST API is. :)

Comment 12 by Adrian posted on 3/10/2010 at 4:43 AM

Never had a need for this cuz im going directly from spanish to english, but i'm pretty sure it has an autodetect as language.AUTO_DETECT.
Also, forgot to mention installation for those who dont know. download the file to ColdFusion8\wwwroot\WEB-INF\lib directory, restart service and...it should work ;)

Comment 13 by Paolo Groppo posted on 2/22/2011 at 4:26 PM

Modified to POST method to handle translations up to 5000 chars.
GET method return "URI too long" before allowed 5000 char

Bye

<cfhttp url="#theURL#" result="result" method="post">
<cfhttpparam name="v" value="1.0" type="formfield">
<cfhttpparam name="q" value="#arguments.str#" type="formfield">
<cfhttpparam name="langpair" value="#langPair#" type="formfield">
</cfhttp>

Comment 14 by vasanth posted on 3/8/2011 at 3:33 PM

Where did u save that .jar file to be used in cold fusion?

Comment 15 by vasanth posted on 3/8/2011 at 4:43 PM

Hey..I added tht jar file and also finished setting up path from cf admin. I'm getting an error "[google-api-translate-java] Error retrieving translation. "
Used same code u gave to use in coldfusion :-(

Comment 16 by Raymond Camden posted on 3/14/2011 at 8:47 PM

Sorry - I didn't use the JAR file myself so I can't help here.

Comment 17 by Kevin posted on 9/15/2011 at 6:35 AM

Is there any way I could use this for Japanese to english? I would need to submit EUC-JP encoded characters
ie. ? ?

Comment 18 by Raymond Camden posted on 9/15/2011 at 6:48 AM

If the API supports it. Give it a shot.

Comment 19 by ben posted on 3/4/2012 at 7:10 AM

Thanks Man ... once again you save the day with a simple elegant solution. (well ... and CF)

Comment 20 by Yoosaf Abdulla posted on 9/11/2013 at 7:23 PM

Hi Raymond,

when i used your function to translate a list of Room Amenities for my resort i got this as response. Please help.

{"responseData": null, "responseDetails": "Suspected Terms of Service Abuse. Please see http://code.google.com/apis...", "responseStatus": 403}

Comment 21 by Raymond Camden posted on 9/11/2013 at 7:24 PM

As this post is almost 5 years old, most likely the API isn't supported anymore.

Comment 22 by Talal Manaa posted on 9/26/2014 at 7:08 PM

The following is much easier for Google Translate API v2:
<cfset theKey = "get your key from google developer console">
<cfset theFormat = "html">
<cfset theSource = "en">
<cfset theTarget = "ar">
<cfset theText = URLencodedFormat("for sale", "utf-8")>
<cfset theURL = "https://www.googleapis.com/..."&theKey&"&format="&theFormat&"&source="&theSource&"&target="&theTarget&"&q="&theText>
<cfhttp url="#theURL#" result="theResult" charset="utf-8">
<cfset getResult = deserializeJSON(theResult.fileContent)>
<cfoutput>#URLDecode(getResult.data.translations[1].translatedText)#</cfoutput>
Please note you also need to enable billing before this code works

Comment 23 by Tala Manaa posted on 9/28/2014 at 3:21 PM

Just one more fix in the following line required to display non English characters:
<cfset getResult = deserializeJSON(theResult.fileContent.toString())> just add the ".toString()"