2010-09-07 39 views
15

Ich habe den unten stehenden Code verwendet, um die Liste des Kulturtyps zu erhalten. Ist das ein Weg, wie Sie nur den Namen des Landes erhalten?So erhalten Sie den Ländernamen

Danke

 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(); 


    } 

Beispielausgabe:

Spanisch (Puerto Rico)

Spanisch (USA)

Antwort

4

Nun, dieser regulären Ausdruck scheint die Arbeit zu tun in den meisten Fällen:

 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

DisplayName gibt Namen wie "Deutsch", "Katalanisch", "Finnisch" usw. Dies sind nicht gerade Ländernamen. Andernfalls können wir DisplayName oder EnglishName verwenden. –

+0

In den meisten Fällen enthält DisplayName den Namen des Landes/der Region, umgeben von Klammern, es ist der letzte Teil, den wir mit dem regulären Ausdruck erhalten. Ein bisschen Hack, aber es funktioniert :-) –

48

Sie können die Name-Eigenschaft der CultureInfo verwenden, um eine RegionInfo zu erstellen. Sie können dann die DisplayName-Eigenschaft verwenden. Versuchen:

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

Sie sollten ** 'neue RegionInfo (ci.LCID)' ** verwenden, es ist schneller. Quelle: Dekompiler. Links auf der MSDN: [CultureInfo.LCID] (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.lcid (v = vs.80) .aspx) und [RegionInfo-Konstruktor] (http://msdn.microsoft.com/en-us/library/3ftdh74h(v=vs.110).aspx). –

+5

Achten Sie darauf, LCID zu verwenden, wenn Sie benutzerdefinierte Kulturen haben - alle haben LCID = 4096. – nom

0

das wäre, was Sie suchen:

 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"); 

oder gebrauchsfertig ohne Kommentare:

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

Mit 'Keys.ToList(). Contains()' ist eine schlechte Idee, da es den Algorithmus O (n^2) macht. Warum nicht einfach 'containsKey()' verwenden? – jbindel

+0

Thx @jbindel, ich habe den snip aktualisiert :) – 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")); 

    } 

} 
Verwandte Themen