From e4c8350a3d374d671039a2ce81d37644ec9e0671 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Tue, 20 Aug 2024 11:38:16 +0200 Subject: [PATCH] Optimize performance of GetCountryByNameConsiderTranslation https://github.com/nager/Nager.Country/pull/29 --- .../CountryProviderExtension.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Nager.Country.Translation/CountryProviderExtension.cs b/src/Nager.Country.Translation/CountryProviderExtension.cs index 44798fb..2fdb754 100644 --- a/src/Nager.Country.Translation/CountryProviderExtension.cs +++ b/src/Nager.Country.Translation/CountryProviderExtension.cs @@ -3,8 +3,13 @@ namespace Nager.Country.Translation { + /// + /// CountryProvider Extension + /// public static class CountryProviderExtension { + private static readonly Lazy TranslationProvider = new Lazy(); + /// public static ICountryInfo GetCountryByNameConsiderTranslation( this ICountryProvider countryProvider, @@ -24,9 +29,8 @@ public static ICountryInfo GetCountryByNameConsiderTranslation( return country; } - var translationProvider = new TranslationProvider(); - var countryTranslation = translationProvider.GetCountryTranslation(country.Alpha2Code); - if (countryTranslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase))) + var countryTanslation = TranslationProvider.Value.GetCountryTranslation(country.Alpha2Code); + if (countryTanslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase))) { return country; }