Comments (11)
Simple pass an empty string to the fucntion:
Translate.translate(textToTranslate, "", Language.SPANISH);
The Google API does the rest for you! =)
Original comment by [email protected]
on 12 Feb 2009 at 6:46
from google-api-translate-java.
Original comment by [email protected]
on 17 Feb 2009 at 7:47
- Changed state: Done
from google-api-translate-java.
There should be a Langage.DETECT constant in the Language.java tho... this
should be added
Original comment by [email protected]
on 17 Feb 2009 at 7:52
from google-api-translate-java.
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
from google-api-translate-java.
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
from google-api-translate-java.
Reopened, thanks for the code changes. I'll look at getting them into a new
release asap.
Original comment by [email protected]
on 19 Feb 2009 at 9:07
- Changed state: Accepted
from google-api-translate-java.
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 [email protected]
on 8 May 2009 at 3:11
from google-api-translate-java.
Oops, from my last comment I forgot to attach the file.
Original comment by [email protected]
on 8 May 2009 at 3:12
Attachments:
from google-api-translate-java.
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 [email protected]
on 18 May 2009 at 4:32
from google-api-translate-java.
Language detection is available via the Language.AUTO_DETECT value as the from
argument.
Original comment by [email protected]
on 22 Jun 2009 at 6:13
- Changed state: Fixed
from google-api-translate-java.
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 [email protected]
on 13 Aug 2009 at 10:29
from google-api-translate-java.
Related Issues (20)
- Can't parse the response from Google Translate service into JSON object
- Error when using Detect.Execute HOT 1
- While using json-20090211.jar language Detect throws org.json.JSONException: JSONObject["responseData"] is not a JSONObject
- Regarding Json
- does it exist a method to have the various meanings of a word?
- How handled Authentication & Autherization in Google Translate API (in Java)?
- Wrong translation from Korean to Japanese by a period
- Different result on TextTranslator
- Not transelated Long sentences in Hangul
- Translated text has a '\' character
- translate returns null string
- Error in translator
- Error retrieving translation
- where do i save this file
- package junit.framework does not exist
- i am getting nothing
- Server returned HTTP response code: 403
- Translation Using Country Code Using Google Translation API
- Wasted a lot of time on this API
- Error: [google-api-translate-java] Error retrieving translation.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from google-api-translate-java.