13

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

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

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

sharmila
  • 1,493
  • 5
  • 23
  • 42
  • 1
    take a look at http://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser – gherkins Jan 18 '13 at 06:50

3 Answers3

11

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>
SharpNoiZy
  • 1,099
  • 2
  • 11
  • 22
  • 4
    This only works if this is an ASP.NET website. There is no general way to access CultureInfo from JavaScript. – schlingel Feb 02 '18 at 07:58
5

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
JSON
  • 1,113
  • 10
  • 24
0

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()]));
DFTR
  • 861
  • 10
  • 30
  • 1
    System.Globalization.CultureInfo.Name does not return the culture of the web server. In ASP.Net, the page's server-side code runs in the culture of the end user. – flip Nov 11 '14 at 08:36