Posted in ColdFusion | Posted on 10-13-2008 | 5,203 views
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:
2 <cfargument name="str" type="string" required="true" hint="Text to translate.">
3 <cfargument name="langfrom" type="string" required="true" hint="Language code of the original text.">
4 <cfargument name="langTo" type="string" required="true" hint="Language code to translate the text to...">
5
6 <cfset var langPair = arguments.langFrom & "|" & arguments.langTo>
7 <cfset var theURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" & urlEncodedFormat(arguments.str) & "&langpair=" & urlEncodedFormat(langPair)>
8 <cfset var result = "">
9 <cfset var data = "">
10
11 <cfhttp url="#theURL#" result="result">
12
13 <cfset data = deserializeJSON(result.fileContent)>
14
15 <cfif data.responseStatus neq 200>
16 <cfreturn "">
17 </cfif>
18
19 <cfreturn data.responseData.translatedText>
20
21</cffunction>
To call it, you just supply the string and the language from and to arguments:
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.


Excusez moi, mais avez vous de la moutarde chère prétentieuse et jaune?
Your text above results in:
"????????, ???? ????? ??????????? ??????, ?????????? ??????? ?????????;"
which is understandable but not quite there - same problem.
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.
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 will extend it to support more document type and eventually do live translation.
PS : Can add me to the coldfusionbloggers feed too ? :-)
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.
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 ;)
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>
Used same code u gave to use in coldfusion :-(
ie. ? ?
[Add Comment] [Subscribe to Comments]