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...">
<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>
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>
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.