Class MicrosoftTranslator

  • All Implemented Interfaces:
    Translator

    public class MicrosoftTranslator
    extends Object
    implements Translator
    Wrapper class to access the Windows translation service. This class uses the com.memetix.mst package as a wrapper for the API calls.
    Since:
    Tika 1.6
    • Constructor Detail

      • MicrosoftTranslator

        public MicrosoftTranslator()
        Create a new MicrosoftTranslator with the client keys specified in resources/org/apache/tika/language/translate/translator.microsoft.properties. Silently becomes unavailable when client keys are unavailable. translator.microsoft.client-id and translator.client-secret must be set in translator.microsoft.properties for translation to work.
        Since:
        Tika 1.6
    • Method Detail

      • translate

        public String translate​(String text,
                                String sourceLanguage,
                                String targetLanguage)
                         throws TikaException,
                                IOException
        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.
        Specified by:
        translate in interface Translator
        Parameters:
        text - The text to translate.
        sourceLanguage - The input text language (for example, "en").
        targetLanguage - The desired language to translate to (for example, "fr").
        Returns:
        The translated text. If translation is unavailable, returns the unchanged text.
        Throws:
        Exception
        TikaException - When there is an error translating.
        IOException
        Since:
        Tika 1.6
        See Also:
        Translator
      • translate

        public String translate​(String text,
                                String targetLanguage)
                         throws TikaException,
                                IOException
        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.
        Specified by:
        translate in interface Translator
        Parameters:
        text - The text to translate.
        targetLanguage - The desired language to translate to (for example, "hi").
        Returns:
        The translated text. If translation is unavailable, returns the unchanged text.
        Throws:
        Exception
        TikaException - When there is an error translating.
        IOException
        Since:
        Tika 1.6
        See Also:
        Translator
      • isAvailable

        public boolean isAvailable()
        Check whether this instance has a working property file and its keys are not the defaults. This is not guaranteed to work, since keys may be incorrect or the webservice may be down.
        Specified by:
        isAvailable in interface Translator
        Returns:
        whether translation will probably work.
      • setId

        public void setId​(String id)
        Sets the client Id for the translator API.
        Parameters:
        id - The ID to set.
      • setSecret

        public void setSecret​(String secret)
        Sets the client secret for the translator API.
        Parameters:
        secret - The secret to set.