I read an article from CF-Brasil mail list where the author (Fabio Cezar Gouveia) posted a small utility which sends a text to Yahoo’s Babelfish site and retrieves the translation using CFHTTP tag. The author asked if someone could improve the code and make it public, so I’m doing it. I made it a function and placed in a template with a form that calls itself. You may download the code here.
<cfprocessingdirective pageencoding="utf-8">
<cfparam name="form.source" default="" />
<cfparam name="form.charset" default="UTF-8" />
<cfparam name="form.doit" default="done" />
<cfparam name="form.fr" default="bf-home" />
<cfparam name="form.intl" default="1" />
<cfparam name="form.tt" default="urltext" />
<cfparam name="form.lp" default="" />
<cfparam name="result" default="Select language to translate" />
<cfif len(trim(form.source)) and len(trim(form.lp))>
<cfset result = translate(form.source,form.lp) />
</cfif>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<script language="JavaScript">
function verifyTrText(source){
if(source.value.length==0){
alert("Please enter a text to translate.");
return false;
}
return true;
}
</script>
<style>
#wrapper {
width:780px;
height:auto;
float:left;
padding:20px;
border:1px solid orange;
display:inline;
}
textarea {
margin:10px;
width:730px;
height:180px;
padding:10px;
border:1px solid grey;
}
.box {
float:left;
width:750px;
height:200px;
background-color:#f0f0f0;
clear:both;
border:1px dashed grey;
display:inline;
}
</style>
</head>
<body style="background-color:#FFFFCC;">
<div id="wrapper" style="">
<h2 style="text-align:center;margin:2px;padding:0;">Translator (Yahoo) Western Languages</h2>
<form name="frmTrText" action="translator.cfm" method="post" onsubmit="return verifyTrText(this.source);">
<div class="box">
<textarea name="source" wrap="virtual"><cfoutput>#form.source#</cfoutput></textarea>
</div>
<div class="box">
<textarea name="result" wrap="virtual"><cfoutput>#result#</cfoutput></textarea>
</div>
<div style="clear:both;float:left;text-align:center;">
<strong>Language:</strong>
<select name="lp" class="inp_sel" style="font-size: 0.8em;">
<option value="" selected>Select the translation</option>
<option value="en_fr">English -> French</option>
<option value="en_de">English -> Deutch</option>
<option value="en_nl">English -> Dutch</option>
<option value="en_pt">English -> Portuguese</option>
<option value="en_es">English -> Spanish</option>
<option value="en_it">English -> Italian</option>
<option value="de_en">Deutch -> English</option>
<option value="de_fr">Deutch -> French</option>
<option value="nl_en">Dutch -> English</option>
<option value="nl_fr">Dutch -> French</option>
<option value="fr_de">French -> Deutch</option>
<option value="fr_nl">French -> Dutch</option>
<option value="fr_en">French -> English</option>
<option value="fr_it">French -> Italian</option>
<option value="fr_pt">French -> Portuguese</option>
<option value="fr_es">French -> Spanish</option>
<option value="el_en">Greek -> English</option>
<option value="el_fr">Greek -> French</option>
<option value="it_en">Italian -> English</option>
<option value="it_fr">Italian -> French</option>
<option value="pt_en">Portuguese -> English</option>
<option value="pt_fr">Portuguese -> French</option>
<option value="es_en">Spanish -> English</option>
<option value="es_en">Spanish -> English</option>
<option value="es_fr">Spanish -> French</option>
</select>
<input class="inp_btn" value="Traduzir" name="btnTrTxt" type="submit" />
<span style="font-size:9pt;">by Fabio Cezar Gouveia (CF-BRASIL)</span>
</div>
</form>
</div>
</body>
</html>
<cffunction name="translate" access="remote" output="false" returntype="Any">
<!--- Author: Fabio Cezar Gouveia (CF-BRASIL) --->
<!--- Modified by: Ricardo Parente (cfdevelopers.net) --->
<cfargument name="inText" type="string" required="true" hint="Text to be translated" />
<cfargument name="lp" type="string" required="true" hint="Language pair" />
<cfargument name="charset" type="string" required="false" default="UTF-8" />
<cfargument name="doit" type="string" required="false" default="done" />
<cfargument name="fr" type="string" required="false" default="bf-home" />
<cfargument name="intl" type="string" required="false" default="1" />
<cfargument name="tt" type="string" required="false" default="urltext" />
<cfset var start = 0 />
<cfset var size = 0 />
<cftry>
<cfhttp url="http://br.babelfish.yahoo.com/translate_txt" method="POST">
<cfhttpparam name="ei" type="FormField" value="#arguments.charset#">
</cfhttpparam>
</cfhttpparam><cfhttpparam name="fr" type="FormField" value="#arguments.fr#">
</cfhttpparam><cfhttpparam name="intl" type="FormField" value="#arguments.intl#">
</cfhttpparam><cfhttpparam name="tt" type="FormField" value="#arguments.tt#">
</cfhttpparam><cfhttpparam name="trtext" type="FormField" value="#arguments.inText#">
</cfhttpparam><cfhttpparam name="lp" type="FormField" value="#arguments.lp#">
</cfhttpparam></cfhttp>
<cfscript>
start = Find("<div id=""result"">",CFHTTP.FileContent) + 45;
size = Find("</div>",CFHTTP.FileContent) - start;
result = mid(CFHTTP.fileContent,start,size);
</cfscript>
<cfcatch>
<cfset result = "An error occurred:<br/>#cfcatch.message#<br />#cfcatch.detail#" />
</cfcatch>
</cftry>
<cfreturn result />
</cffunction>
</cfprocessingdirective>
machine translation? bah humbug. you could kill somebody.
I agree with you Paul, I wouldn’t use this for any professional application, but it helps you having an idea of the subject.
I have done work with CFHTTP using the Paypal API but nothing like a translator. Good thing Ricardo! Thanks!