Class 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 TypeFieldDescription
      protected java.lang.StringserverLockedIP_API_Key
      The API key "Server locked IP", null when not configured.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      booleancanTranslate​(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
      Checks if a translation is handled between two language codes.
      voiddeconfigure()
      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.StringgetLanguageCode​(java.lang.String languageCode)
      Gets the "real" language code, no matter the input case.
      java.lang.StringgetLanguageDescription​(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.StringgetName()
      Returns the name of this translator.
      java.lang.StringgetReadyState()
      Gets the ready state of this translator.
      booleanisLanguageHandled​(java.lang.String languageCode)
      Verifies if a Language Code is handled by this translator.
      Translationtranslate​(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
      Translates a text in plain text or HTML.
      static Translationtranslate​(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.
      voidupdateSupportedLanguages()
      Updates the translator with the supported languages by means of requesting the information from the service.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • serverLockedIP_API_Key

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

      • GoogleTranslate

        public GoogleTranslate()
    • 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 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 java.lang.String getLanguageCode​(java.lang.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 java.lang.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​(java.lang.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 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 interface ITranslator
        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 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,
                                     java.lang.String sourceLanguageCode,
                                     java.lang.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.