Home > ASP.Net > Getting Global in ASP.Net!

Getting Global in ASP.Net!


 

Yesterday, I had a discussion with my boss and a colleague about getting our ASP.Net web application to use the browser settings for “HTTP_ACCEPT_LANGUAGE”.

Currently our application support for this is very limited. And being in a part of the world that officially supports two languages (French and English) doesn’t not make the job easy for a web developer.

That topic came to my mind after a playing with Microsoft Dynamics CRM 4.0 witch is completely written in .Net Framework 2.0. The way they handle the issue in Dynamics provides a pretty good user experience and a flawless experience as well. The end user can customize language, date, and number format in Dynamics as much as they could in the regional settings of their windows control panel. And Dynamics applications currently support close to 40 different languages!

After doing a bit of research, I found out (thanks to aspalliance.com) that there’s a setting we can put in the web.config file to automatically “bind” the display to the end user browser setting.

Here it is:

<globalization enableClientBasedCulture="true" />

MSDN describes enableClientBasedCulture as follows: “Gets or sets a value indicating whether the Culture and UICulture properties should be based on the AcceptLanguage header field value that is sent by the client browser.” (See it here)

Next question is, suppose I have to display a number in French which should display like this 123 456 789,00 but the web server or database server is set for en-US, what do I do?

Answer is simple, there are 2 cultures working together is .Net: The thread’s culture which represents the server in our case and the UI culture which in the case of an ASP.Net application is still on the server, but is use for rendering output and applying resources using the ResourceManager. So I would have to basically “format” my number from 123,456,789.00 coming from the server in en-US to 123 456 789,00 which is the desired output of my end-user.

There comes in play the second argument of the Convert.To…() method that nobody uses but should, the IFormatProvider. If you pass an instance of a culture (which already implement IFormatProvider), to the Convert To method you’ll get the desired result. Given that nobody started to play with the UICulture somewhere else in the code!

   1:  decimal numberFromTheServer;  //123,456,789.00
   2:  TextBox1.Text = Convert.ToString(numberFromTheServer, CultureInfo.CurrentUICulture);
   3:   //Will return “123 456 789,00” is UI culture is set to fr-CA.

Same will happen for dates!

This is yet another reason why people should quit using the Microsoft.VisualBasic Namespace!

I know this discussion is a bag of worms, but even if half of the blogs and forums out there are saying that CStr and CDate or FormatNumber are faster than their .Net counterparts, I just say to myself that they are lying! Why, because for example CStr() doesn’t do the same work as Convert.ToString(). In my sense the Microsoft.VisualBasic Namespace is present in case you have to play with legacy VB stuff and to provider a smoother migration to .Net.

It doesn’t take into consideration the UI culture! And the following remark is displayed on MSDN when you really want to take a look at it!

image 

http://msdn.microsoft.com/en-us/library/ch47ss2a.aspx

Conclusion

To be as flexible as Dynamics is hard but not totally impossible. I plan to do extra research on how to apply this technique on a whole web application with dozens of pages and dozens of controls. My plan is to come up with an anonymous method that would allow go back and forth from one culture to the other.

Comments or help on this matter are always welcome! 

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: