There aren't examples on how to use Google Translate API Cliente library for java.
没有关于如何将Java Translate API Cliente库用于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
在此页面中,Google建议搜索其API的示例,但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.
由于我没有找到任何谷歌翻译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/v2 but there is no documentation or examples available for the public.
我想提出一个简单的请求,将文本(例如Hello World从英语翻译成西班牙语)与Google制作的官方图书馆: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中使用谷歌翻译API客户端库,我已经google了,我根本没有运气。
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.
我已经将所有jar包含在我的项目中,但我不知道我必须使用哪些类或哪些对象实例化以从一种语言转换为另一种语言。我完全不知道。我只需要像其他Google API的示例存储库中那样简单地剪切代码。
2 个解决方案
#1
15
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("*-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();
}
}
}
#2
5
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>
#1
15
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("*-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();
}
}
}
#2
5
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>