您如何使用 Google Translate v2 API Client Library for java 提出翻译请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33424349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you make a request for a translation with the Google Translate v2 API Client Library for java?
提问by Saruva
There aren't examples on how to use Google Translate API Cliente library for java.
没有关于如何使用 Java 的 Google Translate API Cliente 库的示例。
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
在此页面中,谷歌建议搜索他们的 API 示例,但谷歌翻译 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.
由于我没有找到 Google Translate API 的任何示例,因此我对如何使用他们的官方 Java 库一无所知。
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/v2but there is no documentation or examples available for the public.
我想提出一个简单的请求,用 Google 制作的官方库翻译文本(例如 Hello World 从英语到西班牙语):https: //developers.google.com/api-client-library/java/apis/translate /v2但没有可供公众使用的文档或示例。
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.
有没有人有关于如何在 Java 中使用 Google Translate API 客户端库的信息,我已经用谷歌搜索过了,但我一点运气都没有。
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.
我已经将所有 jars 包含到我的项目中,但我不知道我必须使用哪些类或实例化哪些对象才能将一种语言翻译成另一种语言。我一点头绪也没有。我只需要一个简单的代码片段,就像在其他 Google API 的示例存储库中一样。
采纳答案by Jan
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.
您需要为您的应用生成自己的 App-Key(从这里开始),因为翻译 API 不再公开可用。
For options what to pass into Translate.Builder() see here.
有关传递到 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 {
// 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();
}
}
}
回答by Brad
reference:
Translate API Client Libraries
参考:
翻译 API 客户端库
template:
模板:
// Imports the Google Cloud client library
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class QuickstartSample {
public static void main(String... args) throws Exception {
// Instantiates a client
Translate translate = TranslateOptions.builder().apiKey("YOUR_API_KEY").build().service();
// The text to translate
String text = "Hello, world!";
// Translates some text into Russian
Translation translation = translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru")
);
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.translatedText());
}
}
maven:
行家:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
<version>0.4.0</version>
</dependency>