/** * Translate the source text from source to target language. * Make sure that your project is whitelisted. * * @param sourceText source text to be translated * @param sourceLang source language of the text * @param targetLang target language of translated text * @param out print stream */ //[START translate_text_with_model] public static void translateTextWithOptionsAndModel( String sourceText, String sourceLang, String targetLang, PrintStream out) { Translate translate = createTranslateService(); TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); // Use translate `model` parameter with `base` and `nmt` options. TranslateOption model = TranslateOption.model("nmt"); Translation translation = translate.translate(sourceText, srcLang, tgtLang, model); out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText); out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.getTranslatedText()); } //[END translate_text_with_model]
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); } }
@Override protected Void doInBackground(Void... params) { TranslateOptions options = TranslateOptions.newBuilder() .setApiKey(API_KEY) .build(); Translate translate = options.getService(); final Translation translation = translate.translate("Hello World", Translate.TranslateOption.targetLanguage("de")); textViewHandler.post(new Runnable() { @Override public void run() { if (textView != null) { textView.setText(translation.getTranslatedText()); } } }); return null; } }.execute();
/** * {@inheritDoc} */ @Override public String translate(String sourceText, String sourceLang, String targetLang) { TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); Translation translation = translator.translate( sourceText, srcLang, tgtLang, model); return translation.getTranslatedText(); } }
/** * Returns an option for setting the source language. If not provided, Google Translation will * try to detect the language of the text to translate. * * @param sourceLanguage the source language code */ public static TranslateOption sourceLanguage(String sourceLanguage) { return new TranslateOption(TranslateRpc.Option.SOURCE_LANGUAGE, sourceLanguage); }
/** * Sets the language translation model. You can use this parameter to take advantage of Neural * Machine Translation. Possible values are {@code base} and {@code nmt}. Google Translation * could use a different model to translate your text: use {@link Translation#getModel()} to * know which model was used for translation. Please note that you must be whitelisted to use * this option, otherwise translation will fail. * * @param model the language translation model */ public static TranslateOption model(String model) { return new TranslateOption(TranslateRpc.Option.MODEL, model); }
/** * Returns an option for setting the source language. If not provided, Google Translate will try * to detect the language of the text to translate. * * @param sourceLanguage the source language code */ public static TranslateOption sourceLanguage(String sourceLanguage) { return new TranslateOption(TranslateRpc.Option.SOURCE_LANGUAGE, sourceLanguage); }
/** * Returns an option for setting the target language. If this option is not provided, the value * returned by {@link TranslateOptions#getTargetLanguage()} is used. * * @param targetLanguage the target language code */ public static TranslateOption targetLanguage(String targetLanguage) { return new TranslateOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage); }
/** * Returns an option for setting the target language. If this option is not provided, the value * returned by {@link TranslateOptions#targetLanguage()} is used. * * @param targetLanguage the target language code */ public static TranslateOption targetLanguage(String targetLanguage) { return new TranslateOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage); } }
/** * Sets the format of the source text, in either HTML (default) or plain-text. A value of {@code * html} indicates HTML and a value of {@code text} indicates plain-text. * * @param format the format of the source text */ public static TranslateOption format(String format) { return new TranslateOption(TranslateRpc.Option.FORMAT, format); } }
/** * 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 * @param out print stream */ public static void translateTextWithOptions( String sourceText, String sourceLang, String targetLang, PrintStream out) { Translate translate = createTranslateService(); TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); Translation translation = translate.translate(sourceText, srcLang, tgtLang); out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText); out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.getTranslatedText()); }
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()); } }
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()); } }
@Test public void testTranslateTextListWithModel() { List<Translation> translations = TRANSLATE.translate(ImmutableList.of("Hola", "Hallo"), TranslateOption.model("nmt")); Translation translation = translations.get(0); assertEquals("Hello", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); assertEquals("nmt", translation.getModel()); translation = translations.get(1); assertEquals("Hello", translation.getTranslatedText()); assertEquals("de", translation.getSourceLanguage()); assertEquals("nmt", translation.getModel()); }
/** * Returns an option for setting the source language. If not provided, Google Translation will * try to detect the language of the text to translate. * * @param sourceLanguage the source language code */ public static TranslateOption sourceLanguage(String sourceLanguage) { return new TranslateOption(TranslateRpc.Option.SOURCE_LANGUAGE, sourceLanguage); }
/** * Sets the format of the source text, in either HTML (default) or plain-text. A value of {@code * html} indicates HTML and a value of {@code text} indicates plain-text. * * @param format the format of the source text */ public static TranslateOption format(String format) { return new TranslateOption(TranslateRpc.Option.FORMAT, format); } }
@Test public void testTranslateOptions() { // target language TranslateOption translateOption = TranslateOption.targetLanguage(LANGUAGE); assertEquals(TranslateRpc.Option.TARGET_LANGUAGE, translateOption.getRpcOption()); assertEquals(LANGUAGE, translateOption.getValue()); // source language translateOption = TranslateOption.sourceLanguage(LANGUAGE); assertEquals(TranslateRpc.Option.SOURCE_LANGUAGE, translateOption.getRpcOption()); assertEquals(LANGUAGE, translateOption.getValue()); } }
/** * Sets the language translation model. You can use this parameter to take advantage of Neural * Machine Translation. Possible values are {@code base} and {@code nmt}. Google Translation * could use a different model to translate your text: use {@link Translation#getModel()} to * know which model was used for translation. Please note that you must be whitelisted to use * this option, otherwise translation will fail. * * @param model the language translation model */ public static TranslateOption model(String model) { return new TranslateOption(TranslateRpc.Option.MODEL, model); }
/** * Returns an option for setting the target language. If this option is not provided, the value * returned by {@link TranslateOptions#getTargetLanguage()} is used. * * @param targetLanguage the target language code */ public static TranslateOption targetLanguage(String targetLanguage) { return new TranslateOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage); }
@Test public void testTranslateTextWithOptions() { Translation translation = TRANSLATE.translate( "Hola", TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de")); assertEquals("Hallo", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); }