new TranslatorEngine(defaultValopt, defaultConfigopt)
- Description:
Handles how a text or an array of texts is translated from one language to another.
Each translation app has its own protocol, api and method. This class takes care of that and at the same time hopes to make that diversity more uniform.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
defaultVal |
Object |
<optional> |
Default value. Please provide an object with the TranslatorEngine like structure. |
defaultConfig |
Object |
<optional> |
Default user configuration. Please provide an object with the TranslatorEngine like structure. |
Members
author
- Description:
Author of this engine
- Source:
- Default Value:
- Anonymous
Author of this engine
batchDelay
- Description:
Batch delay.
The waiting time between batch. Some server will ban user if you send intense attack like requests.
- Source:
- Default Value:
- 5000
Batch delay.
The waiting time between batch. Some server will ban user if you send intense attack like requests.
delimiter
- Description:
delimiter for each line. Pick character that will be obeyed by translator engine.
The possition of the delimiter for the resulted translation should be intact and unchanged.
if the number of lines of original text doesn't match with translation result...dissaster will be occured!Glue between rows
Example:
Google treat line break as line separator. You should use \n (or \r\n) for google.
- Source:
- Default Value:
- \n
delimiter for each line. Pick character that will be obeyed by translator engine.
The possition of the delimiter for the resulted translation should be intact and unchanged.
if the number of lines of original text doesn't match with translation result...dissaster will be occured!
Glue between rows
Example:
Google treat line break as line separator. You should use \n (or \r\n) for google.
description
- Description:
Description of the engine.
- Source:
Description of the engine.
escapeAlgorithm
- Description:
Escape algorithm to escape subset of string.
- Source:
Escape algorithm to escape subset of string.
id
- Description:
ID of the engine. Must be unique.
- Source:
ID of the engine. Must be unique.
languages
- Description:
Supported languages of the translator
default is whatever google supports
- Source:
Supported languages of the translator
default is whatever google supports
maxRequestLength
- Description:
Maximum request length for each batch in characters.
- Source:
- Default Value:
- 5000
Maximum request length for each batch in characters.
name
- Description:
Display name of the engine
- Source:
Display name of the engine
optionsForm :Object
- Description:
A
JSON Form
object to generate a form for options.
More about JSON Form:
https://github.com/jsonform/jsonform/wiki
Sample and playground
- Source:
A JSON Form
object to generate a form for options.
More about JSON Form:
https://github.com/jsonform/jsonform/wiki
Sample and playground
Type:
- Object
version
- Description:
Engine version
- Source:
Engine version
(static) escapeAlgorithms
- Description:
List of the Code Escape Algorithms
- Source:
List of the Code Escape Algorithms
(static) translators
- Description:
List of the translator engines
- Source:
List of the translator engines
Methods
fetchTranslation(filteredTexts, slopt, tlopt, codeEscapeopt) → {Array.<String>}
- Description:
Translate array of strings by using the current engine.
This function should be overrided with the real handler
- Source:
- Since:
- 5.6.16
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
filteredTexts |
String | Array.<String> | A string or Array of string the text to be translated by using the current engine. Ideally the input is text filtered by CodeEscape |
|
sl |
String |
<optional> |
Source language |
tl |
String |
<optional> |
Target language |
codeEscape |
CodeEscape |
<optional> |
Code escape object |
Returns:
- Translated texts. Must match the index of array of input texts (filteredTexts)
- Type
- Array.<String>
getDefaultOptions(key) → {any}
- Description:
Get the default faule of an option
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
string | The key of the option |
Returns:
- the default value of the option
- Type
- any
getLanguageCode(standardCode, langTableopt) → {String}
- Description:
Convert language code from the unified language code to this engine's language code.
- Source:
- Since:
- 4.10.1
Example
trans.getTranslatorEngine("lingvaNex").getLanguageCode("ja")
// ja_JP
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
standardCode |
String | Standard language code |
|
langTable |
String |
<optional> |
Language table. sl||tl |
Returns:
- This engine's language code
- Type
- String
getOptions(key) → {*}
- Description:
Get option from this translator engine.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
* | Key of the object |
Returns:
Value of the retreived data
- Type
- *
init(e) → {Boolean}
- Description:
Initializing the translator engine
- Source:
Parameters:
Name | Type | Description |
---|---|---|
e |
Event | Event |
Returns:
- True on success
- Type
- Boolean
preProcessText(text, optionsopt) → {Object}
- Description:
Pre process text(s) before sending to translator
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
text |
String | Array.<String> | Text or array of text |
|
options |
Object |
<optional> |
Returns:
Text ready to be sent to tranlator engine
- Type
- Object
resetOptions()
- Description:
Reset the option to their default values
This only affect options set through option form
will not re-render option field
- Source:
translate(text, optionsopt, optionsopt) → {Promise.<TranslationResult>}
- Description:
Method to translate text(s)
This function is to be replaced with a handler for each translator end point/apps.
- Source:
Examples
var result = await trans.getTranslatorEngine("deepl").translate(["こんにちは"])
console.log(result.translation);
// prints an array: ["Hello. - Hello."]
var result = await trans.getTranslatorEngine("sugoitrans").translate(["こんにちは", "こんばんは。"], {sl:"ja", tl:"en"})
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
text |
String | Array.<String> | Text(s) to translate |
||||||||||||||||||||||||||||||||
options |
Object |
<optional> |
{}
|
Options Properties
|
||||||||||||||||||||||||||||||
options |
Object |
<optional> |
Options |
Returns:
- Type
- Promise.<TranslationResult>
translatePairs(obj) → {TranslationPairs}
- Description:
Translate a translation pairs.
Will skip the element that already has translation
- Source:
Parameters:
Name | Type | Description |
---|---|---|
obj |
TranslationPairs | Translation pairs object |
Returns:
- Type
- TranslationPairs
update(key, value)
- Description:
Update a configuration and store it to the system.
The value will be saved and remembered when the system is saved.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
String | Key of the key-value pair |
value |
* | Value of the key-value pair |
(static) addEscapeAlgorithm(name, algorithm)
- Description:
Register an escape algorithm into the system
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name of the escape algorithm |
algorithm |
CodeEscape | CodeEscape class |
(static) getEngine(engineID)
- Description:
Get the engine by their ID
- Source:
Parameters:
Name | Type | Description |
---|---|---|
engineID |
string | Engine ID |