JQuery Globalization Plugin from Microsoft multi-language Jquery plug-in written by Microsoft

Source: Internet
Author: User
Tags glob rfc jquery ui datepicker

Last month I blogged about how Microsoft is starting to make code contributions to jquery, and about some of the first code contributions we were working on: jquery templates and data linking support.

Today, we released a prototype of a new jQuery Globalization Plugin that enables you to add globalization support to your JavaScript applications. this plugin is provided by nodes globalization information for over 350 cultures ranging from Scottish Gaelic, Frisian, Hungarian, Japanese, to Canadian English. we will be releasing this plugin to the community as open-source.

You can download our prototype for the jQuery Globalization plugin from our Github repository:


You can also download a set of samples that demonstrate some simple use-cases with it here.

Understanding Globalization

The jQuery Globalization plugin enables you to easily parse and format numbers, currencies, and dates for different cultures in JavaScript. for example, you can use the Globalization plugin to display the proper currency symbol for a culture:

You also can use the globalization plugin to format dates so that the day and month appear in the right order and the day and month names are correctly translated:


Notice above how the Arabic year is displayed as 1431. This is because the year has been converted to use the Arabic calendar ar.

Some cultural differences, such as different currency or different month names, are obvious. other cultural differences are surprising and subtle. for example, in some cultures, the grouping of numbers is done unevenly. in the "te-in" Culture (Telugu in India), groups have 3 digits and then 2 digits. the number 1000000 (one million) is written as ", 000 ". some cultures do not group numbers at all. all of these subtle cultural differences are handled by the jquery globalization plugin automatically.

Getting dates right can be especially tricky. different cultures have different calendars such as the Gregorian and UmAlQura calendars. A single culture can even have multiple calendars. for example, the Japanese culture uses both the Gregorian calendar and a Japanese calendar that has eras named after Japanese emperors. the Globalization Plugin provided des methods for converting dates between all of these different calendars.

Using Language tags

