C # get RegionInfo, TwoLetterISORegionName, by Swedish country name

I need to get the region name with two letters, ISO 3166

- ISO 3166-1 alpha 2

, for countries. My problem is that I only have country names in Swedish like Sverige

for Sweden

and Tyskland

for Germany

. Is it possible to get the RegionInfo from just this information? I know this is possible for English country names.

Works:

var countryName = "Sweden";
//var countryName = "Denmark";
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(countryName));
var twoLetterISORegionName = englishRegion.TwoLetterISORegionName;

      

stackoverflow

+3


source to share


1 answer


Try to compare with NativeName

:

string nativeName = "Sverige"; // Sweden

var region = CultureInfo
    .GetCultures(CultureTypes.SpecificCultures)
    .Select(ci => new RegionInfo(ci.LCID))
    .FirstOrDefault(rg => rg.NativeName == nativeName);

Console.Write($"{region.TwoLetterISORegionName}");

      

Edit: It seems that we really want to know the instance RegionInfo

by its Swedish name

  Sverige  -> Sweden
  Tyskland -> Germany
  ...

      



In this case, we should use DisplayName

instead NativeName

:

string swedishName = "Sverige"; // Sweden

var region = CultureInfo
    .GetCultures(CultureTypes.SpecificCultures)
    .Select(ci => new RegionInfo(ci.LCID))
    .FirstOrDefault(rg => rg.DisplayName == swedishName);

      

and we need to make sure we are using localized .Net

The DisplayName property maps the country / region name to the language of the localized version of the .NET Framework. For example, the DisplayName property displays a country / region in English to the English version of the .NET Framework and in Spanish to the Spanish version of the .NET Framework.

+4


source







All Articles