Giter VIP home page Giter VIP logo

jquery.tr's Introduction

jquery.tr

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

Features

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) {
	$.tr.dictionary(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.

API

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

// set dictionary
$.tr.dictionary(dict);

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

// set language
$.tr.language(lg);

// 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!'));

Examples

The examples directory contains some examples:

Changelog

1.1

Removes rest of the former event feature.

Misc

Licensed under a MIT license, see the LICENSE file.

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

jquery.tr's People

Contributors

koltesdigital avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

ruptik

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.