Search in sources :

Example 1 with Glossary

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

the class ITTranslateSnippetsBeta method test2_testTranslateTextWithGlossary.

@Test
public void test2_testTranslateTextWithGlossary() {
    TranslateTextResponse response = TranslateSnippetsBeta.translateTextWithGlossary(projectId, "us-central1", "glossary-testing", "directions", "en", "es");
    assertEquals("indicaciones", response.getGlossaryTranslationsList().get(0).getTranslatedText());
}
Also used : TranslateTextResponse(com.google.cloud.translate.v3beta1.TranslateTextResponse) Test(org.junit.Test)

Example 2 with Glossary

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

the class ITTranslateSnippetsBeta method test2_testGetGlossary.

@Test
public void test2_testGetGlossary() {
    Glossary response = TranslateSnippetsBeta.getGlossary(projectId, "us-central1", "glossary-testing");
    assertTrue(response.toString(), response.toString().contains("glossary-testing"));
}
Also used : Glossary(com.google.cloud.translate.v3beta1.Glossary) Test(org.junit.Test)

Example 3 with Glossary

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

the class TranslateSnippetsBeta method getGlossary.

// [END translate_list_glossary_beta]
/**
 * Retrieves a glossary.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param name - Glossary name.
 */
// [START translate_get_glossary_beta]
static Glossary getGlossary(String projectId, String location, String name) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        GlossaryName glossaryName = GlossaryName.newBuilder().setProject(projectId).setLocation(location).setGlossary(name).build();
        // Call the API
        Glossary response = translationServiceClient.getGlossary(glossaryName.toString());
        System.out.format("Got: %s\n", response.getName());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) Glossary(com.google.cloud.translate.v3beta1.Glossary) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName)

Example 4 with Glossary

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

the class TranslateSnippetsBeta method translateTextWithGlossary.

// [END translate_get_glossary_beta]
/**
 * Translates a given text using a glossary.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param name - Glossary name.
 * @param text - Text to be translated. e.g. "Hello World!"
 * @param sourceLanguageCode - Language code of text. e.g. "en"
 * @param targetLanguageCode - Language code for translation. e.g. "sr"
 */
// [START translate_translate_text_with_glossary_beta]
static TranslateTextResponse translateTextWithGlossary(String projectId, String location, String name, String text, String sourceLanguageCode, String targetLanguageCode) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GlossaryName glossaryName = GlossaryName.newBuilder().setProject(projectId).setLocation(location).setGlossary(name).build();
        TranslateTextGlossaryConfig translateTextGlossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        TranslateTextRequest translateTextRequest = TranslateTextRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguageCode).setTargetLanguageCode(targetLanguageCode).addContents(text).setGlossaryConfig(translateTextGlossaryConfig).build();
        // Call the API
        TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
        System.out.format("Translated text: %s", response.getGlossaryTranslationsList().get(0).getTranslatedText());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) BatchTranslateTextRequest(com.google.cloud.translate.v3beta1.BatchTranslateTextRequest) TranslateTextRequest(com.google.cloud.translate.v3beta1.TranslateTextRequest) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName) TranslateTextGlossaryConfig(com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig) TranslateTextResponse(com.google.cloud.translate.v3beta1.TranslateTextResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 5 with Glossary

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

the class TranslateSnippetsBeta method listGlossary.

// [END translate_create_glossary_beta]
/**
 * Lists all the glossaries for a given project.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param filter - criteria for listing glossaries.
 */
// [START translate_list_glossary_beta]
static ListGlossariesPagedResponse listGlossary(String projectId, String location, String filter) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        ListGlossariesPagedResponse response = translationServiceClient.listGlossaries(locationName.toString(), filter);
        // Call the API
        for (Glossary element : response.iterateAll()) {
            System.out.format("Name: %s\n", element.getName());
            System.out.format("Language Codes Set:\n");
            System.out.format("Source Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(0));
            System.out.format("Target Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(1));
            System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) Glossary(com.google.cloud.translate.v3beta1.Glossary) ListGlossariesPagedResponse(com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

Glossary (com.google.cloud.translate.v3beta1.Glossary)5 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)5 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)4 Test (org.junit.Test)4 LocationName (com.google.cloud.translate.v3beta1.LocationName)3 DeleteGlossaryResponse (com.google.cloud.translate.v3beta1.DeleteGlossaryResponse)2 TranslateTextResponse (com.google.cloud.translate.v3beta1.TranslateTextResponse)2 BatchTranslateTextRequest (com.google.cloud.translate.v3beta1.BatchTranslateTextRequest)1 CreateGlossaryRequest (com.google.cloud.translate.v3beta1.CreateGlossaryRequest)1 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)1 LanguageCodesSet (com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet)1 GlossaryInputConfig (com.google.cloud.translate.v3beta1.GlossaryInputConfig)1 TranslateTextGlossaryConfig (com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig)1 TranslateTextRequest (com.google.cloud.translate.v3beta1.TranslateTextRequest)1 ListGlossariesPagedResponse (com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse)1