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());
}
}
Aggregations