5

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.

rgb
  • 1,750
  • 1
  • 20
  • 35
  • 1
    _"NativeName is not a solution to my problem"_ it would be helful to know why. – Tim Schmelter Apr 20 '15 at 08:57
  • @TimSchmelter Because as a `NativeName` I get: English (United Kingdom), espanol (Espana), polski (Polska). I need all this names in one language, for instance in Polish. – rgb Apr 20 '15 at 09:02
  • 2
    I'm pretty sure you'll have to build your own lookup table. There's a bit of a difference between storing two names for each language, and storing each language's name in every other language - would having an extra million of strings really benefit the .NET framework? :) – Luaan Apr 20 '15 at 09:11
  • 2
    There is no built-in way to translate a culture-name to a different culture's language. Here's a related question(imo a duplicate): http://stackoverflow.com/questions/2432813/how-to-translate-cultureinfo-language-names – Tim Schmelter Apr 20 '15 at 09:12
  • possible duplicate of [Localized region/country names in .NET](http://stackoverflow.com/questions/843055/localized-region-country-names-in-net) – Panagiotis Kanavos Apr 20 '15 at 12:15
  • This question isn't related to LCIDs. Localized framework resources (eg messages and language names) require localized versions of the framework eg [this one for 4.5](https://www.microsoft.com/en-us/download/details.aspx?id=30667) – Panagiotis Kanavos Apr 20 '15 at 12:17
  • @TimSchmelter localized language names come with the .NET Framework language pack. No need for translation or custom coding. – Panagiotis Kanavos Apr 20 '15 at 12:25
  • @PanagiotisKanavos: but if there's no need for custom coding, how do you get the language-name of the english-culure(`new CultureInfo(2057)`) in polish? So: `"2057 - Angielski (Wielka Brytania)"`. I haven't checked Hans approach but you haven't mentioned it in your answer, have you? OP doesn't want the language of the current culture but of a different culture in another culture's language. – Tim Schmelter Apr 20 '15 at 12:28
  • @TimSchmelter install the polish language pack, ensure Thread.CurrentUICulture is set to Polish (done automatically if the user's UI language is Polish), then call NativeName. This will change every Framework text to polish, including error messages which can be ... suprising. – Panagiotis Kanavos Apr 20 '15 at 12:32
  • @PanagiotisKanavos: but that is not OP's requirement. As mentioned, he wants to get the `NativeName` of a different culture than the current, he doesn't want to change the current. But he doesn't want the `NativeName` in the language of that culture but of the other(so not english in english but polish). – Tim Schmelter Apr 20 '15 at 12:33
  • @TimSchmelter actually it is: `How to retrieve names of the languages in language of the current UI thread` – Panagiotis Kanavos Apr 20 '15 at 12:34
  • @PanagiotisKanavos: whoops, i have overlooked that. But it's still not the same, he wants to change the culture to polish but with the effect that he gets an english culture's name also in polish. That's why `NativeName` doesn't work, it only returns the current cultures name in that language not a different cultures name in the current culture's language. – Tim Schmelter Apr 20 '15 at 12:36
  • @TimSchmelter because he doesn't have the language pack installed so the framework falls back to English. Once you install it, the framework will pick the correct localized resources. – Panagiotis Kanavos Apr 20 '15 at 12:38

3 Answers3

1

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Not quite. Since Vista the OS version doesn't matter as you can install multiple languages. The text, messages returned depend on the current user's UI language. – Panagiotis Kanavos Apr 20 '15 at 12:29
  • They can theoretically be available. Doesn't matter much, a Polish user is likely to run the Polish version of Windows and expects the Polish country names. – Hans Passant Apr 20 '15 at 12:32
  • There is no Polish version any more, not since Vista. What you see as a localized version is a different default language defined in a setup answer file. I think Vista *Home* didn't allow installation of language packs but Win 7+ come with most languages already available. – Panagiotis Kanavos Apr 20 '15 at 12:41
  • Hmm, I have no trouble downloading the Polish version of Windows from my MSDN account. You are talking about the Ultimate edition. – Hans Passant Apr 20 '15 at 12:50
1

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

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

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

Hylaean
  • 1,237
  • 13
  • 19