Package com.iizix.translate
Class TranslationEngine
java.lang.Object
com.iizix.translate.TranslationEngine
- All Implemented Interfaces:
- ITranslationEngine
- Direct Known Subclasses:
- EditorTranslationEngine,- TestEngine
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 SummaryFieldsModifier and TypeFieldDescription- static booleanDebug mode.- static final CharsetAlways UTF-8 encoded.- static final StringThe "UTF-8" string.- Fields inherited from interface com.iizix.translate.ITranslationEngine- REQTYPE_POST, REQTYPE_POST_OVERRIDES_GET
- Constructor SummaryConstructorsModifierConstructorDescription- protectedConstructor.- protected- TranslationEngine- (boolean recreate) Constructor.
- Method SummaryModifier and TypeMethodDescription- void- cancel()Cancels pending or running translation job.- boolean- canTranslate- (String sourceLanguageCode, String targetLanguageCode) Checks if a translation is handled between two language codes.- protected voidClears the language queue after reconfiguration.- static ITranslationEngineGets the translation engine instance.Gets the translation queues.- protected abstract TranslationQueue- newTranslationQueue- (String sourceLanguageCode, String targetLanguageCode, ITranslator translator) Creates a new TranslationQueue instance.Requests a function using HTTPS POST, configured for proxy as specified in Eclipse or the Server.Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.- requestJSON- (String httpRequest, Map<String, - String> params, Map<String, - String> headers, int type, int sizeLimit) Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.- requestString- (String httpRequest, String params, Map<String, - String> headers, int type, int sizeLimit) Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.- requestString- (String httpRequest, Map<String, - String> params, Map<String, - String> headers, int type, int sizeLimit) Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.- boolean- translate- (KString sourceText, String sourceLanguageCode, String targetLanguageCode, ITranslationReply replyCallback) Translates a text in plain text or HTML.- Methods inherited from class java.lang.Object- clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait- Methods inherited from interface com.iizix.translate.ITranslationEngine- configureProxy, getProxy, getReadyState, getTranslator, isLanguageHandled
- Field Details- ENCODINGAlways UTF-8 encoded.
- ENCODING_NAMEThe "UTF-8" string.
- DEBUGpublic static boolean DEBUGDebug mode.
 
- Constructor Details- TranslationEngineprotected TranslationEngine()Constructor.- Throws:
- InternalError- if the engine is already instantiated.
 
- TranslationEngineprotected TranslationEngine- (boolean recreate) Constructor.- Throws:
- InternalError- if the engine is already instantiated.
 
 
- Method Details- getInstanceGets the translation engine instance.- Returns:
- The engine.
- Throws:
- NullPointerException- If the engine is not created yet.
 
- getQueuesGets the translation queues.- Specified by:
- getQueuesin interface- ITranslationEngine
- Returns:
- The array of translation queues.
 
- clearLanguageQueueprotected void clearLanguageQueue()Clears the language queue after reconfiguration.
- canTranslateChecks if a translation is handled between two language codes.- Specified by:
- canTranslatein interface- ITranslationEngine
- Parameters:
- sourceLanguageCode- The source language code, null for automatic detection.
- targetLanguageCode- The target language code.
- Returns:
- true if supported, false if not.
 
- cancelpublic void cancel()Cancels pending or running translation job.- Specified by:
- cancelin interface- ITranslationEngine
 
- translatepublic boolean translate- (KString sourceText, String sourceLanguageCode, String targetLanguageCode, ITranslationReply replyCallback) Translates a text in plain text or HTML.- Specified by:
- translatein 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.
 
- newTranslationQueueprotected 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.
 
- requestJSONpublic JSONObject requestJSON- (String httpRequest, Map<String, - String> params, Map<String, throws IOException, JSONException- String> headers, int type, int sizeLimit) 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:
- requestJSONin 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.
 
- requestJSONpublic JSONObject requestJSON- (String httpRequest, String params, Map<String, - String> headers, int type, int sizeLimit) throws IOException, JSONExceptionRequests 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:
- requestJSONin 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.
 
- requestStringpublic String requestString- (String httpRequest, Map<String, - String> params, Map<String, throws IOException- String> headers, int type, int sizeLimit) 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:
- requestStringin 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.
 
- requestStringpublic String requestString- (String httpRequest, String params, Map<String, - String> headers, int type, int sizeLimit) throws IOExceptionRequests 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:
- requestStringin 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.
 
- postRequestJSONpublic String postRequestJSON- (String httpRequest, Map<String, - String> headers, String data) throws IOExceptionRequests a function using HTTPS POST, configured for proxy as specified in Eclipse or the Server.- Specified by:
- postRequestJSONin 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.