简体   繁体   中英

Thread.CurrentThread.CurrentUICulture doesn't return country correctly

I'm with a very strange problem. I am implementing localization on my project, but when I try to get the current locale Windows is running, it misses the country information. Here it is a sample code:

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CurrentUICulture;
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        culture.NativeName, culture.Name);
   }
}

When I run it in the most common languages (En-US, FR-fr), it returns correctly. However, when I select French from Belgium, for instance, it retrieves me FR-fr instead of FR-be - even if I delete French from France from the language preference options.

I wonder how could I get the country I selected correctly all the time, no matter which country my software is located.

ps: Using CurrentCulture isn't the answer I'm looking for, since I want a match to the display language I'm using in my UI, not to date/time/number formats (they can be totally different).

I think than you have wrong using in header.

MS use system.thread and not system.globalization https://msdn.microsoft.com/it-it/library/system.globalization.cultureinfo.currentuiculture(v=vs.110).aspx In some of these there are compilation errors.

The correct and compiling code is this:

(notice as CultureInfo.CurrentCulture is readonly, instead i've used System.Threading.Thread.CurrentThread.CurrentCulture that has setter accessible)

public static void Main(string[] args)
{
    // Display the name of the current thread culture.
    Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);

    // Change the current culture to th-TH.
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH", false);
    Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);

    // Display the name of the current UI culture.
    Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);

    // Change the current UI culture to ja-JP.
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP", false);
    Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM