Package com.iizix.translate.google
Class GoogleTranslate
java.lang.Object
com.iizix.translate.google.GoogleTranslate
- All Implemented Interfaces:
ITranslator
- Direct Known Subclasses:
GoogleTranslate
,TestGoogleTranslate
Google Translate translator implementation to get supported Languages, detect language(s) and perform translations.
- Author:
- Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionprotected String
The API key "Server locked IP", null when not configured.Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionboolean
canTranslate
(String sourceLanguageCode, String targetLanguageCode) Checks if a translation is handled between two language codes.void
Deconfigures the translator to "lose" the API key(s).static DetectedLanguage[]
detectLanguages
(String key, String string) Detects the languages in a string using Google Translate.getLanguageCode
(String languageCode) Gets the "real" language code, no matter the input case.getLanguageDescription
(String languageCode) Gets the language description for a language code, e.g.static String[]
getLanguages
(String key) Gets the array of languages supported by Google Translate (online).static LanguageInfo[]
getLanguages
(String key, Language language) Gets the languages that Google supports translations operations with.static LanguageInfo[]
getLanguages
(String key, String langID) Gets the languages that Google supports translations operations with.getName()
Returns the name of this translator.Gets the ready state of this translator.boolean
isLanguageHandled
(String languageCode) Verifies if a Language Code is handled by this translator.Translates a text in plain text or HTML.static Translation
translate
(ITranslator translator, String key, KString kstring, String sourceLangID, String targetLangID) Translates a string from one language (that can be auto detected) to another using Google Translate.void
Updates the translator with the supported languages by means of requesting the information from the service.
Field Details
serverLockedIP_API_Key
The API key "Server locked IP", null when not configured.
Constructor Details
GoogleTranslate
public GoogleTranslate()
Method Details
getLanguages
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
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
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
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, String key, KString kstring, String sourceLangID, 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
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
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
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
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
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
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, String sourceLanguageCode, 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.