com.google.cloud.translate.Translate.TranslateOption Java Examples
The following examples show how to use
com.google.cloud.translate.Translate.TranslateOption.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Translate.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Translate the source text from source to target language. * * @param sourceText source text to be translated * @param sourceLang source language of the text * @param targetLang target language of translated text * @return source text translated into target language. */ public static String translateText( String sourceText, String sourceLang, String targetLang) { if (Strings.isNullOrEmpty(sourceLang) || Strings.isNullOrEmpty(targetLang) || sourceLang.equals(targetLang)) { return sourceText; } com.google.cloud.translate.Translate translate = createTranslateService(); TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); Translation translation = translate.translate(sourceText, srcLang, tgtLang); return translation.getTranslatedText(); }
Example #2
Source File: DetectLanguageAndTranslate.java From google-cloud-java with Apache License 2.0 | 6 votes |
public static void main(String... args) { // Create a service object // // If no explicit credentials or API key are set, requests are authenticated using Application // Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY // environment variable Translate translate = TranslateOptions.getDefaultInstance().getService(); // Text of an "unknown" language to detect and then translate into English final String mysteriousText = "Hola Mundo"; // Detect the language of the mysterious text Detection detection = translate.detect(mysteriousText); String detectedLanguage = detection.getLanguage(); // Translate the mysterious text to English Translation translation = translate.translate( mysteriousText, TranslateOption.sourceLanguage(detectedLanguage), TranslateOption.targetLanguage("en")); System.out.println(translation.getTranslatedText()); }
Example #3
Source File: TranslateWorkitemHandler.java From jbpm-work-items with Apache License 2.0 | 5 votes |
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) { try { RequiredParameterValidator.validate(this.getClass(), workItem); String toTranslate = (String) workItem.getParameter("ToTranslate"); String sourceLang = (String) workItem.getParameter("SourceLang"); String targetLang = (String) workItem.getParameter("TargetLang"); Map<String, Object> results = new HashMap<String, Object>(); translationService = googleTranslateAuth.getTranslationService(apiKey); TranslateOption srcLangOption = TranslateOption.sourceLanguage(sourceLang); TranslateOption targetLangOption = TranslateOption.targetLanguage(targetLang); Translation translation = translationService.translate(toTranslate, srcLangOption, targetLangOption); if (translation != null && translation.getTranslatedText() != null) { results.put(RESULTS_TRANSLATION, translation.getTranslatedText()); } else { logger.error("Could not translate provided text."); throw new IllegalArgumentException("Could not translate provided text."); } workItemManager.completeWorkItem(workItem.getId(), results); } catch (Exception e) { logger.error("Error executing workitem: " + e.getMessage()); handleException(e); } }
Example #4
Source File: GoogleTranslateWorkitemHandlerTest.java From jbpm-work-items with Apache License 2.0 | 5 votes |
@Before public void setUp() { try { when(auth.getTranslationService(anyString())).thenReturn(translationService); when(translationService.detect(anyString())).thenReturn(detection); when(detection.getLanguage()).thenReturn("Serbian"); when(translationService.translate(anyString(), any(TranslateOption.class), any(TranslateOption.class))).thenReturn(translation); when(translation.getTranslatedText()).thenReturn("Dobar dan"); } catch (Exception e) { fail(e.getMessage()); } }
Example #5
Source File: QuickstartSample.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void main(String... args) throws Exception { // Instantiates a client Translate translate = TranslateOptions.getDefaultInstance().getService(); // The text to translate String text = "Hello, world!"; // Translates some text into Russian Translation translation = translate.translate( text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru")); System.out.printf("Text: %s%n", text); System.out.printf("Translation: %s%n", translation.getTranslatedText()); }
Example #6
Source File: Translator.java From dualsub with GNU General Public License v3.0 | 5 votes |
public String translate(String text, String languageFrom, String languageTo) { Translation translation = translate.translate(text, TranslateOption.sourceLanguage(languageFrom), TranslateOption.targetLanguage(languageTo)); String translatedText = translation.getTranslatedText(); log.trace("Translating {} [{}] to [{}] ... result={}", text, languageFrom, languageTo, translatedText); return translatedText; }
Example #7
Source File: Translator.java From dualsub with GNU General Public License v3.0 | 5 votes |
public List<String> translate(List<String> text, String languageFrom, String languageTo) { List<Translation> translation = translate.translate(text, TranslateOption.sourceLanguage(languageFrom), TranslateOption.targetLanguage(languageTo)); List<String> out = new ArrayList<String>(); for (Translation t : translation) { String translatedText = t.getTranslatedText(); out.add(translatedText); log.trace("Translating {} [{}] to [{}] ... result={}", text, languageFrom, languageTo, translatedText); } return out; }