A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
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.
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
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)|
|Zh-CHS||Chinese (Simplified) Legacy|
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.
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.
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:
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:
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:
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
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.Summary
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
Start building with 50+ products and up to 12 months usage for Elastic Compute Service