2010-09-07 25 views
15

Tôi đã sử dụng mã bên dưới để lấy danh sách loại văn hóa, đó là cách để chỉ lấy tên quốc gia?Cách lấy tên quốc gia

Cảm ơn bạn

 static void Main(string[] args) 
     { 

     StringBuilder sb = new StringBuilder(); 

     foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
     { 
      sb.Append(ci.DisplayName); 
      sb.AppendLine(); 
     } 
     Console.WriteLine(sb.ToString()); 
     Console.ReadLine(); 


    } 

Sample Output:

Tây Ban Nha (Puerto Rico)

Tây Ban Nha (Hoa Kỳ)

Trả lời

4

Vâng, biểu thức chính quy này dường như thực hiện công việc trong hầu hết các trường hợp:

 var regex = new System.Text.RegularExpressions.Regex(@"([\w+\s*\.*]+\))"); 
     foreach (var item in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
     { 
      var match = regex.Match(item.DisplayName); 
      string countryName = match.Value.Length == 0 ? "NA" : match.Value.Substring(0, match.Value.Length - 1); 
      Console.WriteLine(countryName); 
     } 
+1

Tên hiển thị cung cấp các tên như "tiếng Đức", "Tiếng Catalan", "Phần Lan" vv Đây không phải là tên quốc gia chính xác. Nếu không, chúng tôi có thể sử dụng DisplayName hoặc EnglishName. –

+0

Trong hầu hết các trường hợp DisplayName bao gồm tên quốc gia/khu vực được bao quanh bởi dấu ngoặc đơn, đó là phần cuối cùng mà chúng tôi đang nhận được với cụm từ thông dụng. Một chút hack, nhưng nó hoạt động :-) –

48

Bạn có thể sử dụng thuộc tính Tên của CultureInfo để tạo một RegionInfo. Sau đó, bạn có thể sử dụng thuộc tính DisplayName. Hãy thử:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
{ 
      var ri = new RegionInfo(ci.Name); 
      Console.WriteLine(ri.DisplayName); 
} 
+2

Bạn nên sử dụng ** 'RegionInfo mới (ci.LCID)' **, nó nhanh hơn. Nguồn: decompiler. Các liên kết trên MSDN: [CultureInfo.LCID] (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.lcid (v = vs.80) .aspx) và [RegionInfo constructor] (http://msdn.microsoft.com/en-us/library/3ftdh74h(v=vs.110).aspx). –

+5

Hãy nhận biết việc sử dụng LCID nếu bạn có văn hóa tùy chỉnh - tất cả đều có LCID = 4096. – nom

0

này sẽ là những gì bạn đang tìm kiếm:

 foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
     { 
      sb.Append(ci.EnglishName); 
      sb.AppendLine(); 
     } 
1
// Build your normal dictionary as container 
Dictionary<string, string> countryNames = new Dictionary<string, string>(); 
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
{ 
    RegionInfo ri = new RegionInfo(ci.Name); 
    // Check if the ISO language code is already in your collection 
    // Because you don't want double entries in a country box because we had to use the culture info 
    if (!countryNames.ContainsKey(ri.TwoLetterISORegionName)) 
    { 
     countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName); 
    } 
} 
// Code for dictionary to dropdownlist transform can be written with your personal preference for symantics 
SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value"); 

Hoặc sẵn sàng để sử dụng mà không cần bình luận:

Dictionary<string, string> countryNames = new Dictionary<string, string>(); 
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
{ 
    RegionInfo ri = new RegionInfo(ci.Name); 
    if (!countryNames.ContainsKey(ri.TwoLetterISORegionName)) countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName); 
} 
SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value"); 
+2

Sử dụng 'Keys.ToList(). Chứa()' là một ý tưởng tồi vì nó làm cho thuật toán O (n^2). Tại sao không chỉ sử dụng 'containsKey()'? – jbindel

+0

Thx @jbindel, tôi đã cập nhật snip :) – Lesage

0
 You will need to use following namespaces 

    using System.Configuration; 
    using System.Globalization;  

///

/// populate country name 

    /// </summary> 

    /// <param name="dropDown"></param> 

    public static void GetCountryNames(DropDownList dropDown) 

    { 

     Hashtable h = new Hashtable(); 



     Dictionary<string, string> objDic = new Dictionary<string, string>(); 

     foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 

     { 

      RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name); 

      if (!objDic.ContainsKey(objRegionInfo.EnglishName)) 

      { 

       objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower()); 

      } 

     } 



     SortedList<string, string> sortedList = new SortedList<string, string>(objDic); 



     foreach (KeyValuePair<string, string> val in sortedList) 

     { 

      dropDown.Items.Add(new ListItem(val.Key, val.Key)); 

     } 



     dropDown.Items.Insert(0, new ListItem("Select", "Select")); 

     dropDown.Items.Insert(1, new ListItem("Other Country", "Other")); 

    } 

}