Class TranslationEngine

java.lang.Object
com.iizix.translate.TranslationEngine
All Implemented Interfaces:
ITranslationEngine
Direct Known Subclasses:
EditorTranslationEngine, TestEngine

public abstract class TranslationEngine extends Object implements ITranslationEngine
The translation engine: two current translators are provided: Google and Microsoft, but the interface allows others. This engine is built to provide support to use multiple translators in parallel, with its own language capabilities and this engine would select the most suitable one to use.
Author:
Christopher Mindus
  • Field Details

    • ENCODING

      public static final Charset ENCODING
      Always UTF-8 encoded.
    • ENCODING_NAME

      public static final String ENCODING_NAME
      The "UTF-8" string.
    • DEBUG

      public static boolean DEBUG
      Debug mode.
  • Constructor Details

    • TranslationEngine

      protected TranslationEngine()
      Constructor.
      Throws:
      InternalError - if the engine is already instantiated.
    • TranslationEngine

      protected TranslationEngine(boolean recreate)
      Constructor.

      A registered engine of the SAME class but from ANOTHER class loader is replaced by this one, with an INFO line: it belongs to an application that has been reloaded (hot deploy) without its engine having been released (belt and braces to releaseEngine(ClassLoader)). A registered engine of another class is refused as before.

      Parameters:
      recreate - Flag to replace a registered engine, whatever its class.
      Throws:
      InternalError - if an engine of another class is already registered and recreate is false.
  • Method Details

    • getInstance

      public static ITranslationEngine getInstance()
      Gets the translation engine instance.

      An engine that has been released, because the application that registered it was unloaded or reloaded, is never returned: the reply is then null or the engine registered since.

      Returns:
      The engine, or null if no engine is registered.
    • releaseEngine

      public static boolean releaseEngine(ClassLoader loader)
      Releases the registered engine if its class was defined by the class loader loader or by a class loader that has loader among its parents. Called by the server when an application is unloaded or reloaded (hot deploy), so that the engine the application registered does not outlive it: the reloaded application registers its own engine anew, and the released engine no longer keeps the old class loader reachable from this class.

      An engine registered through another class loader (another application, the Designer) is left alone.

      Parameters:
      loader - The class loader of the application that is unloaded, null for none.
      Returns:
      true if an engine was released, false if no engine is registered or the registered engine does not belong to the class loader.
    • isReleased

      public final boolean isReleased()
      Checks if this engine has been released: the application that registered it was unloaded or reloaded, or an engine of the same class from another class loader replaced it, and getInstance() no longer returns it.
      Returns:
      true if released, false if not.
    • getQueues

      public TranslationQueue[] getQueues()
      Gets the translation queues.
      Specified by:
      getQueues in interface ITranslationEngine
      Returns:
      The array of translation queues.
    • clearLanguageQueue

      protected void clearLanguageQueue()
      Clears the language queue after reconfiguration.
    • canTranslate

      public boolean canTranslate(String sourceLanguageCode, String targetLanguageCode)
      Checks if a translation is handled between two language codes.
      Specified by:
      canTranslate in interface ITranslationEngine
      Parameters:
      sourceLanguageCode - The source language code, null for automatic detection.
      targetLanguageCode - The target language code.
      Returns:
      true if supported, false if not.
    • cancel

      public void cancel()
      Cancels pending or running translation job.
      Specified by:
      cancel in interface ITranslationEngine
    • translate

      public boolean translate(KString sourceText, String sourceLanguageCode, String targetLanguageCode, ITranslationReply replyCallback)
      Translates a text in plain text or HTML.
      Specified by:
      translate in interface ITranslationEngine
      Parameters:
      sourceText - The source text to translate.
      sourceLanguageCode - The source language code, null for automatic detection.
      targetLanguageCode - The target language code.
      replyCallback - Callback when translation completes.
      Returns:
      true for success, false for failure (error is logged).
      Throws:
      NullPointerException - If sourceText, targetLanguageCode or replyCallback is null.
    • newTranslationQueue

      protected abstract TranslationQueue newTranslationQueue(String sourceLanguageCode, String targetLanguageCode, ITranslator translator)
      Creates a new TranslationQueue instance.
      Parameters:
      sourceLanguageCode - The source language code, null for auto-detect.
      targetLanguageCode - The target language code.
      translator - The translator.
    • requestJSON

      public JSONObject requestJSON(String httpRequest, Map<String,String> params, Map<String,String> headers, int type, int sizeLimit) throws IOException, JSONException
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      Specified by:
      requestJSON in interface ITranslationEngine
      Parameters:
      httpRequest - The request string.
      params - The parameters, null for none.
      headers - The HTTP headers, null for none.
      type - Type of request: REQTYPE_* value.
      sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
      Returns:
      The parsed JSON object.
      Throws:
      IOException - For I/O errors.
      JSONException - For JSON exceptions.
    • requestJSON

      public JSONObject requestJSON(String httpRequest, String params, Map<String,String> headers, int type, int sizeLimit) throws IOException, JSONException
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      Specified by:
      requestJSON in interface ITranslationEngine
      Parameters:
      httpRequest - The request string.
      params - The parameters, null for none.
      headers - The HTTP headers, null for none.
      type - Type of request: REQTYPE_* value.
      sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
      Returns:
      The parsed JSON object.
      Throws:
      IOException - For I/O errors.
      JSONException - For JSON exceptions.
    • requestString

      public String requestString(String httpRequest, Map<String,String> params, Map<String,String> headers, int type, int sizeLimit) throws IOException
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      Specified by:
      requestString in interface ITranslationEngine
      Parameters:
      httpRequest - The request string.
      params - The parameters, null for none.
      headers - The HTTP headers, null for none.
      type - Type of request: REQTYPE_* value.
      sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
      Returns:
      The reply string.
      Throws:
      IOException - For I/O errors.
    • requestString

      public String requestString(String httpRequest, String params, Map<String,String> headers, int type, int sizeLimit) throws IOException
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      Specified by:
      requestString in interface ITranslationEngine
      Parameters:
      httpRequest - The request string.
      params - The parameters, null for none.
      headers - The HTTP headers, null for none.
      type - Type of request: REQTYPE_* value.
      sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
      Returns:
      The reply string.
      Throws:
      IOException - For I/O errors.
    • postRequestJSON

      public String postRequestJSON(String httpRequest, Map<String,String> headers, String data) throws IOException
      Requests a function using HTTPS POST, configured for proxy as specified in Eclipse or the Server.
      Specified by:
      postRequestJSON in interface ITranslationEngine
      Parameters:
      httpRequest - The request string.
      headers - The HTTP headers, never null.
      data - The data string.
      Returns:
      The reply string.
      Throws:
      IOException - For I/O errors.