简体   繁体   中英

How to get language name in language of the current thread from lcid in C#

Assume that I have 3 LCID:

  • 2057 - English (United Kingdom)
  • 1034 - Spanish (Spain)
  • 1045 - Polish (Poland)

How to retrieve names of the languages in language of the current UI thread, for example in Polish I expected:

  • 2057 - Angielski (Wielka Brytania)
  • 1034 - Hiszpański (Hiszpania)
  • 1045 - Polski (Polska)

Property NativeName is not a solution to my problem.

LCIDs are deprecated in Windows, it now uses locale identifiers that you are familiar with in .NET, like "en-US". Certainly best to get on that bandwagon. You can still pinvoke the legacy function GetLocaleInfo() to get this information, pass LOCALE_SCOUNTRY to get the localized country name. A sample program:

using System;
using System.Text;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        var buffer = new StringBuilder(256);
        if (GetLocaleInfo(2057, 6, buffer, buffer.Capacity) == 0) {
            throw new System.ComponentModel.Win32Exception();
        }
        Console.WriteLine(buffer.ToString());
        Console.ReadLine();
    }
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int GetLocaleInfo(int LCID, int LCType, StringBuilder buffer, int buflen);
}

Output: United Kingdom

I'm using the en-US version of Windows so get the English names.

I already posted the link to this duplicate question . In short, the language names are .NET resources. If you want the localized versions you need to install the appropriate language pack.

There are different language packs for different frameworks,eg for 4.5 , 4.5.2 . You need to select the appropriate language version before downloading.

The language of the returned resources is determined by the Thread.CurrentUICulture property of the calling thread. This is set to match the current OS user's UI Language setting so you may not need to change anything in your code.

Note that this affects all localizable framework resources, including exception messages like File not found

The unicode consortium does maintain this kind of data. It is not, I believe, built into the .net framework.

You can find it here, along with other data, and info about the xml data. http://cldr.unicode.org/index/downloads

The data is in LDML, which can be used natively by the framework. https://msdn.microsoft.com/en-us/library/ms404373%28v=vs.85%29.aspx

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