您如何使用 Google Translate v2 API Client Library for java 提出翻译请求?

How do you make a request for a translation with the Google Translate v2 API Client Library for java?

没有关于如何使用 Google 翻译 API java 客户端库的示例。

在此页面中 Google 建议搜索他们的 API 示例,但 Google 没有一个示例 翻译 API:https://github.com/google/google-api-java-client-samples

因为我没有找到 Google 翻译 API 的任何示例,所以我不知道如何使用他们的官方 java 库。

我想提出一个简单的请求,用 Google 制作的官方图书馆翻译文本(例如 Hello World 从英语到西班牙语):https://developers.google.com/api-client-library/java/apis/translate/v2 但没有文档或示例适用于 public.

有没有人知道如何在 java 中使用 Google 翻译 API 客户端库,我已经用谷歌搜索了,但我一点运气都没有。

我已经将所有 jar 包含到我的项目中,但我不知道我必须使用哪个 类 或实例化哪些对象来将一种语言翻译成另一种语言。我一点头绪都没有。我只需要一个简单的代码片段,就像其他 Google APIs.

的示例存储库中那样

这是一个工作示例。

您需要为您的应用程序(开始 here)生成自己的应用程序密钥,因为翻译 API 不再公开可用。

有关传递给 Translate.Builder() 的选项,请参阅 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("Whosebug-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();
        }
    }
}

参考: Translate API Client Libraries

模板:

// 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());
  }
}


专家:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-translate</artifactId>
    <version>0.4.0</version>
</dependency>