How to translate CultureInfo language names

前端 未结 3 1174
心在旅途
心在旅途 2020-11-28 15:14

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

CultureInfo.DisplayName   
CultureInfo.NativeName  
CultureInfo.EnglishName
         


        
相关标签:
3条回答
  • 2020-11-28 15:27

    This functionality isn't built into the .NET Framework

    Maybe look at Google Translate API

    0 讨论(0)
  • 2020-11-28 15:40

    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;
        }
    
    0 讨论(0)
  • 2020-11-28 15:48

    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.

    0 讨论(0)
提交回复
热议问题