/** Returns a default {@code TranslateOptions} instance. */ public static TranslateOptions getDefaultInstance() { return newBuilder().build(); }
@Override public Translation apply(TranslationsResource translationPb) { return Translation.fromPb(translationPb); } };
/** * 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 testTranslateTextWithOptions() { Translation translation = TRANSLATE.translate( "Hola", TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de")); assertEquals("Hallo", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); }
@Test public void testTranslateTextWithApiKey() { Translate translate = RemoteTranslateHelper.create(TranslateOptions.getDefaultApiKey()).getOptions().getService(); Translation translation = translate.translate("Hola"); assertEquals("Hello", translation.getTranslatedText()); assertEquals("es", translation.getSourceLanguage()); } }
@Test public void testFromPb() { assertEquals(TRANSLATED_TEXT, TRANSLATION.getTranslatedText()); assertEquals(SOURCE_LANGUAGE, TRANSLATION.getSourceLanguage()); compareTranslation(TRANSLATION, Translation.fromPb(TRANSLATION_PB)); }
@Override public Language apply(LanguagesResource languagePb) { return Language.fromPb(languagePb); } };
/** Returns a builder for {@code TranslateOptions} objects. */ public static Builder newBuilder() { return new Builder(); } }
/** * Sets the code for the default target language. If not set, English ({@code en}) is used. * Calls to {@link Translate#translate(List, TranslateOption...)} and {@link * Translate#translate(String, TranslateOption...)} will use this value unless an option from * {@link TranslateOption#targetLanguage(String)} is explicitly provided. * * @return the builder */ public Builder setTargetLanguage(String targetLanguage) { this.targetLanguage = targetLanguage; return self(); }
/** * Returns an option for setting the target language. If this option is not provided, the value * returned by {@link TranslateOptions#getTargetLanguage()} is used. When provided, the returned * {@link Language#getName()} will be in the language specified by the {@code targetLanguage} * code. * * @param targetLanguage the target language code */ public static LanguageListOption targetLanguage(String targetLanguage) { return new LanguageListOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage); } }
@Override public Translate create(TranslateOptions options) { return new TranslateImpl(options); } }
@Override public TranslateOptions build() { return new TranslateOptions(this); } }
@Override public int hashCode() { return baseHashCode(); }
@Override public TransportOptions getDefaultTransportOptions() { return getDefaultHttpTransportOptions(); } }
TranslateImpl(TranslateOptions options) { super(options); translateRpc = options.getTranslateRpcV2(); }
protected TranslateRpc getTranslateRpcV2() { return (TranslateRpc) getRpc(); }
/** * 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); }
@SuppressWarnings("unchecked") @Override public Builder toBuilder() { return new Builder(this); }
/** * 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 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); }