To obtain a list of countries more or less supported by .Net you can use CultureInfo and RegionInfo like this:
List<RegionInfo> allRegions = new List<RegionInfo>();
var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in specificCultures)
{
var info = new RegionInfo(culture.LCID);
allRegions.Add(info);
}
This will give you all the countries, some of them multiple times (because different languages are in use).
RegionInfo has few interesting properties:
NativeName
that will give you translated name of the country but unfortunately one corresponding to given Locale Identifier (LCID), that is "Polska" for Poland, "Deutschland" for Germany and so on; some will be translated into few languages (that's interesting, because USA is visible both as United States and as Estados Unidos)
DisplayName
what should be what you are looking for but unfortunately is not – Microsoft "forgot" to translate it in the .Net framework (maybe it is OK, for it should not be the Property then)
- Name which unlike name suggests will give you two-letter country code.
So what you can do with this information? Theoretically you can use translated countries names – in this case you would just create a dictionary (Dictionary<int, string>
) and add LCID with corresponding NativeName string and use it as a source for your Drop down menu.
In theory one born in the country should be able to understand at least one of its languages (at least that happens most of the times).
In reality though, you probably want to have unique list of countries translated into whatever language your application is displaying at the moment. You can use the method above to obtain list of countries and use for example DisplayName
(or EnglishName
). At runtime you would resolve it to translated name just like any other string. Since that need to happen on the backend, you would add another resource file (could be placed in App_GlobalResources, does not matter) and read it in your code-behind. No more theory, some code sample is required:
const string RESOURCE_FILE = "Countries";
Dictionary<string, string> countryNames = new Dictionary<string, string>();
var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in specificCultures)
{
var info = new RegionInfo(culture.LCID);
var name = info.EnglishName;
var translated = GetGlobalResourceObject(RESOURCE_FILE, name).ToString();
countryNames[name] = translated;
}
If you want to read the name in a specific language (other than CurrentUICulture
) pass CultureInfo
object as a third parameter to GetGlobalResourceObject()
.