Search in sources :

Example 1 with DocumentInputConfig

use of com.google.cloud.translate.v3beta1.DocumentInputConfig in project java-translate by googleapis.

the class TranslateDocument method translateDocument.

// Translating Document
public static void translateDocument(String projectId, String filePath) throws IOException {
    // up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // The ``global`` location is not supported for batch translation
        LocationName parent = LocationName.of(projectId, "us-central1");
        // Supported file types: https://cloud.google.com/translate/docs/supported-formats
        ByteString content = ByteString.readFrom(new FileInputStream(filePath));
        DocumentInputConfig documentInputConfig = DocumentInputConfig.newBuilder().setContent(content).setMimeType("application/pdf").build();
        TranslateDocumentRequest request = TranslateDocumentRequest.newBuilder().setParent(parent.toString()).setTargetLanguageCode("fr-FR").setDocumentInputConfig(documentInputConfig).build();
        TranslateDocumentResponse response = client.translateDocument(request);
        // To view translated document, write `response.document_translation.byte_stream_outputs`
        // to file. If not provided in the TranslationRequest, the translated file will only be
        // returned through a byte-stream and its output mime type will be the same as the input
        // file's mime type
        System.out.println("Response: Detected Language Code - " + response.getDocumentTranslation().getDetectedLanguageCode());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) ByteString(com.google.protobuf.ByteString) TranslateDocumentRequest(com.google.cloud.translate.v3beta1.TranslateDocumentRequest) TranslateDocumentResponse(com.google.cloud.translate.v3beta1.TranslateDocumentResponse) DocumentInputConfig(com.google.cloud.translate.v3beta1.DocumentInputConfig) FileInputStream(java.io.FileInputStream) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

DocumentInputConfig (com.google.cloud.translate.v3beta1.DocumentInputConfig)1 LocationName (com.google.cloud.translate.v3beta1.LocationName)1 TranslateDocumentRequest (com.google.cloud.translate.v3beta1.TranslateDocumentRequest)1 TranslateDocumentResponse (com.google.cloud.translate.v3beta1.TranslateDocumentResponse)1 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)1 ByteString (com.google.protobuf.ByteString)1 FileInputStream (java.io.FileInputStream)1