private void initializeService() { translate = options.getService(); }
return; Translate translate = optionsBuilder.build().getService(); action.run(translate, arg);
public static void main(String... args) { // Create a service object // // If no explicit credentials or API key are set, requests are authenticated using Application // Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY // environment variable Translate translate = TranslateOptions.getDefaultInstance().getService(); // Text of an "unknown" language to detect and then translate into English final String mysteriousText = "Hola Mundo"; // Detect the language of the mysterious text Detection detection = translate.detect(mysteriousText); String detectedLanguage = detection.getLanguage(); // Translate the mysterious text to English Translation translation = translate.translate( mysteriousText, TranslateOption.sourceLanguage(detectedLanguage), TranslateOption.targetLanguage("en")); System.out.println(translation.getTranslatedText()); } }
@Test public void testRetryableException() { EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new TranslateException(500, "internalError")) .andReturn(ImmutableList.of(LANGUAGE1_PB, LANGUAGE2_PB)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); assertEquals(LANGUAGES1, translate.listSupportedLanguages()); verify(); }
@Test public void testRuntimeException() { String exceptionMessage = "Artificial runtime exception"; EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); verify(); }
@Test public void testNonRetryableException() { String exceptionMessage = "Not Implemented"; EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new TranslateException(501, exceptionMessage)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); verify(); }
@Test public void testTranslateTextWithApiKey() { Translate translate = RemoteTranslateHelper.create(TranslateOptions.getDefaultApiKey()).getOptions().getService(); Translation translation = translate.translate("Hola"); assertEquals("Hello", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); } }
/** * Create Google Translate API Service. * * @return Google Translate Service */ public static Translate createTranslateService() { return TranslateOptions.newBuilder().build().getService(); }
public static void main(String... args) throws Exception { // Instantiates a client Translate translate = TranslateOptions.getDefaultInstance().getService(); // 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.getTranslatedText()); } }
public Translate getTranslationService(String apiKey) throws Exception { try { return TranslateOptions.newBuilder().setApiKey(apiKey).build().getService(); } catch (Exception e) { throw new IllegalArgumentException(e); } } }
@Override protected Void doInBackground(Void... params) { TranslateOptions options = TranslateOptions.newBuilder() .setApiKey(API_KEY) .build(); Translate translate = options.getService(); final Translation translation = translate.translate("Hello World", Translate.TranslateOption.targetLanguage("de")); textViewHandler.post(new Runnable() { @Override public void run() { if (textView != null) { textView.setText(translation.getTranslatedText()); } } }); return null; } }.execute();