Skip to content

Commit

Permalink
Move ContentCulturePicker logic back to driver for easier shape overr…
Browse files Browse the repository at this point in the history
…ide (#16230)
  • Loading branch information
Piedone authored Jun 3, 2024
1 parent 9bd92a0 commit a67204f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
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

0 comments on commit a67204f

Please sign in to comment.