How to translate CultureInfo language names
Asked Answered
K

3

21

I know of three ways to get a full language name of a CultureInfo object.

CultureInfo.DisplayName   
CultureInfo.NativeName  
CultureInfo.EnglishName

DisplayName gives the name in the installed .net language.
NativeName gives the name in 'CultureInfos' language.
EnglishName gives the name in English (surprisingly...)

So for CultureInfo de-DE this gives (on an English .net installation)
German
Deutsch
German

Now my question: Is there a way to ask for the language name of de-DE in another language? E.g. I want the language name of de-DE in Dutch (which would be 'Duits').

Kaplan answered 12/3, 2010 at 12:58 Comment(0)
M
15

This functionality isn't built into the .NET Framework

Maybe look at Google Translate API

Miffy answered 12/3, 2010 at 13:0 Comment(1)
Thats what I was afraid for. The only free translation you get is English, the .NET installation language (if that is different) and the translation in the native CultureInfo language. I'll have to weigh off the gains of using the google translate api to the efforts.Kaplan
S
5

Example for CultureInfo.EnglishName:

    public CultureInfo GetCultureInfo(string EnglishName)
    {
        foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures))
        {
            if (info.EnglishName == EnglishName)
                return new CultureInfo(info.Name);
        }

        return null;
    }
Scibert answered 19/10, 2012 at 9:46 Comment(1)
Thanks, I was looking for a way to get culture info based on the language name, this is what I neededNelda
T
2

In principle the following code works:

private static ResourceManager resourceManager = new ResourceManager("mscorlib", typeof(int).Assembly);

public static string CultureName(CultureInfo culture, CultureInfo displayCulture)
{
    return resourceManager.GetString("Globalization.ci_" + culture.Name, displayCulture);
}

However there are important limitations:

  • It relies on undocumented behaviour, so it can break with updates of Windows or .NET
  • The display language needs to be installed on the Computer you run it on. You can install additional languages, depending on the edition of Windows you're using.

    On most computers there is only one installed language, making this a pointless exercise in most cases.

Trillbee answered 15/5, 2018 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.