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 SummaryFieldsModifier and TypeFieldDescription- protected StringThe API key "Server locked IP", null when not configured.
- Constructor SummaryConstructors
- Method SummaryModifier and TypeMethodDescription- boolean- canTranslate- (String sourceLanguageCode, String targetLanguageCode) Checks if a translation is handled between two language codes.- voidDeconfigures 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.- voidUpdates the translator with the supported languages by means of requesting the information from the service.
- Field Details- serverLockedIP_API_KeyThe API key "Server locked IP", null when not configured.
 
- Constructor Details- GoogleTranslatepublic GoogleTranslate()
 
- Method Details- getLanguagesGets 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.
 
- getLanguagesGets the languages that Google supports translations operations with.- Parameters:
- key- The API key from Google.
- language- The language (- Language.Auto_detectis not allowed).
- Returns:
- The array of Languages supported for translation.
- Throws:
- RequestException- For Google Translate request errors.
 
- getLanguagesGets 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.
 
- detectLanguagesDetects 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.
 
- translatepublic 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
 
- getReadyStateGets the ready state of this translator.- Specified by:
- getReadyStatein interface- ITranslator
- Returns:
- An error message describing the state (e.g. "Missing API key"), or null for OK.
 
- updateSupportedLanguagespublic 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:
- updateSupportedLanguagesin interface- ITranslator
 
- getLanguageCodeGets the "real" language code, no matter the input case.- Specified by:
- getLanguageCodein interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- The potentially changed language code if the case was wrong, or null if language is not supported.
 
- deconfigurepublic void deconfigure()Deconfigures the translator to "lose" the API key(s).- Specified by:
- deconfigurein interface- ITranslator
 
- getNameReturns the name of this translator.- Specified by:
- getNamein interface- ITranslator
- Returns:
- A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
 
- isLanguageHandledVerifies if a Language Code is handled by this translator.- Specified by:
- isLanguageHandledin interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- true if supported, false if not.
 
- getLanguageDescriptionGets the language description for a language code, e.g. "sv" would be "Swedish".- Specified by:
- getLanguageDescriptionin interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- The language description the current Eclipse + IIZI language (English only for now).
 
- canTranslateChecks if a translation is handled between two language codes.- Specified by:
- canTranslatein interface- ITranslator
- Parameters:
- sourceLanguageCode- The source language code.
- targetLanguageCode- The target language code.
- Returns:
- true if supported, false if not.
 
- translatepublic Translation translate- (KString sourceText, String sourceLanguageCode, String targetLanguageCode) throws TranslationException, RequestException Translates a text in plain text or HTML.- Specified by:
- translatein interface- ITranslator
- 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.