How can I force Localization Culture to en-US for whole application

58,548

Solution 1

The problem you describe is the reason InvariantCulture exists. Rather than change your application's culture, you should do your behind-the scenes data manipulation/persistence with the invariant culture and then let the user's culture determine how values are rendered.

Solution 2

Your code will set the culture of the current thread, but any new threads that are created will not have this culture 'inherited'. You have to set the culture you require yourself. (I believe that any new threads will be created with the installed Windows culture, but I'm prepared to be proved wrong on that.)

This is answered in this post: Is there a way of setting culture for a whole application? All current threads and new threads?

Personally, I find this behaviour annoying, but that's the way it is.

Share:
58,548
Admin
Author by

Admin

Updated on December 10, 2020

Comments

  • Admin
    Admin over 3 years

    I'm having an issue with some byte conversions and a few of my calculations in one of my applications. I was able to contribute it to the person running it having an Italian Culture setting in windows. So my question is: What is the best way to for "en-US" on any computer running my application. I have a code sample below, but I am unsure if any thread I use will inhert it.

    [STAThread]
    static void Main()
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    
        ...
    }
    
  • ouah
    ouah about 9 years
    Actually it makes good sense. Think about ASP.NET where multiple users accessing a site can have separate cultures active at any given point in time. You need to have a baseline to start from if there's fallback when a culture can't be matched.
  • Andy
    Andy about 9 years
    @Rick: That's a fair point. I have no experience in ASP.NET so it's not something I have considered before.
  • Alex Sanséau
    Alex Sanséau about 6 years
    Incorrect about ASP.NET. It would take culture info from the server where the website is running, not from user's machine (how could it have access to that?). The website might take into consideration the HTTP header Accept-Language though, but this is not guaranteed (depends if it's been coded for that and if it offers the requested language)
  • Mark Lopez
    Mark Lopez about 2 years
    Under ASP.NET you can customize the culture on a per-request bases - that's how culture was designed (as was identity). How that occurs is developer discretion (e.g. from what the user agent tells the server). Under ASP.NET Core, the concept is the same (See RequestLocalizationOptions).