Skip to content

Commit

Permalink
optimized GetCountryByNameConsiderTranslation in CountryProviderExten…
Browse files Browse the repository at this point in the history
…sion.cs
  • Loading branch information
SebastianBrandt committed Aug 8, 2024
1 parent 6e6aacc commit 5721767
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Nager.Country.Translation/CountryProviderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Nager.Country.Translation
{
public static class CountryProviderExtension

Check warning on line 6 in src/Nager.Country.Translation/CountryProviderExtension.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CountryProviderExtension'
{
private static ITranslationProvider _translationProviderSingleton;

///<inheritdoc/>
public static ICountryInfo GetCountryByNameConsiderTranslation(
this ICountryProvider countryProvider,
Expand All @@ -24,15 +26,20 @@ public static ICountryInfo GetCountryByNameConsiderTranslation(
return country;
}

var translationProvider = new TranslationProvider();
var countryTanslation = translationProvider.GetCountryTranslation(country.Alpha2Code);
if (countryTanslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase)))
var countryTranslation = GetTranslationProviderInstance().GetCountryTranslation(country.Alpha2Code);
if (countryTranslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase)))
{
return country;
}
}

return null;
}

private static ITranslationProvider GetTranslationProviderInstance()
{
return _translationProviderSingleton
?? (_translationProviderSingleton = new TranslationProvider());
}
}
}
}

0 comments on commit 5721767

Please sign in to comment.