Java Code Examples for com.google.cloud.translate.Translation#getTranslatedText()

The following examples show how to use com.google.cloud.translate.Translation#getTranslatedText() . 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 vote down vote up
/**
 * 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: TranslateWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
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 3
Source File: Translator.java    From dualsub with GNU General Public License v3.0 5 votes vote down vote up
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 4
Source File: Translator.java    From dualsub with GNU General Public License v3.0 5 votes vote down vote up
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;
}