如何从C#中的国家/地区代码获取语言的本地名称(地名)?

时间:2018-12-24 04:37:25

标签: c# localization globalization cultureinfo

我可以从国家/地区代码中获得语言名称吗?我创建了一个程序,允许用户选择本地化以显示货币。这是执行配置的程序。

CultureInfo[] cinfo = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
Dictionary<string, string> Misc_dictLocalization = new Dictionary<string, string>();
var sortList = cinfo.OrderBy(p => p.EnglishName).ToList();
foreach (CultureInfo cul in sortList)
{
    //Add country name and bind with country code
    Misc_dictLocalization.Add(cul.Name, cul.EnglishName);
}
Misc_LocalizationCombobox.DataSource = new BindingSource(Misc_dictLocalization, null);
Misc_LocalizationCombobox.DisplayMember = "Value";
Misc_LocalizationCombobox.ValueMember = "Key";

在“组合框”选择上,

string key = ((KeyValuePair<string, string>)Misc_LocalizationCombobox.SelectedItem).Key;
string value = ((KeyValuePair<string, string>)Misc_LocalizationCombobox.SelectedItem).Value;
Misc_CountryCode.Text = key;
decimal dec = Convert.ToDecimal(Misc_DecimalNumber.Text);
Misc_CurrencyFormat.Text = dec.ToString("C", new CultureInfo(Misc_CountryCode.Text));

enter image description here

我当前正在使用

var allCultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
string countrycode = "ar-SA";
string langname = allCultures.FirstOrDefault(c => c.Name == countrycode).DisplayName;
//langname will have "Arabic"

如何从国家/地区代码获取本地名称(地名)?

基于维基百科List of ISO 639-1 codes, 我需要得到;

1)菲律宾(fil-PH)=>他加禄语

2)越南(vi-VN)=>廷维特(tệngViệt)

3)美国(en-US)=>英语

4)阿拉伯语(ar-SA)=>العربية

已解决的问题(信贷Nhan Phan) ..就像我想要的 Language resources folder

DirectoryInfo di = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Language"));
FileInfo[] Files = di.GetFiles("*.xaml");
int ID = 1;
var allCultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
LocalizationProperty.LangLoc = new List<AppModel.LanguageLocalization>();
foreach (FileInfo file in Files)
{
    string countrycode = file.Name.Replace(".xaml", "").Replace("Resources.", "");
    int index = countrycode.LastIndexOf("-");
    string ISOName = countrycode;
    if (index > 0)
        ISOName = ISOName.Substring(0, index);
    string langname = allCultures.FirstOrDefault(c => c.Name == ISOName).NativeName;
    AppModel.LanguageLocalization applang = new AppModel.LanguageLocalization
    {
        LanguageID = ID,
        CountryCode = countrycode,
        LanguageName = langname
    };
    LocalizationProperty.LangLoc.Add(applang);
    ID += 1;
}
string langjson = Newtonsoft.Json.JsonConvert.SerializeObject(LocalizationProperty.LangLoc, Formatting.Indented);
LogEvents($"[{PageTitle} Language file retrieved. {langjson}", EventLogEntryType.Information);

Object2JsonString:-

[
   {
      "LanguageID":1,
      "CountryCode":"ar-SA",
      "LanguageName":"العربية"
   },
   {
      "LanguageID":2,
      "CountryCode":"en-US",
      "LanguageName":"English"
   },
   {
      "LanguageID":3,
      "CountryCode":"fil-PH",
      "LanguageName":"Filipino"
   },
   {
      "LanguageID":4,
      "CountryCode":"ms-MY",
      "LanguageName":"Melayu"
   },
   {
      "LanguageID":5,
      "CountryCode":"syr-SY",
      "LanguageName":"ܣܘܪܝܝܐ"
   },
   {
      "LanguageID":6,
      "CountryCode":"ta-IN",
      "LanguageName":"தமிழ்"
   },
   {
      "LanguageID":7,
      "CountryCode":"tt-RU",
      "LanguageName":"Татар"
   },
   {
      "LanguageID":8,
      "CountryCode":"vi-VN",
      "LanguageName":"Tiếng Việt"
   },
   {
      "LanguageID":9,
      "CountryCode":"zh-CN",
      "LanguageName":"中文"
   }
]

视图:- Main select language page

1 个答案:

答案 0 :(得分:3)

您可以通过 NativeName (而不是DisplayName)来检索它:

var allCultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
string countrycode = "ar-SA";
string langname = allCultures.FirstOrDefault(c => c.Name == countrycode).NativeName;