Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ILocalizationService instead of get all cultures #14934

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class AdminController : Controller
private readonly ILogger _logger;
private readonly IOptions<TemplateOptions> _templateOptions;
private readonly IShapeFactory _shapeFactory;
private readonly ILocalizationService _localizationService;

protected readonly IStringLocalizer S;
protected readonly IHtmlLocalizer H;
Expand All @@ -68,9 +70,9 @@ public AdminController(
ILogger<AdminController> logger,
IOptions<TemplateOptions> templateOptions,
IShapeFactory shapeFactory,
ILocalizationService localizationService,
IStringLocalizer<AdminController> stringLocalizer,
IHtmlLocalizer<AdminController> htmlLocalizer
)
IHtmlLocalizer<AdminController> htmlLocalizer)
{
_session = session;
_siteService = siteService;
Expand All @@ -86,8 +88,8 @@ IHtmlLocalizer<AdminController> htmlLocalizer
_notifier = notifier;
_logger = logger;
_templateOptions = templateOptions;

_shapeFactory = shapeFactory;
_localizationService = localizationService;
S = stringLocalizer;
H = htmlLocalizer;
}
Expand Down Expand Up @@ -182,7 +184,7 @@ public async Task<ActionResult> Edit(string indexName = null)
StoreSourceData = settings.StoreSourceData
};

PopulateMenuOptions(model);
await PopulateMenuOptionsAsync(model);

return View(model);
}
Expand Down Expand Up @@ -214,7 +216,7 @@ public async Task<ActionResult> EditPost(ElasticIndexSettingsViewModel model, st

if (!ModelState.IsValid)
{
PopulateMenuOptions(model);
await PopulateMenuOptionsAsync(model);

return View(model);
}
Expand Down Expand Up @@ -243,7 +245,7 @@ public async Task<ActionResult> 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);
}
Expand Down Expand Up @@ -271,7 +273,7 @@ public async Task<ActionResult> 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);
}
Expand Down Expand Up @@ -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();
Copy link
Contributor

@Skrypt Skrypt Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebastienros I'm not sure this is appropriate to list only supported cultures. This means that now the ElasticSearch indices culture can only be supported cultures that are added in the Admin UI. This means that I need to add translations to each of these supported cultures while I could have been able to loosely couple a culture with an index before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example of that I could have a website that is majorly written in french but has some content in english which I would want to index in a different indice if required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that I need to add translations to each of these supported cultures while I could have been able to loosely couple a culture with an index before.

How this different from getting all cultures?

Copy link
Contributor

@Skrypt Skrypt Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They come from the framework instead of the DB. And the ASP.Net localization middleware will support these cultures if they are added. if you just list cultures that are in the framework ; it doesn't do anything else than that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew what you mentioned :)

What I mean how the change that I made is different for the Elastic search module?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different in what matter? I'm just saying that we should not use supported cultures only for this and also Lucene or any other Search index configuration.

Copy link
Member Author

@hishamco hishamco Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly, I didn't understand why using the supported cultures in this case is not preferred!! maybe @sebastienros understands what you are referring to

Copy link
Contributor

@Skrypt Skrypt Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have a website that is majorly written in english. You would have only "en" as a supported culture.
But your website has content written in french for a specific content type... then you would not want to support "fr" but only index that content type for the "fr" culture in a custom IndexingHandler...

Now, I understand that what you expect is to see the same cultures as the ones that are supported in these index settings but it is not everyone that uses OC that way. This is limiting options for people that have custom handlers.

And now I need to support all the cultures which means that I need to do custom code to remove these supported cultures from the LocalizationMiddleware.


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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class AdminController : Controller
protected readonly IHtmlLocalizer H;
private readonly ILogger _logger;
private readonly IOptions<TemplateOptions> _templateOptions;
private readonly ILocalizationService _localizationService;

public AdminController(
ISession session,
Expand All @@ -69,7 +70,8 @@ public AdminController(
IStringLocalizer<AdminController> stringLocalizer,
IHtmlLocalizer<AdminController> htmlLocalizer,
ILogger<AdminController> logger,
IOptions<TemplateOptions> templateOptions)
IOptions<TemplateOptions> templateOptions,
ILocalizationService localizationService)
{
_session = session;
_luceneIndexManager = luceneIndexManager;
Expand All @@ -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<IActionResult> Index(ContentOptions options, PagerParameters pagerParameters)
Expand Down Expand Up @@ -208,8 +210,14 @@ public async Task<ActionResult> 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);
Expand Down