Monday, August 3, 2009

ASP.NET Globalization

I just finished a project where one of the requirements was to localize completely the input the user gave and the output of values (numbers, date) sent back to the user.

There is a nice way in dot.NET to set current culture information on a thread. You can for example examine the Request.UserLanguages - which is an array of strings and shows you which locales and languages the browser accepted - and then prepare CultureInfo object and assign that object to CurrentThread.

That way you set culture info for every request on the webserver. You use later the culture info information from CurrentThread to parse the user input and to write back localized values to the user.

Set CultureInfo for CurrentThread



The very first step is to set the culture information on CurrentThread on per page request basis. To do so edit function Application_BeginRequest from file Global.aspx.cs.


protected void Application_BeginRequest(object sender, EventArgs e)
{
// you could use Request.UserLanguages
// to set appropriate culture information
System.Globalization.CultureInfo ci = …;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
}



Set ASP.NET AJAX in your Master Page or View Page



Edit file Site.Master (or the MasterPage or ViewPage where the ScriptManager is.
Set EnableScriptGlobalization attribute to "true". Insert bug-fix for Number.parseLocal


<form id="frm" runat="server">
<asp:ScriptManager
ID="ScriptManager1"
runat="server"
EnableScriptGlobalization="true">
<Services>
<asp:ServiceReference Path="~/WebServices/Service.asmx" />
...
</Services>
</asp:ScriptManager>

<script type="text/javascript">
Number.parseLocaleFixed = function(value) {
return Number.parseLocale(value.replace(' ', ' '));
}
</script>



Parse User Input in ASP.NET Ajax


To parse user input of numbers use Number.parseLocaleFixed() javascript function. To parse user input of date use Date.parseLocale() javascript function.
Example:


Var numb = Number.parseLocaleFixed(
document.getELementById(‘txtNumb’).value);



Parse user input in ASPX page



Use the Parse() functions of the expected type. Specify the culture info in Parse(). Take the culture info from CurrentThread.


DateTime dt = DateTime.Parse(s,
System.Threading.Thread
.CurrentThread.CurrentCulture);




Write localized Information to the page



To write localized values to the page use the culture information from CurrentThread:


<%= dt.ToString(“d”, System.Threading.Thread
.CurrentThread.CurrentCulture) %>


To write localized pattern for validation of date input field you can use something like:



CultureInfo ci = System.Threading.Thread
.CurrentThread.CurrentCulture;

// validation of date input
string dSeparator = ci.DateTimeFormat.DateSeparator;
if (dSeparator == ".") dSeparator = @"\.";
string dPatternExpr = String.Format(@"^(|[0-9]{{1,4}}{0}[0-9]{{1,4}}{0}[0-9]{{1,4}})$",
dSeparator);

valSigningDate.ValidationExpression = dPatternExpr;

// validation of number input
string nDecimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;
if (nDecimalSeparator == ".") nDecimalSeparator = @"\.";
string nPatternExpr = String.Format(@"^[-+]?[0-9]*{0}?[0-9]*$",
nDecimalSeparator);

valSinglePayment.ValidationExpression = nPatternExpr;



Atanas Hristov

No comments:

Post a Comment