use of com.google.cloud.translate.v3.TranslateTextRequest in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method translateTextWithGlossary.
// [END translate_get_glossary_beta]
/**
* Translates a given text using a glossary.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param name - Glossary name.
* @param text - Text to be translated. e.g. "Hello World!"
* @param sourceLanguageCode - Language code of text. e.g. "en"
* @param targetLanguageCode - Language code for translation. e.g. "sr"
*/
// [START translate_translate_text_with_glossary_beta]
static TranslateTextResponse translateTextWithGlossary(String projectId, String location, String name, String text, String sourceLanguageCode, String targetLanguageCode) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GlossaryName glossaryName = GlossaryName.newBuilder().setProject(projectId).setLocation(location).setGlossary(name).build();
TranslateTextGlossaryConfig translateTextGlossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
TranslateTextRequest translateTextRequest = TranslateTextRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguageCode).setTargetLanguageCode(targetLanguageCode).addContents(text).setGlossaryConfig(translateTextGlossaryConfig).build();
// Call the API
TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
System.out.format("Translated text: %s", response.getGlossaryTranslationsList().get(0).getTranslatedText());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.translate.v3.TranslateTextRequest 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());
}
}
}
use of com.google.cloud.translate.v3.TranslateTextRequest in project java-translate by googleapis.
the class TranslateTextWithGlossary method translateTextWithGlossary.
// Translates a given text using a glossary.
public static void translateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId) 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();
// 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).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());
}
}
}
use of com.google.cloud.translate.v3.TranslateTextRequest in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method translateText.
// [END translate_detect_language_beta]
// [START translate_translate_text_beta]
/**
* Translates a given text to a target language.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param text - Text for translation.
* @param sourceLanguageCode - Language code of text. e.g. "en"
* @param targetLanguageCode - Language code for translation. e.g. "sr"
*/
static TranslateTextResponse translateText(String projectId, String location, String text, String sourceLanguageCode, String targetLanguageCode) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
TranslateTextRequest translateTextRequest = TranslateTextRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguageCode).setTargetLanguageCode(targetLanguageCode).addContents(text).build();
// Call the API
TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
System.out.format("Translated Text: %s", response.getTranslationsList().get(0).getTranslatedText());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.translate.v3.TranslateTextRequest 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);
}
}
Aggregations