Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

How to get ISO2 code from Country Code

Vote:
 

Hi all,

I'm wondering if there's a way to get the ISO2 code from the Commerce Countries list to pass to PayPal as part of its payment structure. As far as I can tell the Commerce Countries list contains only the Code property that is ISO3 country code: is there any way to get the corresponding ISO2 code? (ex. for United Kingdom country ISO3 is GBR while ISO2 is GB). Thanks in advance! Jennifer

#115131
Jan 09, 2015 15:26
Vote:
 

If you can get throught to create a CultureInfo out of Commerce language, then you can request two letter region name:

var c = new CultureInfo("en-GB");
var r = new RegionInfo(c.LCID);
string name = r.Name;

string regionName = r.TwoLetterISORegionName;   // gives you "GB"
#115139
Jan 09, 2015 20:58
Vote:
 

Thanks Valdis for your suggestion, it would definitely work if I could link the country to the culture as the RegionInfo class is culture dependant. What I'd like to get is a direct correspondance between the ISO3 country code already built in as Code property in the Commerce Countries list with its ISO2 code. From what I've found so far from EPiServer's example for PayPal payment gateway their solution is to create a txt file that maps the ISO3/ISO2 codes and then add some code logic that retrieves one based on the other. Thanks anyway for your help.

#115299
Jan 13, 2015 13:28
Vote:
 

There is no builtin feature to get the two letter code from a three letter code.

RegionInfo does not take a three letter code as input as it's rather a culture function than a country function.

What I've done is that I enumerate the full culture list (CultureInfo.GetCultures) and create a mapping dictionary in a singleton. See quick example below

var alpha3Regions = new Dictionary<string,RegionInfo>();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{                     alpha3Regions.Add(region.ThreeLetterISORegionName, new RegionInfo(culture.LCID));
}
#147358
Apr 12, 2016 21:20
Vote:
 

Next time I'll try compiling the code as well :D

var region = new RegionInfo(culture.LCID);

alpha3Regions.Add(region.ThreeLetterISORegionName, region);

#147359
Apr 12, 2016 21:22
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.