Search in sources :

Example 1 with BatchTranslateTextRequest

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

the class TranslateSnippetsBeta method batchTranslateText.

// [END translate_translate_text_beta]
/**
 * Translates a batch of texts on GCS and stores the result in a GCS location.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param sourceUri - Google Cloud Storage URI. Location where text is stored.
 * @param destinationUri - Google Cloud Storage URI where result will be stored.
 */
// [START translate_batch_translate_text_beta]
static BatchTranslateResponse batchTranslateText(String projectId, String location, String sourceUri, String destinationUri) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        BatchTranslateTextRequest batchTranslateTextRequest = BatchTranslateTextRequest.newBuilder().setParent(locationName.toString()).setSourceLanguageCode("en").addTargetLanguageCodes("sr").addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
        // Call the API
        BatchTranslateResponse response = translationServiceClient.batchTranslateTextAsync(batchTranslateTextRequest).get(300, TimeUnit.SECONDS);
        System.out.printf("Total Characters: %d\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3beta1.GcsSource) OutputConfig(com.google.cloud.translate.v3beta1.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3beta1.BatchTranslateTextRequest) InputConfig(com.google.cloud.translate.v3beta1.InputConfig) GlossaryInputConfig(com.google.cloud.translate.v3beta1.GlossaryInputConfig) GcsDestination(com.google.cloud.translate.v3beta1.GcsDestination) BatchTranslateResponse(com.google.cloud.translate.v3beta1.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

BatchTranslateResponse (com.google.cloud.translate.v3beta1.BatchTranslateResponse)1 BatchTranslateTextRequest (com.google.cloud.translate.v3beta1.BatchTranslateTextRequest)1 GcsDestination (com.google.cloud.translate.v3beta1.GcsDestination)1 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)1 GlossaryInputConfig (com.google.cloud.translate.v3beta1.GlossaryInputConfig)1 InputConfig (com.google.cloud.translate.v3beta1.InputConfig)1 LocationName (com.google.cloud.translate.v3beta1.LocationName)1 OutputConfig (com.google.cloud.translate.v3beta1.OutputConfig)1 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)1