6

I need to get the two letter ISO region name, ISO 3166 - ISO 3166-1 alpha 2, for countries. My problem is that I only have the country names in Swedish, for example Sverige for Sweden and Tyskland for Germany. Is it possible to get RegionInfo from only this information? I know it 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;

https://stackoverflow.com/a/14262292/3850405

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Ogglas
  • 62,132
  • 37
  • 328
  • 418

1 Answers1

5

Try comparing 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 actually want to find out RegionInfo instance by its Swedish name

  Sverige  -> Sweden
  Tyskland -> Germany
  ...

In this case we should use DisplayName instead of NativeName:

string swedishName = "Sverige"; // Sweden

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

and we should be sure that we use localized .Net

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Works fine with `Sverige` and `Danmark` but fails with for example `Tyskland` for `Germany`. – Ogglas Apr 18 '17 at 10:47
  • It should be `Deutschland` (Native name) in case of `Germany` (English name), `Danmark` in case of `Denmark`, `Ελλάδα` in case of `Greece` etc. – Dmitry Bychenko Apr 18 '17 at 11:21
  • Yes but the saved value I have is `Tyskland` for `Germany`. :) – Ogglas Apr 18 '17 at 11:27
  • Another possibility is to put `DisplayName` instead of `NativeName` but it requires *localized* (Swedish) .Net version, see http://stackoverflow.com/questions/28408154/how-to-force-culture- – Dmitry Bychenko Apr 18 '17 at 11:27