<%
String pathToRoot="../../";
request.setAttribute("pathToRoot", pathToRoot);
request.setAttribute("pageTitle", "If You Are Translate a Sentence Of English Into Another Language And Back, Probably It Will Obtain Dirtied Upwards");
request.setAttribute("bodyAttributes", " LINK=\"#a9a9a9\" VLINK=\"#a9a9a9\" ALINK=\"#a9a9a9\" ");
request.setAttribute("contentWidth", "745");
request.setAttribute("contentBorderColorHex", "ffffff");
request.setAttribute("contentPaddingTopPx", "0");
request.setAttribute("contentLeftPx", "85");
pageContext.include("../../includes/templateTop.jsp");
%>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>
<%!
static final String[] LANGS={
"fr",
"de",
"it",
"es",
"pt",
//"ko",
//"zt",
};
static final String BASE_URL="http://babelfish.yahoo.com/translate_txt?doit=done&intl=1&tt=urltext&";
static final char TRANSLATED_PHRASE_START_CHAR='`';
static final char TRANSLATED_PHRASE_END_CHAR='`';
static final int NUM_TRIES=5;
static final String DEFAULT_SOURCE="Type something please.";
static final String ERR_MSG="Hmm, please try again, or type something else.";
static String[] languageLabels=
{
"French",
"German",
"Italian",
"Spanish",
"Portuguese",
//"Korean",
//"Chinese",
};
static final String getTranslation(final String txt, String fromLang, String toLang) {
String text=""+txt;
try {
text=text
.replace('\"','~')
.replace('>',' ')
.replace('<',' ')
.replace('\n',' ')
.replace('\r',' ')
.replace(TRANSLATED_PHRASE_START_CHAR,' ')
.replace(TRANSLATED_PHRASE_END_CHAR,' ')
;
text=""+TRANSLATED_PHRASE_START_CHAR+text+TRANSLATED_PHRASE_END_CHAR;
String urlStr=BASE_URL+"trtext="+URLEncoder.encode(text)+"&lp="+fromLang+"_"+toLang;
URL url=new URL(urlStr);
System.out.println("in getTranslation(), connecting to: "+urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(true);
conn.setUseCaches(false);
conn.setRequestProperty("accept","text/plain, text/html");
conn.setRequestProperty("referer","http://babelfish.altavista.com/tr");
//conn.setRequestProperty("accept-language","en");
conn.setRequestProperty("content-type","application/x-www-form-urlencoded");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("cache-control","no-cache");
conn.connect();
InputStream in=conn.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
boolean more=true;
String line;
StringBuffer response=new StringBuffer(1024*25);
while (more) {
line=reader.readLine();
if (line==null) {
more=false;
}
else {
response.append(line+" ");
}
}
try {
reader.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
conn.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
String res=response.toString();
if (res.toLowerCase().indexOf("translation error")>-1) {
System.out.println("in getTranslation(), returning null");
return null;
}
// get it down to something slightly more manageable and relevant (the BODY):
res=res.substring(res.indexOf("-1) {
res=res.substring(0, res.indexOf('<'));
}
if (res.indexOf("<")>-1) {
res=res.substring(0, res.indexOf("<"));
}
if(fromLang.equals("el")) { // greek
res=res.replace('?',(char)0).replace('\'',(char)0);
}
System.out.println("in getTranslation(), returning "+res.trim());
return res.trim();
}
catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException();
}
}
%>
<%
boolean showBabelCredit=true;
String source=request.getParameter("source");
String lang=request.getParameter("lang");
boolean languageChosen=(lang!=null);
String dest="";
int t;
for (t=0; t0) {
dest=getTranslation(source, "en", lang);
if (dest!=null && !dest.trim().equals("null")) {
dest=getTranslation(dest, lang, "en");
}
else {
if (t==NUM_TRIES-1) {
dest=ERR_MSG;
}
else {
continue;
}
}
}
else {
source=DEFAULT_SOURCE;
dest="";
}
if (dest==null || dest.trim().equals("null")) {
if (t==NUM_TRIES-1) {
dest=ERR_MSG;
}
else {
continue;
}
}
break;
}
catch (Throwable e) {
e.printStackTrace();
if (t==NUM_TRIES-1) {
dest=ERR_MSG;
}
}
}
dest=dest.trim().replace('\"','~').replace('\\',' ');
int destLn=dest.length();
if (destLn>0
&& dest.charAt(destLn-1)!='?'
&& dest.charAt(destLn-1)!='!'
&& dest.charAt(destLn-1)!=','
&& dest.charAt(destLn-1)!=';'
&& dest.charAt(destLn-1)!='-'
&& dest.charAt(destLn-1)!='.'
) {
dest+=".";
}
%>

<% pageContext.include(pathToRoot+"includes/templateBottom.jsp"); %>
<% pageContext.include(pathToRoot+"includes/templateBottom.jsp"); %>