10

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" ?

Tomasz Sikora
  • 1,649
  • 3
  • 19
  • 30

2 Answers2

8

try this

<script>
var cultureInfo = '@System.Globalization.CultureInfo.CurrentCulture.Name';
</script>
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
6

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>
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • But it seems that this gives me the server lanaugae. What i want to do is to get user browser's language to get proper translation for login page. – Tomasz Sikora Dec 11 '13 at 13:56
  • Try it. In ASP.NET, your page's server-side code runs under the culture of the end user. – Roy Dictus Dec 11 '13 at 14:03
  • I've tried it. But to be honest i have to use DotNetNuke and maybe he somehow overrides this, because I always have en-US which is "default" language in site settings. Unfortunately "enable browser language detection" option does not work at all. So i'm trying to find something to override it. I think I will end up with some javascript mapping and detect language with JS because it seems to be only one option that works for me. – Tomasz Sikora Dec 11 '13 at 14:10