如何使用适用于Java的Google翻译版本2 API客户端库请求翻译? [英] How do you make a request for a translation with the Google Translate v2 API Client Library for java?
问题描述
没有关于如何将Java Translate API Cliente库用于java的示例。
在这个页面中,Google建议搜索他们的API的示例,但是没有Google翻译API: https://github.com/google/google-api-java-客户端样本
因为我没有找到Google Translate API的示例,所以我没有任何关于如何使用官方Java库的线索。
我想提出一个简单的请求,用Google制作的官方库翻译一段文本(例如,从英语到西班牙语的Hello World): https://developers.google.com/api-client-library/java/apis/translate/v2 一>但没有任何文档或示例可供公众查看。
有没有人有关于如何在java中使用Google Translate API客户端库的信息,我已经我已经将所有的罐子都包含在我的项目中,但我不知道我必须使用哪些类或哪些对象实例化从一种语言翻译到另一种语言。我根本不知道。我只需要简单地剪下一段代码,就像其他Google API的示例存储库一样。
这是一个工作示例。 p>
您需要为您的应用程序生成您自己的App-Key(开始 ),因为翻译API不再公开。
有关Translate.Builder()的选项,请参阅这里。
import java.util.Arrays;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;
public class TranslateMe {
public static void main(String [] args){
try {
/ /请参阅关于
的评论// https://developers.google.com/resources/api-libraries/documentation/translate/v2/java/latest/
//关于设置
的选项Translate t = new Translate.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport()
,com.google.api.client.json.gson.GsonFactory.getDefaultInstance( ),null)
//需要将它更新到您的应用程序名称
.setApplicationName(Stackoverflow-Example)
.build();
Translate.Translations.List list = t.new Translations()。list(
Arrays.asList(
//传入要翻译的字符串列表
Hello World ,
如何使用Google Translate from Java),
//目标语言
ES);
//从https://console.developers.google.com/
list.setKey(you-need-your-own-api-key)设置您的API密钥。
TranslationsListResponse response = list.execute();
for(TranslationsResource tr:response.getTranslations()){
System.out.println(tr.getTranslatedText());
}
} catch(Exception e){
e.printStackTrace();
}
}
}
There aren't examples on how to use Google Translate API Cliente library for java.
In this page Google suggest to search examples for their APIs but there is not a single one for Google Translate API: https://github.com/google/google-api-java-client-samples
Since I didn't found any example for Google Translate API I don't have any clue about how to use their official java library.
I want to make a simple request to translate a text (for example Hello World from english to spanish) with the Official library made by Google: https://developers.google.com/api-client-library/java/apis/translate/v2 but there is no documentation or examples available for the public.
Does anyone have info about how to use Google Translate API client library in java, I already googled and I had no luck at all.
I already have included all the jars to my project, but I don't know which classes I must use or which objects instantiate to make a translation from one language to another. I have no clue at all. I just need a simple snipped of code like in the examples repositories for other Google APIs.
Here's a working example.
You need to have your own App-Key generated for your app (start here) as translate API is no longer publicly available.
For options what to pass into Translate.Builder() see here.
import java.util.Arrays;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;
public class TranslateMe {
public static void main(String[] args) {
try {
// See comments on
// https://developers.google.com/resources/api-libraries/documentation/translate/v2/java/latest/
// on options to set
Translate t = new Translate.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport()
, com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
//Need to update this to your App-Name
.setApplicationName("Stackoverflow-Example")
.build();
Translate.Translations.List list = t.new Translations().list(
Arrays.asList(
//Pass in list of strings to be translated
"Hello World",
"How to use Google Translate from Java"),
//Target language
"ES");
//Set your API-Key from https://console.developers.google.com/
list.setKey("you-need-your-own-api-key");
TranslationsListResponse response = list.execute();
for(TranslationsResource tr : response.getTranslations()) {
System.out.println(tr.getTranslatedText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这篇关于如何使用适用于Java的Google翻译版本2 API客户端库请求翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!