2010-09-07 14 views
15

Ho usato il seguente codice per ottenere l'elenco del tipo di cultura, è un modo per ottenere solo il nome del paese?Come ottenere il nome del paese

Grazie

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


    } 

Output di esempio:

Spagnolo (Porto Rico)

Spagnolo (Stati Uniti)

risposta

4

Beh, questa espressione regolare sembra fare il lavoro nella maggior parte dei casi:

 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 fornisce nomi come "tedesco", "catalano", "finlandese" ecc. Questi non sono esattamente i nomi dei paesi. Altrimenti, potremmo usare DisplayName o EnglishName. –

+0

Nella maggior parte dei casi DisplayName include il nome del paese/regione circondato da parentesi, è quest'ultima porzione che stiamo ottenendo con l'espressione regolare. Un po 'di trucco, ma funziona :-) –

48

È possibile utilizzare la proprietà Name di CultureInfo per creare una RegionInfo. È quindi possibile utilizzare la proprietà DisplayName. Prova:

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

Dovresti usare ** 'new RegionInfo (ci.LCID)' **, è più veloce. Fonte: decompilatore. Collegamenti su MSDN: [CultureInfo.LCID] (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.lcid (v = vs.80) .aspx) e [Costruttore RegionInfo] (http://msdn.microsoft.com/en-us/library/3ftdh74h(v=vs.110).aspx). –

+5

Ricorda di usare LCID se hai colture personalizzate - tutte hanno LCID = 4096. – nom

0

questo sarebbe quello che stai cercando:

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

o pronti per l'uso senza commenti:

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

Uso di 'Keys.ToList(). Contains()' è una cattiva idea poiché rende l'algoritmo O (n^2). Perché non usare semplicemente 'containsKey()'? – jbindel

+0

Thx @jbindel, ho aggiornato lo 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")); 

    } 

}