Closed GoogleCodeExporter closed 8 years ago
Simple pass an empty string to the fucntion:
Translate.translate(textToTranslate, "", Language.SPANISH);
The Google API does the rest for you! =)
Original comment by alo...@gmail.com
on 12 Feb 2009 at 6:46
Original comment by rich.mid...@gmail.com
on 17 Feb 2009 at 7:47
There should be a Langage.DETECT constant in the Language.java tho... this
should be added
Original comment by alo...@gmail.com
on 17 Feb 2009 at 7:52
i modified the source for language detecttion
here is the Translate.java code
/**
* Translate.java
*
* Copyright (C) 2007, Richard Midwinter
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.google.api.translate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONObject;
/**
* Makes the Google Translate API available to Java applications.
*
* @author Richard Midwinter
* @author Emeric Vernat
* @author Juan B Cabral
*/
public class Translate {
private final String ENCODING = "UTF-8";
private final String URL_STRING
= "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=";
private final String TEXT_VAR = "&q=";
private TranslatedText gt;
/**
* Translates text from a given language to another given language using Google
Translate
*
* @param text The String to translate.
* @param from The language code to translate from.
* @param to The language code to translate to.
* @return The translated String.
* @throws MalformedURLException
* @throws IOException
*/
public String translate(String text, String from, String to) throws Exception {
gt = retrieveTranslation(text, from, to);
return gt.getText();
}
public String getDetectedLanguage(){
return gt.getGoogleLanguage();
}
/**
* Forms an HTTP request and parses the response for a translation.
*
* @param text The String to translate.
* @param from The language code to translate from.
* @param to The language code to translate to.
* @return The translated String.
* @throws Exception
*/
private TranslatedText retrieveTranslation(String text, String from, String to)
throws Exception {
try {
StringBuilder url = new StringBuilder();
url.append(URL_STRING).append(from).append("%7C").append(to);
url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING));
HttpURLConnection uc = (HttpURLConnection) new URL(url.toString
()).openConnection();
try {
String result = toString(uc.getInputStream());
System.out.println(result);
JSONObject json = new JSONObject(result);
TranslatedText tt = new TranslatedText();
tt.setText((((JSONObject)json.get("responseData")).getString
("translatedText")));
tt.setGoogleLanguage((((JSONObject)json.get
("responseData")).getString("detectedSourceLanguage")));
return tt;
} finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-
keepalive.html
uc.getInputStream().close();
if (uc.getErrorStream() != null) uc.getErrorStream().close();
}
} catch (Exception ex) {
throw new Exception("[google-api-translate-java] Error retrieving
translation.", ex);
}
}
/**
* Reads an InputStream and returns its contents as a String. Also effects rate
control.
* @param inputStream The InputStream to read from.
* @return The contents of the InputStream as a String.
* @throws Exception
*/
private String toString(InputStream inputStream) throws Exception {
StringBuilder outputBuilder = new StringBuilder();
try {
String string;
if (inputStream != null) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream, ENCODING));
while (null != (string = reader.readLine())) {
outputBuilder.append(string).append('\n');
}
}
} catch (Exception ex) {
throw new Exception("[google-api-translate-java] Error reading
translation stream.", ex);
}
return outputBuilder.toString();
}
}
Original comment by vdharankar
on 19 Feb 2009 at 8:16
There is a need of one more class that i have added to the package
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.google.api.translate;
/**
*
* @author vish
*/
public class TranslatedText {
private String text;
public String getGoogleLanguage() {
return googleLanguage;
}
public String getText() {
return text;
}
private String googleLanguage;
public void setGoogleLanguage(String googleLanguage) {
this.googleLanguage = googleLanguage;
}
public void setText(String text) {
this.text = text;
}
}
Original comment by vdharankar
on 19 Feb 2009 at 8:18
Reopened, thanks for the code changes. I'll look at getting them into a new
release asap.
Original comment by rich.mid...@gmail.com
on 19 Feb 2009 at 9:07
Hello! I went ahead and created a simple Detect class that does the language
detection. I used the Translate class as the basis for this other class. I
attach a
patch if anyone is interested.
Original comment by cesar.ar...@gmail.com
on 8 May 2009 at 3:11
Oops, from my last comment I forgot to attach the file.
Original comment by cesar.ar...@gmail.com
on 8 May 2009 at 3:12
Attachments:
Hi, Thank you for providing this. However the jar you uploaded does not contain
the
Detect class. I would be gratefull if you could upload the right one.
Original comment by ispi...@gmail.com
on 18 May 2009 at 4:32
Language detection is available via the Language.AUTO_DETECT value as the from
argument.
Original comment by rich.mid...@gmail.com
on 22 Jun 2009 at 6:13
is there a way to find the language of given text. Auto_Detect does not return
the
language. It just identifies the language of source text internally and
translates
source text into target language. Instead, Can it return language? or is there
any
other way?
Original comment by zeeva...@gmail.com
on 13 Aug 2009 at 10:29
Original issue reported on code.google.com by
vdharankar
on 11 Feb 2009 at 5:44