Search in sources :

Example 1 with Language

use of com.memetix.mst.language.Language in project tika by apache.

the class MicrosoftTranslator method translate.

/**
     * 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());
    }
}
Also used : TikaException(org.apache.tika.exception.TikaException) Language(com.memetix.mst.language.Language) TikaException(org.apache.tika.exception.TikaException) IOException(java.io.IOException)

Aggregations

Language (com.memetix.mst.language.Language)1 IOException (java.io.IOException)1 TikaException (org.apache.tika.exception.TikaException)1