Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
/ transfero Public archive

JavaScript library wrapping the Microsoft Translator services

Notifications You must be signed in to change notification settings

othermedia/transfero

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Transfero

Transfero provides a wrapper library around the JSONP service exposed by the Microsoft Translator services. It has a provider-neutral API for which new backends can be easily built simply by implementing a query method.

Usage

The primary class exposed by the Transfero library is Transfero.Translator. To perform translations, create a new instance of this class and call methods (primarily the translate method) on it.

Since translation services typically require an API key, this must be provided to the constructor as the first argument. If you are using a backend that does not require an API key, just pass in a dummy value.

The languages to be translated from and to must also be provided as the second and third arguments respectively.

var translator = new Transfero.Translator('myapikey', 'French', 'Spanish'),
    inputStr   = "Salut tout le monde";

translator.translate(inputStr, function(translation) {
    console.log(translation);
});

For an extended example of how this can be used in concert with a web user interface to allow users to translate arbitrary blocks of text from one language to another, please refer to the test/ directory.

Adding new backends

Out of the box, Transfero only comes with one translator backend, for the Microsoft Translator services.

New translator backends must be objects with a query method which should accept four parameters: the action name (translate, detect etc.); a parameters object representing key-value pairs to be serialised; a callback; and an object which will be the value of the this keyword within the callback when it is called.

MyTranslatorBackend = {
    query: function(action, params, callback, scope) {
        // Implementation here
    }
};

Note that new backends do not need to wrap JSONP services or web services of any kind; all that is required from the point of the library is that they implement the required interface and call the callback with appropriate values.

A backend can be specified when creating a new Translator object, by passing in the relevant option.

translator = new Transfero.Translator('myapikey', 'fromlang', 'tolang', {
    backend: MyTranslatorBackend
});

Alternatively, it can be modified dynamically by calling setBackend.

translator.setBackend(MyTranslatorBackend);

To change the default backend, simply change the value of the Transfero.Translator.defaultBackend property.

Transfero.Translator.defaultBackend = MyTranslatorBackend;

About

JavaScript library wrapping the Microsoft Translator services

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published