views:

710

answers:

5

Good morning,

Apologies for the newbie question. I'm just getting started with ASP.NET internationalization settings.

Background info:

I have a website which displays a <table> HTML object. In that <table> HTML object, I have a column which displays dates. My server being in the US, those dates show up as MM/DD/YYYY. Many of my users plug into this webpage through Excel, via the Data --> Import External Data --> Import Web Query interface. My users, for the most part, are in the US, so those dates show up correctly in their Excel screens.

Now I need to make the webpage work for UK users. As is, they are downloading the dates as MM/DD/YYYY, which makes their spreadsheets unusable since their regional settings are set to DD/MM/YYYY.

My question is:

How do I make it so the web server realizes that the incoming request has a en-GB culture setting? I could engineer my own little custom workaround, but I'm sure I'm not the first programmer to come across this. How do the pro's handle this? I'm looking for a solution that would be relatively simple and quick to put up, but I don't want to just put some crappy buggy piece of my own logic togethe that I'm going to dread 6 months from now.

Thanks a lot in advance, -Alan.

+2  A: 

You can allow the browser to set your UI culture automatically if you wish, by opening up the web.config, like this:

<configuration>
   <system.web>    
       <globalization uiCulture="auto" />
       ...

And then the culture set by the browser will be automatically set in your app. This means that when you have the framework display date/time values, they will be formatted according to the current thread's UI Culture.

This will also help if you are using currency and/or localized text (however you have to provide the localized resources for each culture you support).

Ben Scheirman
+1  A: 

You should read this post from Rick.

Bruno Shine
This post was very helpful. See my last comment for how I used it.
AlanR
A: 

Thank you both for the quick responses. Give me a little while to test these out before picking an accepted answer. For now, I'll just vote your answers up.

-Alan

AlanR
+2  A: 

A couple of points:

  • The <globalization> element also needs the attribute culture="auto". The uiCulture attribute affects the language used to retrieve resources. The culture attribute affects the culture used for formatting numbers an dates.

  • As noted in this MSDN article, it is not a best practice to rely exclusively on browser settings to determine the UI culture for a page. Users frequently use browsers that are not set to their preferences (for example, in an Internet cafe). You should provide a method for users to explicitly choose a language or language and culture (CultureInfo name) for the page.

Joe
That's it! Thanks. I couldn't get the web.config to work, but with your first comment it all worked. This way I don't have to write any code. Many, many thanks.
AlanR
+1  A: 

You could also accept a query string parameter for overriding the culture settings.

Culture initialization should go in the Page.InitializeCulture method.

protected override void InitializeCulture ( )
{
  Thread.CurrentThread.CurrentCulture
    = Thread.CurrentThread.CurrentUICulture
    = Request.QueryString [ "culture" ] != null ? new CultureInfo ( Request.QueryString [ "culture" ] ) : CultureInfo.InvariantCulture;
  //base.InitializeCulture ( );
}

Usage: http://tempuri.org/page.aspx?culture=en-GB

baretta
Actually, this web page is being called from an Excel add-in. I wouldn't wanto to much around with detecting the machine's regional settings out of VBA :).
AlanR
Still, very good point on where I should be initializing the culture.
AlanR