Search in sources :

Example 1 with CreateGlossaryRequest

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

the class TranslateSnippetsBeta method createGlossary.

// [END translate_batch_translate_text_beta]
/**
 * Creates a glossary.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param name - Glossary name.
 * @param gcsUri - Google Cloud Storage URI where glossary is stored in csv format.
 */
// [START translate_create_glossary_beta]
static Glossary createGlossary(String projectId, String location, String name, String gcsUri) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        LanguageCodesSet languageCodesSet = LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build();
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(gcsUri).build();
        GlossaryInputConfig glossaryInputConfig = GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
        GlossaryName glossaryName = GlossaryName.newBuilder().setProject(projectId).setLocation(location).setGlossary(name).build();
        Glossary glossary = Glossary.newBuilder().setLanguageCodesSet(languageCodesSet).setInputConfig(glossaryInputConfig).setName(glossaryName.toString()).build();
        CreateGlossaryRequest request = CreateGlossaryRequest.newBuilder().setParent(locationName.toString()).setGlossary(glossary).build();
        // Call the API
        Glossary response = translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS);
        System.out.format("Created: %s\n", response.getName());
        System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : LanguageCodesSet(com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3beta1.GcsSource) CreateGlossaryRequest(com.google.cloud.translate.v3beta1.CreateGlossaryRequest) GlossaryInputConfig(com.google.cloud.translate.v3beta1.GlossaryInputConfig) Glossary(com.google.cloud.translate.v3beta1.Glossary) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

CreateGlossaryRequest (com.google.cloud.translate.v3beta1.CreateGlossaryRequest)1 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)1 Glossary (com.google.cloud.translate.v3beta1.Glossary)1 LanguageCodesSet (com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet)1 GlossaryInputConfig (com.google.cloud.translate.v3beta1.GlossaryInputConfig)1 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)1 LocationName (com.google.cloud.translate.v3beta1.LocationName)1 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)1