JavaScript 中的文化信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14393771/
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-26 21:31:06  来源:igfitidea点击:

CultureInfo in JavaScript

javascript

提问by sharmila

In c# I use the below method in order to get the CultureInfo.

在 C# 中,我使用以下方法来获取 CultureInfo。

System.Globalization.CultureInfo.CurrentCulture.Name // output :en-US

Can any one tell me how can I get the CultureInfo in JavaScript?

谁能告诉我如何在 JavaScript 中获取 CultureInfo?

回答by SharpNoiZy

Very easy way is to render it into the view, like this:

非常简单的方法是将其渲染到视图中,如下所示:

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

This is razor syntax, if you use classic asp.net, then use:

这是剃刀语法,如果您使用经典的 asp.net,则使用:

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

回答by JSON

This is a very old post an is definately in need of an update. All major browsers now support the navigator property. Simply use

这是一篇非常老的帖子,肯定需要更新。所有主要浏览器现在都支持 navigator 属性。只需使用

var lang = navigator.language

回答by DFTR

It depends on your goal. If you want the entire website to be treated as the same culture as your server, you can use System.Globalization.CultureInfo.CurrentCulture.Name only and remove the if-then shorthand within the first code snippet. This is not advisable if you have a global website.

这取决于你的目标。如果您希望将整个网站视为与您的服务器相同的文化,您可以仅使用 System.Globalization.CultureInfo.CurrentCulture.Name 并删除第一个代码片段中的 if-then 简写。如果您拥有全球网站,则不建议这样做。

Include the following in the bottom of your page:

在页面底部包含以下内容:

  <input id="currentCulture" type="hidden" value="<%=((Request.UserLanguages != null && Request.UserLanguages.Length > 0) ? new System.Globalization.CultureInfo(Request.UserLanguages.First(), true).Name : System.Globalization.CultureInfo.CurrentCulture.Name) %>" />

Now you can retrieve the culture info specific to the user, within your javascript, using:

现在,您可以在 javascript 中检索特定于用户的文化信息,使用:

$("#currentCulture").val();   //Jquery
document.getElementById("currentCulture").value; //Pure javascript

Within your code behind, any datetime parsing you require, pass in the culture info provider to the parse and tryparse and Convert.ToDateTime functions by using the below:

在您后面的代码中,您需要的任何日期时间解析,通过使用以下内容将区域性信息提供程序传递给 parse 和 tryparse 和 Convert.ToDateTime 函数:

  CultureInfo info = ((Request.UserLanguages != null && Request.UserLanguages.Length > 0) ? new CultureInfo(Request.UserLanguages.First(), true) : System.Globalization.CultureInfo.CurrentCulture);

Note: if you use Jquery UI and have cultures not included by default (such as en-CA or en-GB), you will have to add them. You can retrieve the code here:

注意:如果您使用 Jquery UI 并且默认情况下不包含文化(例如 en-CA 或 en-GB),则必须添加它们。您可以在此处检索代码:

https://code.google.com/p/dobo/source/browse/trunk/dobo/Kooboo.CMS/Kooboo.CMS.Web/Scripts/jquery-ui-i18n/?r=7

https://code.google.com/p/dobo/source/browse/trunk/dobo/Kooboo.CMS/Kooboo.CMS.Web/Scripts/jquery-ui-i18n/?r=7

You can then include it dynamically by following the below example:

然后,您可以按照以下示例动态包含它:

 $.datepicker.regional['en-CA'] = { "Name": "en-CA", "closeText": "Close", "prevText": "Prev", "nextText": "Next", "currentText": "Today", "monthNames": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ""], "monthNamesShort": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""], "dayNames": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], "dayNamesShort": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], "dayNamesMin": ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], "dateFormat": "dd/mm/yy", "firstDay": 0, "isRTL": false };
 $(".datepick").datepicker($.datepicker.setDefaults($.datepicker.regional[$("#currentCulture").val()]));