views:

228

answers:

2

I'm trying to get my clients' country, so I use CultureInfo.CurrentCulture. Problem is that when my Canadian customers use my website, they're showing up as American.

It looks like CultureInfo.CurrentCulture is returning my server's country instead of their country. So how do I get my clients' country?

A: 

I believe you need to write code to read the user's culture from the incoming browser request, and set your CultureInfo from that.

This fellow describes how they do it: Set the display culture for the current thread to the most appropriate culture from the user's incoming Http "request" object.

He has an excellent discussion there, but this is basically how he does it:

In Page_Load, they make this call: UIUtilities.setCulture(Request);

Where this is what gets called:

/// Set the display culture for the current thread to the most
/// appropriate culture from the user's incoming Http "request" object.
internal static void setCulture(HttpRequest request)
{
    if (request != null)
    {
      if (request.UserLanguages != null)
      {
        if (request.UserLanguages.Length > -1)
        {
          string cultureName = request.UserLanguages[0];
          UIUtilities.setCulture(cultureName);
        }
      }
        // TODO: Set to a (system-wide, or possibly user-specified) default
        // culture if the browser didn't give us any clues.
    }
}

/// Set the display culture for the current thread to a particular named culture.
/// <param name="cultureName">The name of the culture to be set 
/// for the thread</param>
private static void setCulture(string cultureName)
{
    Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(cultureName);
    Thread.CurrentThread.CurrentUICulture = new
        CultureInfo(cultureName);
}
DOK
On the right lines, but note that the Accept-Language header sent by the browser is not necessarily the user's preferred language. I'm in Europe and happen to have a US version of Windows, so my IE8 sends en-US. Also note that the name in the accept-language header may not be a valid .NET culture name, so the call to CreateSpecificCulture should be enclosed in a try/catch.
Joe
no luck. I'm still getting en-us in testing
thchaver
What do you get from Request.UserLanguages? That is, are you sure what language your browser is requesting?
DOK
IE:{Dimensions:[1]} [0]: "en-us"FF:{Dimensions:[2]} [0]: "en-us" [1]: "en;q=0.5"Chrome:{Dimensions:[2]} [0]: "en-US" [1]: "en;q=0.8"Problem is that since I have no idea how to change that, I can't expect my users will know how. So can I assume that my Canadian customers will all have it set the right way? Not so sure.
thchaver
OK, it isn't a reliable assumption that users will understand how a website knows their preferred language and culture. Many non-US websites show little flag images near the top of the page, where users can click to choose their language. Might be an option for your site.
DOK
+1  A: 

You just need to set the culture attribute to auto in your web.config file:

<system.web>
    <globalization culture="auto" />
<system.web>

This will automatically set the CurrentCulture to the client's culture.

You can also set uiCulture to auto if you're using localized resources.

Thomas Levesque