views:

255

answers:

3

I'm not sure if I understand this code or if I'm using it right, but I was under the impression that in an ASP.NET 2.0 AJAX website I could run javascript like:

var c = Sys.CultureInfo.CurrentCulture

and it would give me the culture/language settings the user had specified in their browser at the time of the visit. However, for me, it always comes back 'en-US' no matter what language I pick in firefox or IE.

This serverside code however:

string[] languages = HttpContext.Current.Request.UserLanguages;

if (languages == null || languages.Length == 0)
   return null;

try
{
   string language = languages[0].ToLowerInvariant().Trim();
   return CultureInfo.CreateSpecificCulture(language);
}
catch (ArgumentException)
{
    return null;
}

does return the language I have currently set. But I need to do this clientside, because I need to parse a string into a datetime and do some validations before I postback, and the string could be a MM/DD/YYYY or DD/MM/YYYY, or some other such thing.

What am I missing?

EDIT: Ok, I came across this library http://www.datejs.com/. It looks pretty sweet, and it has ninjas, so I'm pretty much sold already. But in their doc I noticed that you need to include one of the language specific js files to include. I imagine you'd have to make that determination serverside and emit the proper script include tag, which got me thinking. Am I supposed to be doing this with the ASP.NET AJAX stuff? i.e. checking the serverside culture(which works) and setting the Sys.CultureInfo.CurrentCulture in js based on that? I was hoping it would just automagically get it from the browser clientside.

A: 

TMK, the browser does not pass any timezone info to server in the first request. You need to do this with Javascript

TheLQ
The browser always sends the `Accept-Language` header, which ASP.NET uses to determine locale information by default.
Dean Harding
A: 

You could try something along these lines to determine the locale of the browser.

R0MANARMY
I tried that second, and it always comes back en-US as well
LoveMeSomeCode
+1  A: 

The value of Sys.CultureInfo.CurrentCulture is based on a value that is sent from the server. To get the server to send the correct value back to the client, you need to set the EnableScriptGlobalization property to true on the ScriptManager object (it's false by default).

Dean Harding
I set it to true and I still get en-US every time from the JavaScript, and 'English (United Kingdom)' from the server side code. Is there something else I need to set?
LoveMeSomeCode
also, there's EnableScriptGlobalization and EnableScriptLocalization. Neither seem to work.
LoveMeSomeCode