Java Code Examples for com.google.cloud.translate.Translate.TranslateOption#targetLanguage()

The following examples show how to use com.google.cloud.translate.Translate.TranslateOption#targetLanguage() . 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);
    }
}