Package com.iizix.translate.google
Class GoogleTranslate
- java.lang.Object
- com.iizix.translate.google.GoogleTranslate
- All Implemented Interfaces:
ITranslator
- Direct Known Subclasses:
GoogleTranslate
,TestGoogleTranslate
public abstract class GoogleTranslate extends java.lang.Object implements ITranslator
Google Translate translator implementation to get supported Languages, detect language(s) and perform translations.- Author:
- Christopher Mindus
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
serverLockedIP_API_Key
The API key "Server locked IP", null when not configured.
Constructor Summary
Constructors Constructor Description GoogleTranslate()
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canTranslate(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
Checks if a translation is handled between two language codes.void
deconfigure()
Deconfigures the translator to "lose" the API key(s).static DetectedLanguage[]
detectLanguages(java.lang.String key, java.lang.String string)
Detects the languages in a string using Google Translate.java.lang.String
getLanguageCode(java.lang.String languageCode)
Gets the "real" language code, no matter the input case.java.lang.String
getLanguageDescription(java.lang.String languageCode)
Gets the language description for a language code, e.g.static java.lang.String[]
getLanguages(java.lang.String key)
Gets the array of languages supported by Google Translate (online).static LanguageInfo[]
getLanguages(java.lang.String key, Language language)
Gets the languages that Google supports translations operations with.static LanguageInfo[]
getLanguages(java.lang.String key, java.lang.String langID)
Gets the languages that Google supports translations operations with.java.lang.String
getName()
Returns the name of this translator.java.lang.String
getReadyState()
Gets the ready state of this translator.boolean
isLanguageHandled(java.lang.String languageCode)
Verifies if a Language Code is handled by this translator.Translation
translate(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
Translates a text in plain text or HTML.static Translation
translate(ITranslator translator, java.lang.String key, KString kstring, java.lang.String sourceLangID, java.lang.String targetLangID)
Translates a string from one language (that can be auto detected) to another using Google Translate.void
updateSupportedLanguages()
Updates the translator with the supported languages by means of requesting the information from the service.
Method Detail
getLanguages
public static java.lang.String[] getLanguages(java.lang.String key) throws RequestException
Gets the array of languages supported by Google Translate (online).- Parameters:
key
- The API key from Google.- Returns:
- The language ID array.
- Throws:
RequestException
- For Google Translate request errors.
getLanguages
public static LanguageInfo[] getLanguages(java.lang.String key, Language language) throws RequestException
Gets the languages that Google supports translations operations with.- Parameters:
key
- The API key from Google.language
- The language (Language.Auto_detect
is not allowed).- Returns:
- The array of Languages supported for translation.
- Throws:
RequestException
- For Google Translate request errors.
getLanguages
public static LanguageInfo[] getLanguages(java.lang.String key, java.lang.String langID) throws RequestException
Gets the languages that Google supports translations operations with.- Parameters:
key
- The API key from Google.langID
- The language ID.- Returns:
- The array of Languages supported for translation.
- Throws:
RequestException
- For Google Translate request errors.
detectLanguages
public static DetectedLanguage[] detectLanguages(java.lang.String key, java.lang.String string) throws RequestException
Detects the languages in a string using Google Translate.- Parameters:
key
- The API key from Google.string
- The string to detect the languages for.- Returns:
- The array of possible languages.
- Throws:
RequestException
- For Google Translate request errors.
translate
public static Translation translate(ITranslator translator, java.lang.String key, KString kstring, java.lang.String sourceLangID, java.lang.String targetLangID) throws TranslationException, RequestException
Translates a string from one language (that can be auto detected) to another using Google Translate.- Parameters:
translator
- The translator instance.key
- The API key from Google.kstring
- The KString to translate.sourceLangID
- Source language ID or null for auto detect.targetLangID
- Target language ID.- Returns:
- The translation.
- Throws:
RequestException
- For Google Translate request errors.TranslationException
getReadyState
public java.lang.String getReadyState()
Gets the ready state of this translator.- Specified by:
getReadyState
in interfaceITranslator
- Returns:
- An error message describing the state (e.g. "Missing API key"), or null for OK.
updateSupportedLanguages
public void updateSupportedLanguages()
Updates the translator with the supported languages by means of requesting the information from the service. If the request fails, it will be logged in the server.- Specified by:
updateSupportedLanguages
in interfaceITranslator
getLanguageCode
public java.lang.String getLanguageCode(java.lang.String languageCode)
Gets the "real" language code, no matter the input case.- Specified by:
getLanguageCode
in interfaceITranslator
- Parameters:
languageCode
- The language code.- Returns:
- The potentially changed language code if the case was wrong, or null if language is not supported.
deconfigure
public void deconfigure()
Deconfigures the translator to "lose" the API key(s).- Specified by:
deconfigure
in interfaceITranslator
getName
public java.lang.String getName()
Returns the name of this translator.- Specified by:
getName
in interfaceITranslator
- Returns:
- A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
isLanguageHandled
public boolean isLanguageHandled(java.lang.String languageCode)
Verifies if a Language Code is handled by this translator.- Specified by:
isLanguageHandled
in interfaceITranslator
- Parameters:
languageCode
- The language code.- Returns:
- true if supported, false if not.
getLanguageDescription
public java.lang.String getLanguageDescription(java.lang.String languageCode)
Gets the language description for a language code, e.g. "sv" would be "Swedish".- Specified by:
getLanguageDescription
in interfaceITranslator
- Parameters:
languageCode
- The language code.- Returns:
- The language description the current Eclipse + IIZI language (English only for now).
canTranslate
public boolean canTranslate(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
Checks if a translation is handled between two language codes.- Specified by:
canTranslate
in interfaceITranslator
- Parameters:
sourceLanguageCode
- The source language code.targetLanguageCode
- The target language code.- Returns:
- true if supported, false if not.
translate
public Translation translate(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode) throws TranslationException, RequestException
Translates a text in plain text or HTML.- Specified by:
translate
in interfaceITranslator
- Parameters:
sourceText
- The source text to translate.sourceLanguageCode
- The source language code, null for automatic detection.targetLanguageCode
- The target language code.- Returns:
- The translation.
- Throws:
RequestException
- If the translation web service failed.TranslationException
- An exception for translation failure.