Can you get culture info string in javascript just like in .NET?

24.9k Views Asked by At

I need to get culture string from browser's language.

I thought about getting it from javascript like this:

var userLang = navigator.language || navigator.userLanguage;

but it gives me only first part of culture info that i would get from .NET:

Thread.CurrentThread.CurrentCulture.Name;

So javascript gives me "de" or "pl" instead of "de-DE" or "pl-PL" like in .NET. Is there a way to get the "full info" ?

2

There are 2 best solutions below

3
On

Not really. The browser doesn't keep this detailed information in its DOM.

What you can do is, in your ASP.NET page, generate the CultureInfo in C#, like so:

<script language="javascript">
   var culture = '<%= System.Globalization.CultureInfo.CurrentCulture.Name %>';
   var cultureDisplayName = '<%= System.Globalization.CultureInfo.CurrentCulture.DisplayName %>';
   // etc

   // rest of your JavaScript code
</script>
0
On

try this

<script>
var cultureInfo = '@System.Globalization.CultureInfo.CurrentCulture.Name';
</script>