views:

19

answers:

2

I'm letting users choose their preferred language setting for number, currency and date formats. I've got this implemented in a filter to set the Current(UI)Culture. Now I want to use that culture information on the client to setup jQuery's datepicker.

What would be the most efficient way of getting the configured culture setting to the client on every request?

+1  A: 

You could a helper method that will pull the current user culture from wherever you've stored it (presumably the user session, cookie, ...).

public static class CultureExtensions
{
    public static string GetCulture(this HtmlHelper htmlHelper)
    {
        var session = htmlHelper.ViewContext.HttpContext.Session;
        return session["culture"] as string ?? "en-US";
    }
}

And use this helper in your view:

<script type="text/javascript">
$(function() {
    var culture = '<%: Html.GetCulture() %>';
    // TODO: use the culture to set the datepicker
});
</script>
Darin Dimitrov
A: 

Perhaps save the chosen culture value in a cookie on the client and use the cookie value for the datepicker localization. If the user changes their culture from the one sent in the cookie, just overwrite the cookie value with the next response.

Russ Cam