Search in sources :

Example 31 with TranslationServiceClient

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

the class TranslateSnippetsBeta method deleteGlossary.

// [END translate_translate_text_with_glossary_beta]
/**
 * Deletes a glossary.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param name - Glossary name.
 */
// [START translate_delete_glossary_beta]
static DeleteGlossaryResponse deleteGlossary(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
        DeleteGlossaryResponse response = translationServiceClient.deleteGlossaryAsync(glossaryName.toString()).get(300, TimeUnit.SECONDS);
        System.out.format("Deleted: %s\n", response.getName());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : DeleteGlossaryResponse(com.google.cloud.translate.v3beta1.DeleteGlossaryResponse) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName)

Example 32 with TranslationServiceClient

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

the class TranslateSnippetsBeta method translateText.

// [END translate_detect_language_beta]
// [START translate_translate_text_beta]
/**
 * Translates a given text to a target language.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param text - Text for translation.
 * @param sourceLanguageCode - Language code of text. e.g. "en"
 * @param targetLanguageCode - Language code for translation. e.g. "sr"
 */
static TranslateTextResponse translateText(String projectId, String location, String text, String sourceLanguageCode, String targetLanguageCode) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        TranslateTextRequest translateTextRequest = TranslateTextRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguageCode).setTargetLanguageCode(targetLanguageCode).addContents(text).build();
        // Call the API
        TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
        System.out.format("Translated Text: %s", response.getTranslationsList().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) TranslateTextResponse(com.google.cloud.translate.v3beta1.TranslateTextResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 33 with TranslationServiceClient

use of com.google.cloud.translate.v3.TranslationServiceClient in project google-cloud-java by googleapis.

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)

Example 34 with TranslationServiceClient

use of com.google.cloud.translate.v3.TranslationServiceClient in project google-cloud-java by googleapis.

the class TranslateSnippetsBeta method listSupportedLanguagesWithTarget.

// [END translate_list_codes_beta]
/**
 * Lists all the supported language names and codes.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 */
// [START translate_list_language_names_beta]
static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).setDisplayLanguageCode("en-US").build();
        // Call the API
        ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
        SupportedLanguages response = future.get();
        List<SupportedLanguage> languages = response.getLanguagesList();
        for (SupportedLanguage language : languages) {
            System.out.printf("Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3beta1.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3beta1.SupportedLanguage) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 35 with TranslationServiceClient

use of com.google.cloud.translate.v3.TranslationServiceClient in project google-cloud-java by googleapis.

the class TranslateSnippetsBeta method listSupportedLanguages.

/**
 * Lists all the supported language codes.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 */
// [START translate_list_codes_beta]
static SupportedLanguages listSupportedLanguages(String projectId, String location) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build();
        // Call the API
        ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
        SupportedLanguages response = future.get();
        List<SupportedLanguage> languages = response.getLanguagesList();
        for (SupportedLanguage language : languages) {
            System.out.printf("Code: %s\n", language.getLanguageCode());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3beta1.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3beta1.SupportedLanguage) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)22 LocationName (com.google.cloud.translate.v3beta1.LocationName)18 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)17 LocationName (com.google.cloud.translate.v3.LocationName)13 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)8 GlossaryName (com.google.cloud.translate.v3.GlossaryName)7 BatchTranslateTextRequest (com.google.cloud.translate.v3beta1.BatchTranslateTextRequest)6 Glossary (com.google.cloud.translate.v3beta1.Glossary)6 GcsSource (com.google.cloud.translate.v3.GcsSource)5 TranslateTextRequest (com.google.cloud.translate.v3.TranslateTextRequest)5 TranslateTextResponse (com.google.cloud.translate.v3.TranslateTextResponse)5 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)5 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)4 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)4 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)4 GcsDestination (com.google.cloud.translate.v3.GcsDestination)4 InputConfig (com.google.cloud.translate.v3.InputConfig)4 OutputConfig (com.google.cloud.translate.v3.OutputConfig)4 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)4 Translation (com.google.cloud.translate.v3.Translation)4