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

Move ContentCulturePicker logic back to driver for easier shape override (Lombiq Technologies. OCORE-178) #16230

Merged
merged 2 commits into from
Jun 3, 2024
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
@@ -1,14 +1,41 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using OrchardCore.Admin.Models;
using OrchardCore.ContentLocalization.ViewModels;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Localization;

namespace OrchardCore.ContentLocalization.Drivers;

public class ContentCulturePickerNavbarDisplayDriver : DisplayDriver<Navbar>
{
public override IDisplayResult Display(Navbar model)
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILocalizationService _localizationService;

public ContentCulturePickerNavbarDisplayDriver(
IHttpContextAccessor httpContextAccessor,
ILocalizationService localizationService)
{
return View("ContentCulturePicker", model)
.Location("Detail", "Content:5");
_httpContextAccessor = httpContextAccessor;
_localizationService = localizationService;
}

public override async Task<IDisplayResult> DisplayAsync(Navbar model, BuildDisplayContext context)
{
var supportedCultures = (await _localizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));

return Initialize<CulturePickerViewModel>("ContentCulturePicker", model =>
{
model.SupportedCultures = supportedCultures;
model.CurrentCulture = _httpContextAccessor
.HttpContext
.Features
.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
}).RenderWhen(() => Task.FromResult(supportedCultures.Count() > 1))
.Location("Detail", "Content:5");
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
@using Microsoft.AspNetCore.Localization
@using OrchardCore.Localization
@using System.Globalization

@inject ILocalizationService LocalizationService
@{
var supportedCultures = (await LocalizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));

if (supportedCultures.Count() < 2)
{
return;
}

var currentCulture = Context.Request.HttpContext.Features
.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
}

<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="oc-culture-picker" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">@currentCulture.NativeName</a>
<a class="nav-link dropdown-toggle" href="#" id="oc-culture-picker" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">@Model.CurrentCulture.NativeName</a>
<div class="dropdown-menu" aria-labelledby="oc-culture-picker">
@foreach (var culture in supportedCultures)
@foreach (var culture in Model.SupportedCultures)
{
if (culture.Name == currentCulture.Name)
if (culture.Name == Model.CurrentCulture.Name)
{
continue;
}
Expand Down