jQuery Localisation

A jQuery plugin that automatically loads JavaScript localisation packages based on the user's language preference. If you find this plugin useful please vote for it on the jQuery site.

The current version is 1.0.4 and is available under the GPL and MIT licences. For more detail see the documentation reference page.

Introduction

Localisation works by appending language indicators to the base package name to download and implement the contained overrides. If no specific language is given, the default setting for the browser is used. For example, setting the language to English - Australian (en-AU) for the greeting package causes the download and evaluation of the greeting-en.js and greeting-en-AU.js files (if they exist) in that order.

As an example the greeting package has the following localisation packages available:

FileLanguageValue
greeting.jsDefaultHello
greeting-en.jsStandard EnglishGood morning
greeting-en-AU.jsAustralian EnglishG'day
greeting-en-US.jsUS EnglishHi
greeting-fr.jsFrenchBonjour

These packages just set a variable (greeting) that is then displayed:

for language

Change to another language

$.localise('js/greeting');
$('#greeting').val(greeting);
$('#languages').val($.localise.defaultLanguage);
$('#changeLocale').change(function() {
	var newLang = $(this).val();
	$.localise('js/greeting', {language: newLang, loadBase: true});
	$('#greeting').val(greeting);
	$('#languages').val(newLang);
});

In the Wild

This tab highlights examples of this plugin in use "in the wild".

To add another example, please contact me (kbwood{at}iinet.com.au) and provide the plugin name, the URL of your site, its title, and a short description of its purpose and where/how the plugin is used.

Quick Reference

A full list of all possible uses is shown below. For more detail see the documentation reference page.

$.localise(package(s), settings)
$.localise(package(s), language, loadBase, path, timeout)

$.localise.defaultLanguage

$.localize(package(s), settings)
$.localize(package(s), language, loadBase, path, timeout)

$.localize.defaultLanguage

Usage

  1. Include the jQuery library in the head section of your page.
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  2. Download and include the jQuery Localisation JavaScript in the head section of your page.
    <script type="text/javascript" src="jquery.localisation.js"></script>
    or the packed version jquery.localisation.pack.js (1.6K vs 4.1K) or minified version jquery.localisation.min.js (1.4K, 0.7K when zipped).
  3. Import your package base code.
    <script type="text/javascript" src="mypackage.js"></script>
  4. Localise your package(s).
    <script type="text/javascript">$.localise('mypackage');</script>

Comments

I am using your code and has made my life very easy.

Parimah Mehrrostami

Contact Keith Wood at kbwood{at}iinet.com.au with comments or suggestions.

Change History

VersionDateChanges
1.0.407 Mar 2009
  • Added path settings to allow files in different locations
  • Extended localise call to accept all parameters directly
1.0.331 Jan 2009
  • localise call now also accepts just language as a string instead of a settings object
  • Added localize equivalent
  • Moved $.defaultLanguage to $.localise.defaultLanguage
1.0.229 Sep 2007
  • Normalise language codes to format aa-AA
1.0.101 Sep 2007
  • Renamed package to jquery.localisation.* to follow jQuery conventions
  • Hid localisation declarations - functionality is only accessible via $.localise
1.0.020 Jun 2007
  • Initial release