Search in sources :

Example 11 with Glossary

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

the class GetSupportedLanguages method getSupportedLanguages.

// Getting a list of supported language codes
public static void getSupportedLanguages(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        LocationName parent = LocationName.of(projectId, "global");
        GetSupportedLanguagesRequest request = GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).build();
        SupportedLanguages response = client.getSupportedLanguages(request);
        // List language codes of supported languages
        for (SupportedLanguage language : response.getLanguagesList()) {
            System.out.printf("Language Code: %s\n", language.getLanguageCode());
        }
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3.SupportedLanguage) LocationName(com.google.cloud.translate.v3.LocationName)

Example 12 with Glossary

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

the class GetSupportedLanguagesForTarget method getSupportedLanguagesForTarget.

// Listing supported languages with target language name
public static void getSupportedLanguagesForTarget(String projectId, String languageCode) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        LocationName parent = LocationName.of(projectId, "global");
        GetSupportedLanguagesRequest request = GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).setDisplayLanguageCode(languageCode).build();
        SupportedLanguages response = client.getSupportedLanguages(request);
        // List language codes of supported languages
        for (SupportedLanguage language : response.getLanguagesList()) {
            System.out.printf("Language Code: %s\n", language.getLanguageCode());
            System.out.printf("Display Name: %s\n", language.getDisplayName());
        }
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3.SupportedLanguage) LocationName(com.google.cloud.translate.v3.LocationName)

Example 13 with Glossary

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

the class ListGlossaries method listGlossaries.

// List all the glossaries in a specified location
public static void listGlossaries(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        LocationName parent = LocationName.of(projectId, "us-central1");
        ListGlossariesRequest request = ListGlossariesRequest.newBuilder().setParent(parent.toString()).build();
        for (Glossary responseItem : client.listGlossaries(request).iterateAll()) {
            System.out.printf("Glossary name: %s\n", responseItem.getName());
            System.out.printf("Entry count: %s\n", responseItem.getEntryCount());
            System.out.printf("Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri());
        }
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) Glossary(com.google.cloud.translate.v3.Glossary) LocationName(com.google.cloud.translate.v3.LocationName) ListGlossariesRequest(com.google.cloud.translate.v3.ListGlossariesRequest)

Example 14 with Glossary

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

the class TranslateText method translateText.

// [END translate_v3_translate_text_1]
// [START translate_v3_translate_text_2]
// Translate text to target language.
public static void translateText(String projectId, String targetLanguage, String text) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        LocationName parent = LocationName.of(projectId, "global");
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setTargetLanguageCode(targetLanguage).addContents(text).build();
        TranslateTextResponse response = client.translateText(request);
        // Display the translation for each input text provided
        for (Translation translation : response.getTranslationsList()) {
            System.out.printf("Translated text: %s\n", translation.getTranslatedText());
        }
    }
}
Also used : Translation(com.google.cloud.translate.v3.Translation) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) TranslateTextRequest(com.google.cloud.translate.v3.TranslateTextRequest) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 15 with Glossary

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

the class BatchTranslateTextWithGlossary method batchTranslateTextWithGlossary.

// Batch Translate Text with a Glossary.
public static void batchTranslateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String glossaryId) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        // Configure the source of the file from a GCS bucket
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        // Configure the glossary used in the request
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        // Build the request that will be sent to the API
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).putGlossaries(targetLanguage, glossaryConfig).build();
        // Start an asynchronous request
        OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
        System.out.println("Waiting for operation to complete...");
        // random number between 300 - 450 (maximum allowed seconds)
        long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
        BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
        // Display the translation for each input text provided
        System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) BatchTranslateResponse(com.google.cloud.translate.v3.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3.LocationName) BatchTranslateMetadata(com.google.cloud.translate.v3.BatchTranslateMetadata) OutputConfig(com.google.cloud.translate.v3.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3.BatchTranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) InputConfig(com.google.cloud.translate.v3.InputConfig) GcsDestination(com.google.cloud.translate.v3.GcsDestination)

Aggregations

TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)14 LocationName (com.google.cloud.translate.v3.LocationName)12 Glossary (com.google.cloud.translate.v3beta1.Glossary)10 GlossaryName (com.google.cloud.translate.v3.GlossaryName)7 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)6 Test (org.junit.Test)6 GcsSource (com.google.cloud.translate.v3.GcsSource)4 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)4 TranslateTextRequest (com.google.cloud.translate.v3.TranslateTextRequest)4 TranslateTextResponse (com.google.cloud.translate.v3.TranslateTextResponse)4 Translation (com.google.cloud.translate.v3.Translation)4 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)4 LocationName (com.google.cloud.translate.v3beta1.LocationName)4 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)3 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)3 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)3 GcsDestination (com.google.cloud.translate.v3.GcsDestination)3 Glossary (com.google.cloud.translate.v3.Glossary)3 InputConfig (com.google.cloud.translate.v3.InputConfig)3 OutputConfig (com.google.cloud.translate.v3.OutputConfig)3