[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.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</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" />
<TextBox Grid.Column="1" x:Name="TbSettingsCountryName"
TextChanged="TbSettingsCountryName_OnTextChanged" />
</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" />
<TextBox Grid.Column="1" x:Name="TbSettingsCountryTranslation"
TextChanged="TbSettingsCountryTranslation_OnTextChanged" />
</Grid>
<Grid Grid.Row="2">
<Grid Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
@@ -431,7 +440,8 @@
<RowDefinition Height="Auto" />
</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">
<TextBox x:Name="TbSettingsNewCountryAlternative"

View File

@@ -13,7 +13,7 @@ public partial class MainWindow : Window
public static MainWindow _instance;
private Country _selectedCountry;
private bool _suppressCountryRefresh = false;
private bool _suppressCountryRefresh;
public Uri filePath;
public MainWindow()
@@ -644,12 +644,8 @@ public partial class MainWindow : Window
if (_selectedCountry == null) return;
//Console.WriteLine("Refreshing alternatives...");
foreach (var a in _selectedCountry.alternatives)
{
LbSettingsAlternatives.Items.Add(a);
//Console.WriteLine(a);
}
foreach (var a in _selectedCountry.alternatives) LbSettingsAlternatives.Items.Add(a);
//Console.WriteLine(a);
TbSettingsCountryName.Text = _selectedCountry.name;
TbSettingsCountryTranslation.Text = _selectedCountry.translation;
@@ -719,4 +715,13 @@ public partial class MainWindow : Window
_selectedCountry = Country.GetByName(CountryList.SelectedItems[0].ToString());
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;
// let's get started: we start from the bottom
// if the country is not germany, set it
// LAND: GER oder DE
if (address.land.ToLower().Trim() != "germany" && address.land.ToLower().Trim() != "ger" &&
address.land.ToLower().Trim() != "" && address.land.ToLower().Trim() != "de" &&
address.land.ToLower().Trim() != "deutschland")
// if the country is not Germany, set it; try to map via Global countries alternatives -> translation
var trimmedLand = (address.land ?? "").Trim();
var trimmedLowerLand = trimmedLand.ToLower();
var isGermany = trimmedLowerLand == "germany" || trimmedLowerLand == "ger" ||
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++;
}