谷歌翻译(或类似API)为Android [英] GOOGLE TRANSLATE (or similar API) for Android
问题描述
我想知道是否有可能获得www.translate.google.com网站上翻译的文本,而无需付出代价。如果这是不可能,有没有其他的API,允许我简单地说,而无需创建任何标识,翻译一个字符串?我没有找到如何做到这一点在Java中/ Android的解决方案。没有人尝试之前做到这一点?
I am wondering if it is possible to get a translated text from www.translate.google.com website without having to pay for it. If it is not possible, is there any other API that allows me to simply, without having to create any ID, translate a String? I didn't find a solution on how to do it in Java/Android. Did anyone try to do it before?
我发现以下资源:
[收费服务] https://开头开发商.google.com /转换/ V2 / getting_started#背景概念
[UNPAID的解决方案 - 而不是Java] 的http://rupeshpatel.word$p$pss.com/2012/06/23/usage-of-google-translator-api-for-free/
[Bing的翻译] HTTP: //www.microsoft.com/web/post/using-the-free-bing-translation-apis
[PAID SERVICE] https://developers.google.com/translate/v2/getting_started#background-concepts
[UNPAID SOLUTION - NOT JAVA] http://rupeshpatel.wordpress.com/2012/06/23/usage-of-google-translator-api-for-free/
[BING TRANSLATION] http://www.microsoft.com/web/post/using-the-free-bing-translation-apis
编辑:
我觉得这说明了一切:
http://en.wikipedia.org/wiki/Computer-assisted_translation
I think this explains everything:
http://en.wikipedia.org/wiki/Computer-assisted_translation
推荐答案
法律声明:这是谷歌翻译服务的滥用,你必须为此付出代价的
Legal notice: This is an abuse of google translator services, you must pay for this.
我开发了一些code根据 [UNPAID的解决方案 - 而不是Java]
I developed some code based on [UNPAID SOLUTION - NOT JAVA]:
import java.io.*;
import java.net.*;
public class Translate{
public static String translate(String sl, String tl, String text) throws IOException{
// fetch
URL url = new URL("http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +
sl + "&tl=" + tl + "&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=" +
URLEncoder.encode(text, "UTF-8"));
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Something Else");
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String result = br.readLine();
br.close();
// parse
// System.out.println(result);
result = result.substring(2, result.indexOf("]]") + 1);
StringBuilder sb = new StringBuilder();
String[] splits = result.split("(?<!\\\\)\"");
for(int i = 1; i < splits.length; i += 8)
sb.append(splits[i]);
return sb.toString().replace("\\n", "\n").replaceAll("\\\\(.)", "$1");
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Source language:");
String sl = br.readLine();
System.out.println("Translation language:");
String tl = br.readLine();
System.out.println("Source:");
String text = br.readLine() + "\n" + br.readLine(); // read two lines
System.out.println("Translation:");
System.out.println(translate(sl, tl, text));
}
}
我承认这code未充分考虑,这样可能会导致资源的泄漏,喷油等。
I admit that this code is not fully considered, it might cause resources leak, injection, etc.
例输入:
en
nl
Hello"World
legal notice: This is an abuse of google translator services , you must pay for
this.
输出示例:
Hello "World
juridische mededeling : Dit is een misbruik van google vertaler diensten , moet
u betalen voor dit .
编辑:
- 您可以取消注释
//的System.out.println(结果);
在//解析
看你从谷歌检索,你可以使用AJAX库来分析它,以获得更多的信息,如替代的翻译。 - 在谷歌可能会改变他们的网站,他们的论点或未来任何事情,并打破了这个code。
- 我不是律师。 (我想你是不是,无论是。)总是问合法性的律师。
- You can uncomment the
// System.out.println(result);
after// parse
to see what you retrieved from google, and you may use an AJAX library to parse it, in order to get more information such as alternative translations. - Google may change their website, their arguments or anything in the future, and break this code.
- I'm not a lawyer. (I suppose you're not, either.) Always ask a lawyer of legality.
这篇关于谷歌翻译(或类似API)为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!