What I want to do is to create a Country Tag Helper for asp.net core.
Something like
<select asp-country=”true”
and then list all countries in this select. More, it should be localized ( Germany vs Allemagne). That means 1 or more resource files
First I need initial data: The WorldBank API gives us the names http://api.worldbank.org/countries and I have made a project https://github.com/ignatandrei/WorldBankAPi that inspects that API.
To retrieve data in resource format , it is simple:
var r = new CountriesRepository();
var arr = r.GetCountries().Result.
Select(it =>
$"<data name = \"{it.iso2Code}\" xml:space=\"preserve\" >" +
$"<value>{it.name}</value>" +
"</data>").ToArray();
var s = string.Join(Environment.NewLine, arr);
File.WriteAllText(@"D:\c.txt", s);
Then we could copy paste into resources:
<data name = "AW" xml:space="preserve" ><value>Aruba</value></data>
<data name = "AF" xml:space="preserve" ><value>Afghanistan</value></data>
<data name = "AO" xml:space="preserve" ><value>Angola</value></data>
...
Now I want to make a list of those names – so I need this code to translate between C# generated code from resource
public static string AD {
get {
return ResourceManager.GetString("AD", resourceCulture);
}
}
public static string AE {
get {
return ResourceManager.GetString("AE", resourceCulture);
}
}
...
and a list of names to be put into select.
Reflection to the rescue:
var t =typeof( ResCTH);
properties= t.GetProperties(
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.GetProperty);
CountryISO = properties
.Where(it=>it.Name.Length==2)
.Select(it => it.Name)
.ToArray();
The code is on github https://github.com/ignatandrei/CountryTagHelper and I will improve next time.