How to get country name
Asked Answered
S

6

18

I used the code below to get the list of culture type, is there a way on how to get just the country name?

Thank you

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:

Spanish (Puerto Rico)

Spanish (United States)

Suttles answered 7/9, 2010 at 4:19 Comment(0)
H
3

Well, this regular expression seems to do the job in most cases:

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);
}
Hellenistic answered 7/9, 2010 at 4:55 Comment(3)
DisplayName gives names like "German", "Catalan", "Finnish" etc. These are not exactly country names. Otherwise, we may use DisplayName or EnglishName.Plutocrat
In most cases DisplayName includes the country/region name surrounded by parenthesis, it's this last portion that we are getting with the regular expression. A bit of hack, but it works :-)Laurelaureano
Display name is not a countryConnecticut
C
54

You can use the Name property of the CultureInfo to construct a RegionInfo. You can then use the DisplayName property. Try:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    var ri = new RegionInfo(ci.Name);
    Console.WriteLine(ri.DisplayName);
}
Constituency answered 9/9, 2010 at 8:23 Comment(2)
You should use new RegionInfo(ci.LCID), it's faster. Source: decompiler. Links on the MSDN: CultureInfo.LCID and RegionInfo constructor.Jeri
Be aware of using LCID if you have custom cultures - they all have LCID = 4096.Smokeless
H
3

Well, this regular expression seems to do the job in most cases:

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);
}
Hellenistic answered 7/9, 2010 at 4:55 Comment(3)
DisplayName gives names like "German", "Catalan", "Finnish" etc. These are not exactly country names. Otherwise, we may use DisplayName or EnglishName.Plutocrat
In most cases DisplayName includes the country/region name surrounded by parenthesis, it's this last portion that we are getting with the regular expression. A bit of hack, but it works :-)Laurelaureano
Display name is not a countryConnecticut
B
3
// 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");

Or ready for use without comments:

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");
Barbel answered 6/3, 2014 at 20:56 Comment(1)
Using Keys.ToList().Contains() is a bad idea since it makes the algorithm O(n^2). Why not just use containsKey()?Kamerun
P
0

this would be what you are looking for:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    sb.Append(ci.EnglishName);
    sb.AppendLine();
}
Postorbital answered 1/9, 2013 at 23:53 Comment(0)
I
0

You will need to use following namespaces

using System.Configuration; 
using System.Globalization;       

/// <summary>
/// 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"));
}
Imena answered 12/6, 2015 at 1:26 Comment(0)
O
0

You can use my nuget package Nager.Country. There is a lot of additional information available for each country. For more information please visit the Github project

PM> install-package Nager.Country
ICountryProvider countryProvider = new CountryProvider();
foreach (var countryCode in (Alpha2Code[])Enum.GetValues(typeof(Alpha2Code)))
{
    var countryInfo = countryProvider.GetCountry(countryCode);
    Console.WriteLine(countryInfo.CommonName);
}
Oletaoletha answered 13/12, 2020 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.