Skip to content

Commit

Permalink
Merge pull request #225 from Lombiq/issue/TDEAL-4
Browse files Browse the repository at this point in the history
TDEAL-4: CookieCultureScopeMiddleware and GetUrlHelper
  • Loading branch information
barthamark authored Nov 27, 2023
2 parents ffd7762 + 367b2d2 commit 56beebf
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public static class CommonStereotypes
public const string Content = nameof(Content);
public const string MenuItem = nameof(MenuItem);
public const string Widget = nameof(Widget);
public const string CustomSettings = nameof(CustomSettings);
}
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,18 @@ 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);

/// <summary>
/// Sets the type's <see cref="ContentTypeSettings.Stereotype"/> to <see cref="CommonStereotypes.CustomSettings"/>.
/// accordingly.
/// </summary>
public static ContentTypeDefinitionBuilder AsCustomSettings(this ContentTypeDefinitionBuilder builder) =>
builder.Stereotype(CommonStereotypes.CustomSettings);
}
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;
}
}
}

0 comments on commit 56beebf

Please sign in to comment.