From 3b32281bd003e89ccbfbb14b7de6933116fc51d5 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 21 Dec 2023 21:51:23 +0000 Subject: [PATCH] Use ILocalizationService instead of get all cultures (#14934) --- .../Controllers/AdminController.cs | 27 ++++++++++++------- .../Controllers/AdminController.cs | 16 ++++++++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Controllers/AdminController.cs index f4d255ba63b..d9a562344f1 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Controllers/AdminController.cs @@ -21,6 +21,7 @@ using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Notify; using OrchardCore.Liquid; +using OrchardCore.Localization; using OrchardCore.Navigation; using OrchardCore.Routing; using OrchardCore.Search.Elasticsearch.Core.Models; @@ -48,6 +49,7 @@ public class AdminController : Controller private readonly ILogger _logger; private readonly IOptions _templateOptions; private readonly IShapeFactory _shapeFactory; + private readonly ILocalizationService _localizationService; protected readonly IStringLocalizer S; protected readonly IHtmlLocalizer H; @@ -68,9 +70,9 @@ public AdminController( ILogger logger, IOptions templateOptions, IShapeFactory shapeFactory, + ILocalizationService localizationService, IStringLocalizer stringLocalizer, - IHtmlLocalizer htmlLocalizer - ) + IHtmlLocalizer htmlLocalizer) { _session = session; _siteService = siteService; @@ -86,8 +88,8 @@ IHtmlLocalizer htmlLocalizer _notifier = notifier; _logger = logger; _templateOptions = templateOptions; - _shapeFactory = shapeFactory; + _localizationService = localizationService; S = stringLocalizer; H = htmlLocalizer; } @@ -182,7 +184,7 @@ public async Task Edit(string indexName = null) StoreSourceData = settings.StoreSourceData }; - PopulateMenuOptions(model); + await PopulateMenuOptionsAsync(model); return View(model); } @@ -214,7 +216,7 @@ public async Task EditPost(ElasticIndexSettingsViewModel model, st if (!ModelState.IsValid) { - PopulateMenuOptions(model); + await PopulateMenuOptionsAsync(model); return View(model); } @@ -243,7 +245,7 @@ public async Task EditPost(ElasticIndexSettingsViewModel model, st await _notifier.ErrorAsync(H["An error occurred while creating the index."]); _logger.LogError(e, "An error occurred while creating index: {indexName}.", _elasticIndexManager.GetFullIndexName(model.IndexName)); - PopulateMenuOptions(model); + await PopulateMenuOptionsAsync(model); return View(model); } @@ -271,7 +273,7 @@ public async Task EditPost(ElasticIndexSettingsViewModel model, st await _notifier.ErrorAsync(H["An error occurred while editing the index."]); _logger.LogError(e, "An error occurred while editing index: {indexName}.", _elasticIndexManager.GetFullIndexName(model.IndexName)); - PopulateMenuOptions(model); + await PopulateMenuOptionsAsync(model); return View(model); } @@ -559,10 +561,15 @@ private void ValidateModel(ElasticIndexSettingsViewModel model) } } - private void PopulateMenuOptions(ElasticIndexSettingsViewModel model) + private async Task PopulateMenuOptionsAsync(ElasticIndexSettingsViewModel model) { - model.Cultures = CultureInfo.GetCultures(CultureTypes.AllCultures) - .Select(x => new SelectListItem { Text = $"{x.Name} ({x.DisplayName})", Value = x.Name }); + var supportedCultures = await _localizationService.GetSupportedCulturesAsync(); + + model.Cultures = supportedCultures.Select(c => new SelectListItem + { + Text = $"{c} ({CultureInfo.GetCultureInfo(c).DisplayName})", + Value = c + }); model.Analyzers = _elasticSearchOptions.Analyzers .Select(x => new SelectListItem { Text = x.Key, Value = x.Key }); diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Controllers/AdminController.cs index 3599900d536..5d5095bbdd9 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Lucene/Controllers/AdminController.cs @@ -51,6 +51,7 @@ public class AdminController : Controller protected readonly IHtmlLocalizer H; private readonly ILogger _logger; private readonly IOptions _templateOptions; + private readonly ILocalizationService _localizationService; public AdminController( ISession session, @@ -69,7 +70,8 @@ public AdminController( IStringLocalizer stringLocalizer, IHtmlLocalizer htmlLocalizer, ILogger logger, - IOptions templateOptions) + IOptions templateOptions, + ILocalizationService localizationService) { _session = session; _luceneIndexManager = luceneIndexManager; @@ -83,12 +85,12 @@ public AdminController( _notifier = notifier; _pagerOptions = pagerOptions.Value; _javaScriptEncoder = javaScriptEncoder; - New = shapeFactory; S = stringLocalizer; H = htmlLocalizer; _logger = logger; _templateOptions = templateOptions; + _localizationService = localizationService; } public async Task Index(ContentOptions options, PagerParameters pagerParameters) @@ -208,8 +210,14 @@ public async Task EditPost(LuceneIndexSettingsViewModel model, str if (!ModelState.IsValid) { - model.Cultures = CultureInfo.GetCultures(CultureTypes.AllCultures) - .Select(x => new SelectListItem { Text = x.Name + " (" + x.DisplayName + ")", Value = x.Name }).Prepend(new SelectListItem { Text = S["Any culture"], Value = "any" }); + var supportedCultures = await _localizationService.GetSupportedCulturesAsync(); + + model.Cultures = supportedCultures + .Select(c => new SelectListItem + { + Text = $"{c} ({CultureInfo.GetCultureInfo(c).DisplayName})", + Value = c + }).Prepend(new SelectListItem { Text = S["Any culture"], Value = "any" }); model.Analyzers = _luceneAnalyzerManager.GetAnalyzers() .Select(x => new SelectListItem { Text = x.Name, Value = x.Name }); return View(model);