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

TDEAL-4: CookieCultureScopeMiddleware and GetUrlHelper #225

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -87,11 +87,15 @@ public static string Action<TController>(
where TController : ControllerBase =>
orchardHelper.HttpContext.Action(taskActionExpression.StripResult(), additionalArguments);

private static IUrlHelper GetUrlHelper(this IOrchardHelper orchardHelper)
/// <summary>
/// Constructs a new <see cref="IUrlHelper"/> instance using the current <see cref="IOrchardHelper.HttpContext"/>.
/// </summary>
public static IUrlHelper GetUrlHelper(this IOrchardHelper orchardHelper)
{
var serviceProvider = orchardHelper.HttpContext.RequestServices;
var urlHelperFactory = serviceProvider.GetService<IUrlHelperFactory>();
var actionContext = serviceProvider.GetService<IActionContextAccessor>()?.ActionContext;
var actionContext = serviceProvider.GetService<IActionContextAccessor>()?.ActionContext ??
throw new InvalidOperationException("Couldn't access the action context.");

return urlHelperFactory.GetUrlHelper(actionContext);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lombiq.HelpfulLibraries.OrchardCore.Contents;
using OrchardCore.Autoroute.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Title.Models;
Expand Down Expand Up @@ -66,4 +67,11 @@ public static ContentTypeDefinitionBuilder WithContentTypeAutoroute(
AllowUpdatePath = true,
ManageContainedItemRoutes = true,
}));

/// <summary>
/// Sets the type's <see cref="ContentTypeSettings.Stereotype"/> to <see cref="CommonStereotypes.Widget"/>.
/// accordingly.
/// </summary>
public static ContentTypeDefinitionBuilder AsWidget(this ContentTypeDefinitionBuilder builder) =>
builder.Stereotype(CommonStereotypes.Widget);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Localization;
using System.Globalization;
using System.Threading.Tasks;

namespace Lombiq.HelpfulLibraries.OrchardCore.Middlewares;

/// <summary>
/// A middleware that looks for the "culture" query string argument or cookie and uses it to initialize a <see
/// cref="CultureScope"/>.
/// </summary>
public class CookieCultureScopeMiddleware
{
public const string CultureKeyName = "culture";

private readonly RequestDelegate _next;

public CookieCultureScopeMiddleware(RequestDelegate next) => _next = next;

public async Task InvokeAsync(HttpContext context)
{
if (!TryGetCultureInfo(context, out var culture))
{
culture = await context
.RequestServices
.GetRequiredService<ILocalizationService>()
.GetDefaultCultureAsync() ?? "en-US";
}

using var scope = CultureScope.Create(culture, culture, ignoreSystemSettings: true);
context.SetCookieForever(CultureKeyName, culture);

await _next(context);
}

private static bool TryGetCultureInfo(HttpContext context, out string culture)
{
if (context.Request.Query.TryGetValue(CultureKeyName, out var queryCulture))
{
culture = queryCulture[0];
}
else
{
context.Request.Cookies.TryGetValue(CultureKeyName, out culture);
}

if (string.IsNullOrWhiteSpace(culture)) return false;

try
{
culture = new CultureInfo(culture).Name;
return true;
}
catch
{
culture = null;
return false;
}
}
}