CultureInfo.CurrentCulture is giving me the wrong culture
Asked Answered
G

3

10

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?

Guesswarp answered 28/6, 2010 at 17:9 Comment(0)
J
18

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.

Junker answered 25/9, 2010 at 15:12 Comment(1)
i wish i could upvote this more than once, thanks. Took me 3 hours to get this working :)Rogers
D
2

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);
}
Dhiren answered 28/6, 2010 at 17:25 Comment(4)
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.Localism
What do you get from Request.UserLanguages? That is, are you sure what language your browser is requesting?Dhiren
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.Guesswarp
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.Dhiren
D
2

In my case my machine originally had English - UK installed. I added the English - US language and set it as default. I also verified that US was set correctly in the registry. Sadly, System.Threading.Thread.CurrentThread.CurrentCulture still displayed the wrong culture, UK. I discovered you need to set the language options. Download the language pack, handwriting and speech.

Even then the culture was incorrect. The UK would show up all over the machine and after I installed the US language pack the start menu went completely nuts. I gave up and reinstalled the OS using an english-US version.

Danseuse answered 1/12, 2016 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.