2

I have a method as given below to convert a 3 letter region code to 2 letters:

public static string ISO3toISO2(string iso3)
{
  var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

  foreach (var culture in cultures)
  {
    RegionInfo region = new RegionInfo(culture.Name);

    if (string.Equals(region.ThreeLetterISORegionName, iso3, StringComparison.OrdinalIgnoreCase))
      return region.TwoLetterISORegionName;
  }

  throw new ArgumentException($"Invalid iso3 code: \"{iso3}\"");
}

So for example 'GBR' converts to 'GB'

The problem lies with the codes 'BHS' and 'GIB', which should convert to 'BS' and 'GI' respectively.

When running on my local machine, it works perfectly with no errors. Whereas on the server, we get the exception from them being invalid.

Is it possible that the server, I'm processing on, is missing something my machine has, or is there something else I could be missing?

EDIT WITH SOLUTION

The comments were right, and the server is missing cultures. My solution is to create a lookup on my local machine, and then use that in my application instead as I don't have the power to do anything on the server:

private static void CreateISOLookup()
{
  var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

  var values = new Dictionary<string, string>();

  foreach (var culture in cultures)
  {
    RegionInfo region = new RegionInfo(culture.Name);

    var threeLetter = region.ThreeLetterISORegionName;
    var twoLetter = region.TwoLetterISORegionName;

    if (values.ContainsKey(threeLetter))
    {
      var currentTwoLetter = values[threeLetter];

      if (currentTwoLetter != twoLetter)
        throw new Exception($"RegionName '{threeLetter}' has conflicting values: '{currentTwoLetter}' and '{threeLetter}'");

      continue;
    }

    var value = new KeyValuePair<string, string>(threeLetter, twoLetter);

    values.Add(value.Key, value.Value);
  }

  using (var writer = new StreamWriter("Cultures.csv"))
  {
    foreach (var value in values)
    {
      writer.WriteLine($"\"{value.Key}\",\"{value.Value}\"");
    }
  }
}
Oyyou
  • 610
  • 5
  • 13
  • Yes, is possible, Culture info depends on the OS, if the server has only some locals you can hit this problem. Is the server also windows? which version? which language? – rekiem87 Aug 13 '20 at 16:29
  • @rekiem87 the server is "Windows Server 2012 R2 Standard" whereas my machine is "Windows 10 Pro". Both English. If this is the issue - do you know how I'd sort it? – Oyyou Aug 13 '20 at 16:31
  • Try this just or debug var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); – AnGG Aug 13 '20 at 16:54
  • [This](https://stackoverflow.com/questions/9697604/from-where-cultureinfo-currentculture-reads-culture) has some useful background info on this. IIRC, Windows updates can also change these values, although I'd expect those 2 should have been around for a long time now. – sellotape Aug 13 '20 at 17:48

0 Answers0