2015-10-29 18 views
14

java用にGoogle Translate API Clienteライブラリを使用する方法の例はありません。このページグーグルでjavaのGoogle Translate v2 APIクライアントライブラリを使用して翻訳をリクエストするにはどうすればよいですか?

が彼らのAPIの例を検索することをお勧めが、Googleのための単一のものが存在しないAPI翻訳:GoogleがAPIを翻訳するために、私はどんな例が見つかりませんでしたのでhttps://github.com/google/google-api-java-client-samples

を私は持っていません。公式のJavaライブラリを使用する方法についてのヒント。

グーグルが作成した公式図書館(たとえば、Hello Worldを英語からスペイン語に翻訳)を簡単にリクエストしたいと思っていますが、一般公開されているドキュメントや例はありません。

誰かがJavaでGoogle Translate APIクライアントライブラリを使用する方法についての情報を持っていますが、私はすでにGoogleで検索しましたが、運がまったくありませんでした。

私は既に自分のプロジェクトにすべてのjarファイルを含めていますが、あるクラスから別のクラスへの翻訳を行うためにどのクラスを使用する必要があるのか​​、どのオブジェクトをインスタンス化するのか分かりません。私は全く手がかりがありません。他のGoogle APIのサンプルリポジトリのような単純なコードを必要とするだけです。

+0

あなたの要件は、正確には何ですか?私にとっては非常に不明です。 –

+0

誰もが自分の問題点を理解できるように、質問を編集しました。 –

答えて

14

実例があります。

翻訳APIは公開されていないため、独自のApp-Keyをアプリに作成する必要があります(開始here)。

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("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(); 
     } 
    } 
} 
+1

ちょうど私が必要なもの、感謝の男! –

+1

あなたのPCからの統合テストを成功させるために、 'referer'プロパティも設定します: "list.getRequestHeaders()。set( 'referer'、 'www.myautorizedsite.com');" –

5

参照: 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()); 
    } 
} 


のmaven:

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

TranslateOptions.builder()。apiKey()は推奨されていません。それを達成するための新しい方法はどれですか? – nanosoft

+0

'builder'の代わりに' new builder'を使います。@nanosoft – grant

+0

@grant:TranslateOptions.new builder()。apiKey( "GOOGLE_API_KEY")。ビルド()。サービス();エラーを返すTranslateOptionsを変数に解決できません - >これは変わっていますTranslateOptionsをインポートすることができます – nanosoft

関連する問題