Search in sources :

Example 6 with TranslateTextResponse

use of com.google.cloud.translate.v3.TranslateTextResponse in project java-docs-samples by GoogleCloudPlatform.

the class OcrTranslateText method accept.

@Override
public void accept(Message message, Context context) {
    OcrTranslateApiMessage ocrMessage = OcrTranslateApiMessage.fromPubsubData(message.getData().getBytes(StandardCharsets.UTF_8));
    String targetLang = ocrMessage.getLang();
    logger.info("Translating text into " + targetLang);
    // Translate text to target language
    String text = ocrMessage.getText();
    TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(LOCATION_NAME).setMimeType("text/plain").setTargetLanguageCode(targetLang).addContents(text).build();
    TranslateTextResponse response;
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        response = client.translateText(request);
    } catch (IOException e) {
        // Log error (since IOException cannot be thrown by a function)
        logger.log(Level.SEVERE, "Error translating text: " + e.getMessage(), e);
        return;
    }
    if (response.getTranslationsCount() == 0) {
        return;
    }
    String translatedText = response.getTranslations(0).getTranslatedText();
    logger.info("Translated text: " + translatedText);
    // Send translated text to (subsequent) Pub/Sub topic
    String filename = ocrMessage.getFilename();
    OcrTranslateApiMessage translateMessage = new OcrTranslateApiMessage(translatedText, filename, targetLang);
    try {
        ByteString byteStr = ByteString.copyFrom(translateMessage.toPubsubData());
        PubsubMessage pubsubApiMessage = PubsubMessage.newBuilder().setData(byteStr).build();
        publisher.publish(pubsubApiMessage).get();
        logger.info("Text translated to " + targetLang);
    } catch (InterruptedException | ExecutionException e) {
        // Log error (since these exception types cannot be thrown by a function)
        logger.log(Level.SEVERE, "Error publishing translation save request: " + e.getMessage(), e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) ByteString(com.google.protobuf.ByteString) TranslateTextRequest(com.google.cloud.translate.v3.TranslateTextRequest) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 7 with TranslateTextResponse

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

the class TranslateTextWithModel method translateTextWithModel.

// Translating Text with Model
public static void translateTextWithModel(String projectId, String sourceLanguage, String targetLanguage, String text, String modelId) 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)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguage).setTargetLanguageCode(targetLanguage).addContents(text).setModel(modelPath).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 8 with TranslateTextResponse

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

the class TranslateTextWithGlossaryAndModel method translateTextWithGlossaryAndModel.

// Translating Text with Glossary and Model
public static void translateTextWithGlossaryAndModel(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId, String modelId) 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)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguage).setTargetLanguageCode(targetLanguage).addContents(text).setGlossaryConfig(glossaryConfig).setModel(modelPath).build();
        TranslateTextResponse response = client.translateText(request);
        // Display the translation for each input text provided
        for (Translation translation : response.getGlossaryTranslationsList()) {
            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) GlossaryName(com.google.cloud.translate.v3.GlossaryName) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Aggregations

TranslateTextRequest (com.google.cloud.translate.v3.TranslateTextRequest)5 TranslateTextResponse (com.google.cloud.translate.v3.TranslateTextResponse)5 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)5 LocationName (com.google.cloud.translate.v3.LocationName)4 Translation (com.google.cloud.translate.v3.Translation)4 TranslateTextResponse (com.google.cloud.translate.v3beta1.TranslateTextResponse)3 GlossaryName (com.google.cloud.translate.v3.GlossaryName)2 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)2 BatchTranslateTextRequest (com.google.cloud.translate.v3beta1.BatchTranslateTextRequest)2 LocationName (com.google.cloud.translate.v3beta1.LocationName)2 TranslateTextRequest (com.google.cloud.translate.v3beta1.TranslateTextRequest)2 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)2 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)1 TranslateTextGlossaryConfig (com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig)1 ByteString (com.google.protobuf.ByteString)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1