2

Is it possible to get the value UK (or the equivalent value for another country) from C# CultureInfo object? My example below shows I currently get "United Kingdon" using the following line of code.

var region = new RegionInfo(CultureInfo.CurrentCulture.LCID)

Update the comments

After receiving really great help I want to clarify my problem. I need to provide the value "UK" based on the locale settings. I currently have the following values

locale_country: "United Kingdom"  // This needs to be UK (or the equivalent for another country)
locale_language: "en-GB"
locale_region: "GB"

The code that generates this is illustrated below (C#)

Code snippet

var localRegion = new RegionInfo(CultureInfo.CurrentCulture.LCID);
LocaleRegion = localRegion.TwoLetterISORegionName,
LocaleCountry = localRegion.DisplayName
user7558986
  • 65
  • 2
  • 8
  • region.name may work. Look at this post: http://stackoverflow.com/questions/20330343/how-to-get-the-country-code-from-cultureinfo – Vishal Apr 29 '17 at 14:39
  • Question title says *how to get **GB***. Question body says *is it possible to get value **UK***. I suppose title is wrong – Sergey Berezovskiy Apr 29 '17 at 14:45
  • The value is used for our website tracking digital data variable on the page. The requirement for the tracking to work when browsing the UK variant of our website to populate a property within the tracking object called locale_country to equal "uk" - does that help? – user7558986 Apr 29 '17 at 14:55
  • apologies - I've corrected the question title. – user7558986 Apr 29 '17 at 14:56

2 Answers2

6

Following code should work :

var cul = new CultureInfo("en-GB");
var regn = new RegionInfo(cul.LCID);
string name = regn.TwoLetterISORegionName;
Biswabid
  • 1,378
  • 11
  • 26
0

The property RegionInfo.TwoLetterISORegionName returns the 2-letter country code as defined by ISO-3166.

ISO-3166 defines the United Kingdom to have the country code "GB" not "UK".

Paul Bentley
  • 344
  • 1
  • 9