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}\"");
}
}
}