Class MicrosoftTranslator
- java.lang.Object
-
- org.apache.tika.language.translate.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
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_ID
static String
DEFAULT_SECRET
static String
ID_PROPERTY
static String
PROPERTIES_FILE
static String
SECRET_PROPERTY
-
Constructor Summary
Constructors Constructor Description MicrosoftTranslator()
Create a new MicrosoftTranslator with the client keys specified in resources/org/apache/tika/language/translate/translator.microsoft.properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isAvailable()
Check whether this instance has a working property file and its keys are not the defaults.void
setId(String id)
Sets the client Id for the translator API.void
setSecret(String secret)
Sets the client secret for the translator API.String
translate(String text, String targetLanguage)
Use the Microsoft service to translate the given text to the given target language.String
translate(String text, String sourceLanguage, String targetLanguage)
Use the Microsoft service to translate the given text from the given source language to the given target.
-
-
-
Field Detail
-
PROPERTIES_FILE
public static final String PROPERTIES_FILE
- See Also:
- Constant Field Values
-
ID_PROPERTY
public static final String ID_PROPERTY
- See Also:
- Constant Field Values
-
SECRET_PROPERTY
public static final String SECRET_PROPERTY
- See Also:
- Constant Field Values
-
DEFAULT_ID
public static final String DEFAULT_ID
- See Also:
- Constant Field Values
-
DEFAULT_SECRET
public static final String DEFAULT_SECRET
- See Also:
- Constant Field Values
-
-
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 interfaceTranslator
- 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 interfaceTranslator
- 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 interfaceTranslator
- 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.
-
-