Created: 2011-07-17 22:52
Updated: 2017-11-15 14:41
License: mit



jquery.tr is a jQuery plugin which enables you to translate text on the client side.


Uses a predefined dictionary

You can get or set the dictionary the plugin uses by calling $.tr.dictionary. It is a object made by key-value pairs, with the original sentence or string id as key, and the translated sentence as value (or a translating function, see below).

Alternatively, you may give an other dictionary when calling $.tr.translator which will be used instead of the global dictionary.

You may also use a hierarchy of dictionaries, by giving the keys to $.tr.transaltor.

Note that you may still use AJAX to retrieve the dictionary, but the plugin itself does not rely on AJAX to translate (it's not its goal).

$.getJSON('dictionary.php', function(data) {

Translates into languages with several plurals

You can write generic sentences, but you may want to customize sentences involving numbers to be more grammatically correct. But some languages have up to six forms of plurals!

By giving functions as dictionary's values, you can achieve this easily. The function has to return a string, used as the translated sentence. Because the function takes the parameters given to the translator, it should uses them to return different strings.

Replaces parameters in translations

Because the order of words may change between languages, the use of parameters is necessary. A parameter is indicated by a & followed by an identifier in the sentences. Give an associative map as second argument to the translator function, which contains identifiers as keys and replacement texts as values.

var tr = $.tr.translator();
var status = tr('&object belongs to &category.', {
	object: item.object,
	category: categories[item.category].name

Alternatively, if identifiers are numbers, you may give the values directly to the translator function. However, don't overuse this syntax: the lack of semantics make it hard to read and maintain.

var tr = $.tr.translator();
var greetings = tr('Hello &1!', name);

Uses cookie information if jQuery.cookie is available

Simply include jquery.cookie.js before jquery.tr.js and that's it. With the cookie plugin, the language will be automatically saved and restored.

Designed to be used by CouchApps

Last but not least, it has been designed with integration with CouchApps in mind.

See the related jquery.couch.tr project.


// get dictionary
var dict = $.tr.dictionary();

// set dictionary

// get language
var lg = $.tr.language();

// set language

// set default language if no previous language has been saved through cookies
$.tr.language(lg, true);

// get translator
var tr = $.tr.translator();
var tr = $.tr.translator('sub-dictionary', ...);
var tr = $.tr.translator(customDictionary, ...);

// using translator
alert(tr('Hello world!'));


The examples directory contains some examples:



Removes rest of the former event feature.


Licensed under a MIT license, see the LICENSE file.

Official site: https://github.com/Bloutiouf/jquery.tr

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more