-
Notifications
You must be signed in to change notification settings - Fork 7
Translation
There are five scripts that deal with translation.
This is the main translation interface. Create the object like this:
var translator = new TextTranslator();
You can add defined translation engines like this:
translator.addTranslationEngine(translationEngine, priority);
You can set up a DOM element so selecting its text will invoke the translator.
translator.attach(DOMNode, sourceLanguage, destinationLanguage, eventData);
The languages should be two-letter language codes as that's what most translation tools accept. eventData
can be anything. It is stored in events that are emitted so you can keep track of where the translation originated.
You can manually invoke a translation with this method:
translator.translate(text, sourceLanguage, destinationLanguage, callback);
When the translation event is emitted, the callback is invoked and the event is passed in as well.
You can add event listeners with translator.addEventListener(event, callback)
.
These are the events emitted by the translator.
- translate - This happens when a translation is started
- translateSuccess - This happens when a translation is successful.
- translateError - This happens when no engine could return a translation.
The translateSuccess event contains the following properties.
Property | Description |
---|---|
text | The text being translated |
translations | An array of translations |
engine | A String describing the engine that was used |
srcLang | The source language code |
destLang | The destination language code |
The translateError event has the text
property containing the text which couldn't be translated.
This is a translation engine class to be used when creating transltation engines. A translation engine must have a name
and a service
method.
The service
method has the following signature:
function (text, srcLang, destLang, callback, error)
The following are descriptions of the arguments:
Argument | Value | Description |
---|---|---|
text | [String] | The text being translated |
srcLang | [String] | The two-letter language code of the text language. |
destLang | [String] | The two-letter language code of the desired translation language. |
callback | [Function] | A method which is called when successfully translated. |
error | [Funtion] | A method which is called if the engine cannot translate the text. |
There are three translation engine instances. They are all registered in the global object.
- ArcliteTranslationEngine.js
- GoogleTranslationEngine.js
- WordReferenceTranslationEngine.js