Class GoogleTranslate

java.lang.Object
com.iizix.translate.google.GoogleTranslate
All Implemented Interfaces:
ITranslator
Direct Known Subclasses:
GoogleTranslate, TestGoogleTranslate

public abstract class GoogleTranslate extends Object implements ITranslator
Google Translate translator implementation to get supported Languages, detect language(s) and perform translations.
Author:
Christopher Mindus
  • Field Details

    • serverLockedIP_API_Key

      protected String serverLockedIP_API_Key
      The API key "Server locked IP", null when not configured.
  • Constructor Details

    • GoogleTranslate

      public GoogleTranslate()
  • Method Details

    • getLanguages

      public static String[] getLanguages(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(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(String key, 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(String key, 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, 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

      public String getReadyState()
      Gets the ready state of this translator.
      Specified by:
      getReadyState in interface ITranslator
      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 interface ITranslator
    • getLanguageCode

      public String getLanguageCode(String languageCode)
      Gets the "real" language code, no matter the input case.
      Specified by:
      getLanguageCode in 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.
    • deconfigure

      public void deconfigure()
      Deconfigures the translator to "lose" the API key(s).
      Specified by:
      deconfigure in interface ITranslator
    • getName

      public String getName()
      Returns the name of this translator.
      Specified by:
      getName in interface ITranslator
      Returns:
      A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
    • isLanguageHandled

      public boolean isLanguageHandled(String languageCode)
      Verifies if a Language Code is handled by this translator.
      Specified by:
      isLanguageHandled in interface ITranslator
      Parameters:
      languageCode - The language code.
      Returns:
      true if supported, false if not.
    • getLanguageDescription

      public String getLanguageDescription(String languageCode)
      Gets the language description for a language code, e.g. "sv" would be "Swedish".
      Specified by:
      getLanguageDescription in interface ITranslator
      Parameters:
      languageCode - The language code.
      Returns:
      The language description the current Eclipse + IIZI language (English only for now).
    • canTranslate

      public boolean canTranslate(String sourceLanguageCode, String targetLanguageCode)
      Checks if a translation is handled between two language codes.
      Specified by:
      canTranslate in interface ITranslator
      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 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.