-
-
Save uzbekdev1/1f5546383851dae7a7e260bba8b69b45 to your computer and use it in GitHub Desktop.
Setting globalization in jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
//Ask ASP.NET what culture we prefer, because we stuck it in a meta tag | |
var data = $("meta[name='accept-language']").attr("content") | |
//Tell jQuery to figure it out also on the client side. | |
$.global.preferCulture(data); | |
//Tell the validator, for example, | |
// that we want numbers parsed a certain way! | |
$.validator.methods.number = function (value, element) { | |
if ($.global.parseFloat(value)) { | |
return true; | |
} | |
return false; | |
} | |
//Fix the range to use globalized methods | |
jQuery.extend(jQuery.validator.methods, { | |
range: function (value, element, param) { | |
//Use the Globalization plugin to parse the value | |
var val = $.global.parseFloat(value); | |
return this.optional(element) || (val >= param[0] && val <= param[1]); | |
} | |
}); | |
//Setup datepickers if we don't support it natively! | |
if (!Modernizr.inputtypes.date) { | |
if ($.global.culture.name != 'en-us' && $.global.culture.name != 'en') { | |
var datepickerScriptFile = "/Scripts/globdatepicker/jquery.ui.datepicker-" + $.global.culture.name + ".js"; | |
//Now, load the date picker support for this language | |
// and set the defaults for a localized calendar | |
$.getScript(datepickerScriptFile, function () { | |
$.datepicker.setDefaults($.datepicker.regional[$.global.culture.name]); | |
}); | |
} | |
$("input[type='datetime']").datepicker(); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System.Web.Mvc | |
{ | |
public static class LocalizationHelpers | |
{ | |
public static IHtmlString MetaAcceptLanguage<t>(this HtmlHelper<t> html) | |
{ | |
var acceptLanguage = HttpUtility.HtmlAttributeEncode(Threading.Thread.CurrentThread.CurrentUICulture.ToString()); | |
return new HtmlString(String.Format("<meta name="\" accept-language\""="" content="\" {0}\""="">",acceptLanguage)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment