你能像在 .NET 中一样在 javascript 中获取文化信息字符串吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20520853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:41:26  来源:igfitidea点击:

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

c#javascriptasp.netcultureinfoculture

提问by Tomasz Sikora

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

我需要从浏览器的语言中获取文化字符串。

I thought about getting it from javascript like this:

我想过像这样从 javascript 获取它:

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

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

但它只给了我从 .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" ?

所以javascript给了我“de”或“pl”而不是像.NET中的“de-DE”或“pl-PL”。有没有办法获得“完整信息”?

回答by DaImTo

try this

试试这个

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

回答by Roy Dictus

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

并不真地。浏览器不会在其 DOM 中保留这些详细信息。

What you can do is, in your ASP.NET page, generatethe CultureInfoin C#, like so:

你可以做的是,在你的ASP.NET页面,生成CultureInfoC#中,像这样:

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

   // rest of your JavaScript code
</script>