LoveMeSomeCode
LoveMeSomeCode

Reputation: 3937

Javascript culture always en-us

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.

Upvotes: 3

Views: 7085

Answers (2)

Dean Harding
Dean Harding

Reputation: 72658

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).

Upvotes: 1

TheLQ
TheLQ

Reputation: 15008

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

Upvotes: 0

Related Questions