CultureInfo in JavaScript

前端 未结 3 1405
轮回少年
轮回少年 2021-01-11 11:26

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

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

Can any

相关标签:
3条回答
  • 2021-01-11 11:34

    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:

    <script>
      var cultureInfo = '<%= System.Globalization.CultureInfo.CurrentCulture.Name %>';
    </script>
    
    0 讨论(0)
  • 2021-01-11 11:37

    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.

    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:

    $("#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:

      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:

    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()]));
    
    0 讨论(0)
  • 2021-01-11 11:40

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

    var lang = navigator.language
    
    0 讨论(0)
提交回复
热议问题