Skip to content

A jQuery plugin for doing client-side translations in javascript.

Notifications You must be signed in to change notification settings

newage/jquery-i18n

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

About

jQuery-i18n is a jQuery plugin for doing client-side translations in javascript. It is based heavily on javascript i18n that almost doesn't suck by Marko Samastur, and is licensed under the MIT license.

Installation

You'll need to download the jQuery library, and include it before jquery.i18n.js in your HTML source. See the examples folder for examples.

Usage

Before you can do any translation you have to initialise the plugin with a 'dictionary' (basically a property list mapping keys to their translations).

var my_dictionary = {
    'some text':      'a translation',
    'some more text': 'another translation'
}
$.i18n.setDictionary(my_dictionary);

Once you've initialised it with a dictionary, you can translate strings using the $.i18n._() function, for example:

$('div#example').text($.i18n._('some text'));

or using $('selector')._t() function

$('div#example')._t('some text');

Wildcards

It's straightforward to pass dynamic data into your translations. First, add %s in the translation for each variable you want to swap in :

var my_dictionary = {
    "wildcard example"  : "We have been passed two values : %s and %s."
}
$.i18n.setDictionary(my_dictionary);

Next, pass an array of values in as the second argument when you perform the translation :

$('div#example').text($.i18n._('wildcard example', [100, 200]));

or

$('div#example')._t('wildcard example', [100, 200]);

This will output We have been passed two values : 100 and 200.

Because some languages will need to order arguments differently to english, you can also specify the order in which the variables appear :

var my_dictionary = {
    "wildcard example"  : "We have been passed two values : %2$s and %1$s."
}
$.i18n.setDictionary(my_dictionary);

$('div#example').text($.i18n._('wildcard example', [100, 200]));

This will output: We have been passed two values: 200 and 100.

If you need to explicitly output the string %s in your translation, use %%s :

var my_dictionary = {
    "wildcard example"  : "I have %s literal %%s character."
}
$.i18n.setDictionary(my_dictionary);

$('div#example').text($.i18n._('wildcard example', [1]));

This will output: I have 1 literal %%s character.

Plural Forms

This plugin uses plural forms such as in Gettext.

List of plural forms expressions for many languages

For example form for russian language:

plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)

For the addition plural form is used next function

$.i18n.setPlural('plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)');

For translate string used function _p(), for example:

$.i18n._p('%s day', '%s days', 3);

First parameter - singular form, second parameter - plural form and last parameter - number of plural forms.

An array with translations for multiple forms of the above should contain 3 translations

$.i18n.setDictionary(
    "%s day": {
        0: "%s день",
        1: "%s дня",
        2: "%s дней"
    }
);

Author

Dave Perrett :: [email protected] :: @recurser

Copyright

Copyright (c) 2010 Dave Perrett. See License for details.

About

A jQuery plugin for doing client-side translations in javascript.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%