c# : Get country name from country code
Asked Answered
S

3

5

I am trying using c# to get the country name from a country code

For example :

  • fr-fr gets me France
  • it-it gets me Italy

The problem with my code is that I am not getting the name but a information like this :

French (France)

This is my code :

var cultureInfo = new CultureInfo("fr-fr");
var result : cultureInfo.EnglishName

My result is "French (France)" instead of France.

I have managed to get what I want by use the parent property of the cultureInfo but I am not sure if it's a good method.

Spleenwort answered 24/4, 2016 at 9:30 Comment(1)
fr-fr and it-it are not country codesRosenblum
S
8

Because CultureInfo.EnglishName still returns culture name.

You can create a RegionInfo based on this culture and call it's EnglishName property as well.

var cultureInfo = new CultureInfo("fr-fr");
var ri = new RegionInfo(cultureInfo.Name);
ri.EnglishName // France

or

var cultureInfo = new CultureInfo("it-it");
var ri = new RegionInfo(cultureInfo.Name);
ri.EnglishName // Italy
Seifert answered 24/4, 2016 at 9:39 Comment(2)
I have "US"... how to get country name from it..? It's not in "us-usa" formatMaidenly
US itself does not refers any kind of culture or region on .net framework. You have to have a string like en-US for example. Then you might wanna use TwoLetterISORegionName which retuns US and ThreeLetterISORegionName which returns USA after you created the RegionInfo instance based on that cutlure. You have to do some string manipulation no matter what to get your string.Hoist
P
4

You can get country name this way

RegionInfo cultureInfo = new RegionInfo("fr-fr");
string result = cultureInfo.EnglishName;
Phenomenalism answered 24/4, 2016 at 9:39 Comment(0)
D
0

Try

var cultureInfoName = new RegionInfo(cultureInfo.Name);
string countryName = cultureInfoName.DisplayName;
Damask answered 24/4, 2016 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.