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...">
<cfset var langPair = arguments.langFrom & "|" & arguments.langTo>
<cfset var theURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" & urlEncodedFormat(arguments.str) & "&langpair=" & urlEncodedFormat(langPair)>
<cfset var result = "">
<cfset var data = "">
<cfhttp url="#theURL#" result="result">
<cfset data = deserializeJSON(result.fileContent)>
<cfif data.responseStatus neq 200>
<cfreturn "">
</cfif>
<cfreturn data.responseData.translatedText>
</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.
Archived Comments
Almost there but not quite.
Excusez moi, mais avez vous de la moutarde chère prétentieuse et jaune?
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.
I guess you are not set up for non-Latin characters :o)
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.
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
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 ? :-)
@Cyril - send me a direct email please with your details.
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.
Where did you find this?
http://code.google.com/p/go... is where this jar file is found
Cool - but note that now I know where the REST API is. :)
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 ;)
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>
Where did u save that .jar file to be used in cold fusion?
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 :-(
Sorry - I didn't use the JAR file myself so I can't help here.
Is there any way I could use this for Japanese to english? I would need to submit EUC-JP encoded characters
ie. ? ?
If the API supports it. Give it a shot.
Thanks Man ... once again you save the day with a simple elegant solution. (well ... and CF)
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}
As this post is almost 5 years old, most likely the API isn't supported anymore.
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
Just one more fix in the following line required to display non English characters:
<cfset getResult = deserializeJSON(theResult.fileContent.toString())> just add the ".toString()"