Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 918
LoveMeSomeCode Avatar asked Dec 01 '25 04:12

LoveMeSomeCode


1 Answers

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

like image 189
Dean Harding Avatar answered Dec 02 '25 17:12

Dean Harding