[chore:] usage of Country (and possible import of ISO3166-Countries)

This commit is contained in:
Elias Fierke
2026-01-02 18:02:08 +01:00
parent 7af6444da2
commit 3877d91af4
3 changed files with 65 additions and 17 deletions

View File

@@ -406,24 +406,33 @@
<Grid Grid.Column="1" Margin="5"> <Grid Grid.Column="1" Margin="5">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid ColumnDefinitions="300,*" Margin="0,0,0,5"> <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,5"
HorizontalAlignment="Right">
<Button x:Name="BtnSettingsInsertDefaultCountries"
Content="Standard-Länder laden (Englisch)"
Click="BtnSettingsInsertDefaultCountries_OnClick" />
</StackPanel>
<Grid Grid.Row="1" ColumnDefinitions="300,*" Margin="0,0,0,5">
<TextBlock Text="Name:" VerticalAlignment="Center" /> <TextBlock Text="Name:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" x:Name="TbSettingsCountryName" <TextBox Grid.Column="1" x:Name="TbSettingsCountryName"
TextChanged="TbSettingsCountryName_OnTextChanged" /> TextChanged="TbSettingsCountryName_OnTextChanged" />
</Grid> </Grid>
<Grid ColumnDefinitions="300,*" Margin="0,0,0,5" Grid.Row="1"> <Grid ColumnDefinitions="300,*" Margin="0,0,0,5" Grid.Row="2">
<TextBlock Text="Übersetzung:" VerticalAlignment="Center" /> <TextBlock Text="Übersetzung:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" x:Name="TbSettingsCountryTranslation" <TextBox Grid.Column="1" x:Name="TbSettingsCountryTranslation"
TextChanged="TbSettingsCountryTranslation_OnTextChanged" /> TextChanged="TbSettingsCountryTranslation_OnTextChanged" />
</Grid> </Grid>
<Grid Grid.Row="2"> <Grid Grid.Row="3">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
@@ -431,7 +440,8 @@
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<ListBox x:Name="LbSettingsAlternatives" Grid.Row="0" SelectionMode="Multiple" />
<ListBox Grid.Row="0" x:Name="LbSettingsAlternatives" SelectionMode="Multiple" />
<Grid Grid.Row="1" ColumnDefinitions="*,200" Margin="0,5,0,0"> <Grid Grid.Row="1" ColumnDefinitions="*,200" Margin="0,5,0,0">
<TextBox x:Name="TbSettingsNewCountryAlternative" <TextBox x:Name="TbSettingsNewCountryAlternative"

View File

@@ -13,7 +13,7 @@ public partial class MainWindow : Window
public static MainWindow _instance; public static MainWindow _instance;
private Country _selectedCountry; private Country _selectedCountry;
private bool _suppressCountryRefresh = false; private bool _suppressCountryRefresh;
public Uri filePath; public Uri filePath;
public MainWindow() public MainWindow()
@@ -644,12 +644,8 @@ public partial class MainWindow : Window
if (_selectedCountry == null) return; if (_selectedCountry == null) return;
//Console.WriteLine("Refreshing alternatives..."); //Console.WriteLine("Refreshing alternatives...");
foreach (var a in _selectedCountry.alternatives) foreach (var a in _selectedCountry.alternatives) LbSettingsAlternatives.Items.Add(a);
{ //Console.WriteLine(a);
LbSettingsAlternatives.Items.Add(a);
//Console.WriteLine(a);
}
TbSettingsCountryName.Text = _selectedCountry.name; TbSettingsCountryName.Text = _selectedCountry.name;
TbSettingsCountryTranslation.Text = _selectedCountry.translation; TbSettingsCountryTranslation.Text = _selectedCountry.translation;
@@ -719,4 +715,13 @@ public partial class MainWindow : Window
_selectedCountry = Country.GetByName(CountryList.SelectedItems[0].ToString()); _selectedCountry = Country.GetByName(CountryList.SelectedItems[0].ToString());
RefreshCountryView(); RefreshCountryView();
} }
private void BtnSettingsInsertDefaultCountries_OnClick(object? sender, RoutedEventArgs e)
{
ISO3166.Country[] countries = ISO3166.Country.List;
foreach (var countryie in countries)
Global._instance.countries.Add(new Country(countryie.Name, countryie.Name,
new List<string> { countryie.ThreeLetterCode, countryie.TwoLetterCode }));
RefreshCountryView();
}
} }

View File

@@ -112,13 +112,46 @@ public static class AddressCreator
if (address == null) return null; if (address == null) return null;
// let's get started: we start from the bottom // let's get started: we start from the bottom
// if the country is not germany, set it // if the country is not Germany, set it; try to map via Global countries alternatives -> translation
// LAND: GER oder DE var trimmedLand = (address.land ?? "").Trim();
if (address.land.ToLower().Trim() != "germany" && address.land.ToLower().Trim() != "ger" && var trimmedLowerLand = trimmedLand.ToLower();
address.land.ToLower().Trim() != "" && address.land.ToLower().Trim() != "de" && var isGermany = trimmedLowerLand == "germany" || trimmedLowerLand == "ger" ||
address.land.ToLower().Trim() != "deutschland") trimmedLowerLand == "" || trimmedLowerLand == "de" ||
trimmedLowerLand == "deutschland";
if (!isGermany)
{ {
string_address = "\n**" + address.land.Trim() + "**"; // Needs to be bold var countryToShow = trimmedLand; // default: use raw land value
if (!string.IsNullOrEmpty(trimmedLand))
{
// search for matching country via alternatives using for-loops
for (var ci = 0; ci < Global._instance.countries.Count; ci++)
{
var country = Global._instance.countries[ci];
for (var ai = 0; ai < country.alternatives.Count; ai++)
{
try
{
var alt = (country.alternatives[ai] ?? "").Trim();
if (string.Equals(alt, trimmedLand, StringComparison.OrdinalIgnoreCase))
{
countryToShow = string.IsNullOrWhiteSpace(country.translation)
? country.name
: country.translation;
goto CountryFound;
}
}
catch
{
// ignore malformed alternative
}
}
}
}
CountryFound:
string_address = "\n**" + countryToShow + "**"; // Needs to be bold
address_line_count++; address_line_count++;
} }