Search in sources :

Example 16 with Translation

use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippets method testTranslateTextList.

@Test
public void testTranslateTextList() {
    // SNIPPET translateTexts
    List<String> texts = new LinkedList<>();
    texts.add("Hello, World!");
    texts.add("¡Hola Mundo!");
    List<Translation> translations = translate.translate(texts);
    // SNIPPET translateTexts
    Translation translation = translations.get(0);
    assertEquals("Hello, World!", translation.getTranslatedText());
    assertEquals("en", translation.getSourceLanguage());
    translation = translations.get(1);
    assertEquals("Hello World!", translation.getTranslatedText());
    assertEquals("es", translation.getSourceLanguage());
}
Also used : Translation(com.google.cloud.translate.Translation) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with Translation

use of com.google.cloud.translate.Translation in project getting-started-java by GoogleCloudPlatform.

the class TranslateServlet method doPost.

// [END getting_started_background_app_list]
/**
 * Handle a posted message from Pubsub.
 *
 * @param req The message Pubsub posts to this process.
 * @param resp Not used.
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    // Block requests that don't contain the proper verification token.
    String pubsubVerificationToken = PUBSUB_VERIFICATION_TOKEN;
    if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    // [START getting_started_background_translate_string]
    String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    PubSubMessage pubsubMessage = gson.fromJson(body, PubSubMessage.class);
    TranslateMessage message = pubsubMessage.getMessage();
    // Use Translate service client to translate the message.
    Translate translate = (Translate) this.getServletContext().getAttribute("translate");
    message.setData(decode(message.getData()));
    Translation translation = translate.translate(message.getData(), Translate.TranslateOption.sourceLanguage(message.getAttributes().getSourceLang()), Translate.TranslateOption.targetLanguage(message.getAttributes().getTargetLang()));
    // [END getting_started_background_translate_string]
    message.setTranslatedText(translation.getTranslatedText());
    try {
        // [START getting_started_background_translate]
        // Use Firestore service client to store the translation in Firestore.
        Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
        CollectionReference translations = firestore.collection("translations");
        ApiFuture<WriteResult> setFuture = translations.document().set(message, SetOptions.merge());
        setFuture.get();
        resp.getWriter().write(translation.getTranslatedText());
    // [END getting_started_background_translate]
    } catch (InterruptedException | ExecutionException e) {
        throw new ServletException("Exception storing data in Firestore.", e);
    }
}
Also used : Translation(com.google.cloud.translate.Translation) CollectionReference(com.google.cloud.firestore.CollectionReference) ServletException(javax.servlet.ServletException) WriteResult(com.google.cloud.firestore.WriteResult) Firestore(com.google.cloud.firestore.Firestore) PubSubMessage(com.getstarted.background.objects.PubSubMessage) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException) Translate(com.google.cloud.translate.Translate)

Aggregations

Translation (com.google.cloud.translate.Translation)17 Test (org.junit.Test)9 Translate (com.google.cloud.translate.Translate)7 TranslateOption (com.google.cloud.translate.Translate.TranslateOption)3 LinkedList (java.util.LinkedList)2 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Firestore (com.google.cloud.firestore.Firestore)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Detection (com.google.cloud.translate.Detection)1 TranslateException (com.google.cloud.translate.TranslateException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1