The jQuery Globalization plugin uses the language tags defined in the RFC 4646 and RFC 5646 standards to identity cultures (see http://tools.ietf.org/html/rfc5646 ). A language tag is composed out of one or more subtags separated by hyphens. for example:

Language Tag Language Name (in English)
En-AU English (Australia)
En-BZ English (Belize)
En-CA English (Canada)
Id Indonesian
Zh-CHS Chinese (Simplified) Legacy
Zu IsiZulu

Notice that a single language, such as English, can have several language tags. speakers of English in Canada format numbers, currencies, and dates using different conventions than speakers of English in Australia or the United States. you can find the language tag for a participant culture by usingLanguage Subtag LookupTool located here: http://rishida.net/utils/subtags/

The jQuery Globalization plugin download includes a folder namedGlobinfoThat contains the information for each of the 350 cultures. Actually, this folder contains more than 700 files because the folder limits des both minified and un-minified versions of each file.

For example,GlobinfoFolder templates des JavaScript files named jQuery. glob. en-AU.js for English Australia, jQuery. glob. id. js for Indonesia, and jQuery. glob. zh-CHS for Chinese (Simplified) Legacy.

Example: setting a participant ular Culture

Imagine that you have been asked to create a German website and want to format all of the dates, currencies, and numbers using German formatting conventions correctly in JavaScript on the client. the HTML for the page might look like this:

Notice the span tags above. they mark the areas of the page that we want to format with the Globalization plugin. we want to format the product price, the date the product is available, and the units of the product in stock.

To use the jQuery Globalization plugin, we'll add three JavaScript files to the page: the jQuery library, the jQuery Globalization plugin, and the culture information for a participant language:

In this case, I 've statically added the jQuery.glob.de-DE. js JavaScript file that contains the culture information for German. the language tag "de-DE" is used for German as spoken in Germany.

Now that I have all of the necessary scripts, I can use the globalization plugin to formatProduct price,Date available, AndUnits in stockValues using the following client-side javascript:

The jquery globalization plugin extends the jquery library with new methods-including new methods namedPreferculture ()AndFormat ().ThePreferculture ()Method enables you to set the default culture used by the jquery globalization plugin methods. notice thatPreferculture ()Method accepts a language tag. The method will find the closest culture that matches the language tag.

The$. Format ()Method is used to actually format the currencies, dates, and numbers. The second parameter passed to$. Format ()Method is a format specifier. For example, passing "c" causes the value to be formatted as a currency. The ReadMe file at github details the meaning of all of the varous format specifiers: http://github.com/nje/jquery-glob

When we open the page in a browser, everything is formatted correctly according to German language conventions. A euro symbol is used for the currency symbol. the date is formatted using German day and month names. finally, a period instead of a comma is used a number separator:

You can see a running example of the above approach with the 3_GermanSite.htm file in this samples download.

Example: enabling a user to dynamically select a culture

In the previous example we explicitly said that we wanted to globalize in German (by referencing the jQuery.glob.de-DE. js file ). let's now look at the first of a few examples that demonstrate how to dynamically set the globalization culture to use.

Imagine that you want to display a dropdown list of the all of the 350 cultures in a page. when someone selects a culture from the dropdown list, you want all of the dates in the page to be formatted using the selected culture.

Here's the HTML for the page:

Notice that all of the dates are contained in a <span> tag with a data-date attribute (data-* attributes are a new feature of HTML 5 that conveniently also still work with older browsers ). we'll format the date represented by the data-date attribute when a user selects a culture from the dropdown list.

In order to display dates for any possible culture, we'll include the jQuery. glob. all. js file like this:

The jQuery Globalization plugin includes a JavaScript file named jQuery. glob. all. js. This file contains globalization information for all of the more than 350 cultures supported by the Globalization plugin.At least kb minified, this file is not small. because of the size of this file, unless you really need to use all of these cultures at the same time, we recommend that you add the individual JavaScript files for particle cultures that you intend to support instead of the combined jQuery. glob. all. js to a page. in the next sample I'll show how to dynamically load just the language files you need.

Next, we'll populate the dropdown list with all of the available cultures. We can use the $. cultures property to get all of the loaded cultures:





Finally, we'll write jquery code that grabs every span element with a data-date attribute and format the date:

The jquery globalization plugin'sParsedate ()Method is used to convert a string representation of a date into a javascript date. The plugin'sFormat ()Method is used to format the date. The "D" format specifier causes the date to be formatted using the long date format.

And now the content will be globalized correctly regardless of which of the 350 ages a user visiting the page selects. you can see a running example of the above approach with the 4_selectculture.htm file in this samples download.

Example: loading globalization files dynamically

As mentioned in the previous section, you should avoid adding the jquery. glob. all. JS file to a page whenever possible because the file is so large. A better alternative is to load the globalization information that you need dynamically.

For example, imagine that you have created a dropdown list that displays a list of ages:

The following jquery code executes whenever a user selects a new language from the dropdown list. the code checks whether the globalization file associated with the selected language has already been loaded. if the globalization file has not been loaded then the globalization file is loaded dynamically by taking advantage of the jquery$. Getscript ()Method.


The globalizepage () method is called after the requested globalization file has been loaded, and contains the client-side code to perform the globalization.

The advantage of this approach is that it enables you to avoid loading the entire jquery. glob. all. JS file. instead you only need to load the files that you need and you don't need to load the files more than once.

The 5_Dynamic.htm file in this samples download demonstrates how to implement this approach.

Example: Setting the user preferred language automatically

Descriwebsites detect a user's preferred language from their browser settings and automatically use it when globalizing content. A user can set a preferred language for their browser. then, whenever the user requests a page, this language preference is encoded in the request in the Accept-Language header.

When using Microsoft Internet Explorer, you can set your preferred language by following these steps:

  1. Select the menu optionTools, Internet Options.
  2. SelectGeneralTab.
  3. ClickAgesButton inAppearanceSection.
  4. ClickAddButton to add a new language to the list of ages.
  5. Move your preferred language to the top of the list.

Notice that you can list multiple versions in the Language Preference dialog. All of these versions are sent in the order that you listed them in the Accept-Language header:

Accept-language: fr-fr, ID-ID; q = 0.7, en-US; q = 0.3

Strangely, you cannot retrieve the value of the Accept-Language header from client JavaScript. microsoft Internet Explorer and Mozilla Firefox support a bevy of language related properties exposed by the window. navigator object, such as windows. navigator. browserLanguage and window. navigator. language, but these properties represent either the language set for the operating system or the language edition of the browser. these properties don't enable you to retrieve the language that the user set as his or her preferred language.

The only reliable way to get a user's preferred language (the value of the Accept-Language header) is to write server code. for example, the following ASP. NET page takes advantage of the server Request. userpolicages property to assign the user's preferred language to a client JavaScript variable namedAcceptlanguage(Which then allows you to access the value using client-side JavaScript ):

In order for this code to work, the culture information associated with the value of acceptlanguage must be encoded in the page. for example, if someone's preferred culture is fr-fr (French in France) Then you need to include either the jquery. glob. fr-FR.js or the jquery. glob. all. JS Javascript file in the page or the culture information won't be available. the "6_acceptlanguages.aspx" sample in this samples download demonstrates how to implement this approach.

If the culture information for the user's preferred language is not supported in the page then$. Preferculture ()Method will fall back to using the neutral culture (for example, using jquery. glob. Fr. js instead of jquery. glob. fr-FR.js). If the neutral culture information is not available then$. Preferculture ()Method falls back to the default culture (English ).

Example: using the globalization plugin with the jquery UI datepicker

One of the goals of the Globalization plugin is to make it easier to build jQuery widgets that can be used with different cultures.

We wanted to make sure that the jQuery Globalization plugin cocould work with existing jQuery UI plugins such as the DatePicker plugin. to that end, we created a patched version of the DatePicker plugin that can take advantage of the Globalization plugin when rendering a calendar. the following image into strates what happens when you add the jQuery Globalization and the patched jQuery UI DatePicker plugin to a page and select Indonesian as the preferred culture:

Notice that the headers for the days of the week are displayed using Indonesian day name abbreviations. Furthermore, the month names are displayed in Indonesian.

You can download the patched version of the jQuery UI DatePicker from our github website. Or you can use the version specified in this samples download and used by the 7_DatePicker.htm sample file.


I'm excited about our continuing participation in the jQuery community. This Globalization plugin is the third jQuery plugin that we 've ve released.

We 've really appreciated all of the great feedback and design suggestions on the jQuery templating and data-linking prototypes that we released earlier this year. we also want to thank the jQuery and jQuery UI teams for working with us to create these plugins.

Hope this helps,


P.s. In addition to blogging, I am also now using Twitter for quick updates and to share links. You can follow me:Twitter.com/scottgu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.