Skip to content
Joshua Monson edited this page Aug 7, 2013 · 1 revision

There are five scripts that deal with translation.

TextTranslator.js

This is the main translation interface. Create the object like this:

var translator = new TextTranslator();

Adding translation engines

You can add defined translation engines like this:

translator.addTranslationEngine(translationEngine, priority);

Invoking the translator by selecting text.

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.

Translating

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.

Events

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.

TranslationEngine.js

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