/** * Translate using Microsoft Translate API * @param sourceLanguageCode Source language code, for example, "en" * @param targetLanguageCode Target language code, for example, "es" * @param sourceText Text to send for translation * @return Translated text */ static String translate(String sourceLanguageCode, String targetLanguageCode, String sourceText) { Translate.setClientId(CLIENT_ID); Translate.setClientSecret(CLIENT_SECRET); try { Log.d(TAG, sourceLanguageCode + " -> " + targetLanguageCode); return Translate.execute(sourceText, Language.fromString(sourceLanguageCode), Language.fromString(targetLanguageCode)); } catch (Exception e) { Log.e(TAG, "Caught exeption in translation request."); e.printStackTrace(); return Translator.BAD_TRANSLATION_MSG; } }
/** * Use the Microsoft service to translate the given text to the given target language. The source language * is automatically detected by Microsoft. You must set the client keys in translator.microsoft.properties. * @param text The text to translate. * @param targetLanguage The desired language to translate to (for example, "hi"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
/** * Use the Microsoft service to translate the given text from the given source language to the given target. * You must set the client keys in translator.microsoft.properties. * * @param text The text to translate. * @param sourceLanguage The input text language (for example, "en"). * @param targetLanguage The desired language to translate to (for example, "fr"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language source = Language.fromString(sourceLanguage); Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, source, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
public MicrosoftParaphraser(String clientId, String clientSecret) { languages.add(Language.FRENCH); languages.add(Language.GERMAN); languages.add(Language.SPANISH); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); }
/** * Use the Microsoft service to translate the given text to the given target language. The source language * is automatically detected by Microsoft. You must set the client keys in translator.microsoft.properties. * @param text The text to translate. * @param targetLanguage The desired language to translate to (for example, "hi"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }
/** * Use the Microsoft service to translate the given text from the given source language to the given target. * You must set the client keys in translator.microsoft.properties. * * @param text The text to translate. * @param sourceLanguage The input text language (for example, "en"). * @param targetLanguage The desired language to translate to (for example, "fr"). * @return The translated text. If translation is unavailable, returns the unchanged text. * @throws Exception * @see org.apache.tika.language.translate.Translator * @since Tika 1.6 */ public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!available) return text; Language source = Language.fromString(sourceLanguage); Language target = Language.fromString(targetLanguage); Translate.setClientId(clientId); Translate.setClientSecret(clientSecret); try { return Translate.execute(text, source, target); } catch (Exception e) { throw new TikaException("Error with Microsoft Translation: " + e.getMessage()); } }