Can you get culture info string in javascript just like in .NET? [duplicate]
Asked Answered
C

2

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

Corrosion answered 11/12, 2013 at 13:47 Comment(0)
U
8

try this

<script>
var cultureInfo = '@System.Globalization.CultureInfo.CurrentCulture.Name';
</script>
Upsurge answered 11/12, 2013 at 13:50 Comment(0)
F
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>
Flagon answered 11/12, 2013 at 13:50 Comment(3)
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.Corrosion
Try it. In ASP.NET, your page's server-side code runs under the culture of the end user.Flagon
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.Corrosion

© 2022 - 2024 — McMap. All rights reserved.