Localize the jQuery DatePicker Control in an ASP.NET Page

Posted by: Suprotim Agarwal , on 5/13/2010, in Category jQuery and ASP.NET
Views: 72198
Abstract: The jQuery UI DatePicker widget is highly configurable and full of features, and I use it in my applications whenever I can. In this article, we will see how to localize the DatePicker based on the value selected from an ASP.NET DropDownList.
The jQuery UI DatePicker widget is highly configurable and full of features, and I use it in my applications whenever I can. In this article, we will see how to localize the DatePicker based on the value selected from an ASP.NET DropDownList.
For this example, I am using the jQuery UI scripts and CSS hosted on Google CDN. You can always customize your jQuery UI download by selecting the version and specific modules you need from here.
[Note: If you are using jQuery with ASP.NET Controls, you may find my EBook 51 Recipes with jQuery and ASP.NET Controls helpful]
Let us quickly jump to the code and see how to localize the jQuery UI DatePicker control
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Localize a DatePicker</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
    type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"
    type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js"
    type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
        $.datepicker.setDefaults($.datepicker.regional['']);
        $("#datepicker").datepicker($.datepicker.regional['en']);
            $("#ddl").change(function() {
                $('#datepicker').datepicker('option', $.datepicker.regional[$(this).val()]);
            });
        });
      </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <asp:Label ID="lblDate" runat="server" Text="Date: "></asp:Label>
        <asp:TextBox ID="datepicker" runat="server"></asp:TextBox> 
       
        <asp:DropDownList
            id="ddl"
            AppendDataBoundItems="true"           
            DataTextField="DisplayName"
            DataValueField="Name"
            DataSourceID="odsCulture" runat="server">
            <asp:ListItem Value="">Please select a Language</asp:ListItem>
        </asp:DropDownList>
 
        <asp:ObjectDataSource
            id="odsCulture"
            TypeName="System.Globalization.CultureInfo"
            SelectMethod="GetCultures"
            Runat="server">
            <SelectParameters>
             <asp:Parameter Name="types" DefaultValue="NeutralCultures" />
            </SelectParameters>
        </asp:ObjectDataSource> 
    </div>
    </form>
</body>
</html>                                                   
As you can see, we start by referencing the jQuery UI CSS theme (jquery-ui.css), jQuery library (jquery.min.js), jQuery UI library (jquery.ui.min.js) and the jQuery Localization file (jquery-ui-i18n.min.js). This localization roll-up file contains a wide range of different translations in it.
There is a DropDownList control(ddl) that is bound to an ObjectDataSource control. This ObjectDataSource control uses the GetCultures() method of the CultureInfo class to retrieves a list of all the culture names supported by the .NET Framework. Here we are using the Neutral culture type - cultures that are associated with a language but are not specific to a country/region.
Note: I have picked up the code for populating the DropDownList with all the culture names, from Stephen Walther’s excellent book ‘ASP.NET 3.5 Unleashed
Let us examine the final piece of code that localizes the DatePicker control based on the value of the DropDownList
<script type="text/javascript">
    $(function() {
    $.datepicker.setDefaults($.datepicker.regional['']);
    $("#datepicker").datepicker($.datepicker.regional['en']);
        $("#ddl").change(function() {
            $('#datepicker').datepicker('option', $.datepicker.regional[$(this).val()]);
        });
    });
</script>
As shown in the code above, the default language is set using  $.datepicker.setDefaults($.datepicker.regional[''])
and then any localizations (in our case English) is applied to a single instance of the datepicker using $("#datepicker").datepicker($.datepicker.regional['en']);
Finally, on the change function() of the DropDownList, we obtain the value of the selected <option> element using [$(this).val() and then pass this value to the $.datepicker.regional option.
When you run the page, the DatePicker localizes the content in English by default
DatePicker_Language
On selecting a Language that the DatePicker localization supports (example Norwegian), the DatePicker localizes the content in Norwegian
DatePicker_Norweign
Note: If the DatePicker Localization file does not support a language provided in the DropDown, it will ignore the value and display the previously selected content. By the way, you can always easily implement Custom Localization to fill up that gap.
I hope you liked the article and I thank you for viewing it. The entire source code of this article can be downloaded over here
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Rod Laver on Tuesday, May 18, 2010 5:03 AM
Note that the Norwegian style date picker doesn't have the correct format. Weekdays starts with Monday, not Sunday!
Comment posted by Suprotim Agarwal on Wednesday, May 19, 2010 4:42 AM
Thanks Rod. I didn't know that, but I hope the context of the article is clear to all :-)
Comment posted by Rakesh on Thursday, December 9, 2010 6:10 AM
I Found Free Asp.net datepicker available online.

please check following link to view demo and download.

http://worldcode.brinkster.net/Dot_Net_Tools/Calender.aspx


Comment posted by Jim on Friday, April 29, 2011 2:45 PM
Love your work and have your book - tried your example and calendar did not show till I set the css reference back to 1.7 - disturbing that a widely referenced  lib would have this sort of problem ...
Comment posted by skate safari on Saturday, November 24, 2012 3:46 AM
Very cool... people like you is always saving my day! Thank you!
Comment posted by abdel on Thursday, May 22, 2014 9:48 AM
doesn't work unfortunately, please help.


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Localize a DatePicker</title>

    <link href="Styles/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css" />
    <link href="Styles/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" />
  
    <script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.10.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.10.4.min.js" type="text/javascript"></script>
      
    <script type="text/javascript">
        $(function () {
            $.datepicker.setDefaults($.datepicker.regional['']);
            $("#datepicker").datepicker($.datepicker.regional['en']);
            $("#ddl").change(function () {
                $('#datepicker').datepicker('option', $.datepicker.regional[$(this).val()]);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblDate" runat="server" Text="Date: "></asp:Label>
        <asp:TextBox ID="datepicker" runat="server"></asp:TextBox>
        <asp:DropDownList ID="ddl" AppendDataBoundItems="true" DataTextField="DisplayName"
            DataValueField="Name" DataSourceID="odsCulture" runat="server">
            <asp:ListItem Value="">Please select a Language</asp:ListItem>
        </asp:DropDownList>
        <asp:ObjectDataSource ID="odsCulture" TypeName="System.Globalization.CultureInfo"
            SelectMethod="GetCultures" runat="server">
            <SelectParameters>
                <asp:Parameter Name="types" DefaultValue="NeutralCultures" />
            </SelectParameters>
        </asp:ObjectDataSource>
    </div>
    </form>
</body>
</html>
Comment posted by koka on Tuesday, August 26, 2014 9:59 AM
Hello! If you're involved in localization projects and are interested in tools that can help you better manage the string translation process, you might want to check out the online platform https://poeditor.com/
It can automate the translation workflow a lot due to being equipped with API and Translation Memory, and offering a platform for collaborative work.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel