mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Adds localization to Lumina.Presentation.Web project
This commit is contained in:
1 parent
771fa74991
commit
2c31fd8714
65 files changed
+1111
-319
No files matched your search
@@ -1,5 +1,8 @@
|
||||
namespace Lumina.Presentation.Web.Common.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for the service for generating URLs from action and controller names, with URL localization.
|
||||
/// </summary>
|
||||
public interface IUrlService
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using System;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for generating URLs from action and controller names.
|
||||
/// Service for generating URLs from action and controller names, with URL localization.
|
||||
/// </summary>
|
||||
public class UrlService : IUrlService
|
||||
{
|
||||
private readonly LinkGenerator _linkGenerator;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UrlService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="linkGenerator">The ASP.NET Core link generator service.</param>
|
||||
/// <param name="httpContextAccessor">Provides access to the current HTTP context.</param>
|
||||
public UrlService(LinkGenerator linkGenerator, IHttpContextAccessor httpContextAccessor)
|
||||
/// <param name="actionDescriptorCollectionProvider">The <see cref="IActionDescriptorCollectionProvider"/> used to retrieve information about the application's routes and actions.</param>
|
||||
public UrlService(LinkGenerator linkGenerator, IHttpContextAccessor httpContextAccessor, IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
|
||||
{
|
||||
_linkGenerator = linkGenerator;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -32,7 +39,25 @@ public class UrlService : IUrlService
|
||||
/// <returns>An absolute URL to the specified action.</returns>
|
||||
public string? GetAbsoluteUrl(string action, string controller)
|
||||
{
|
||||
string? scheme = _httpContextAccessor.HttpContext?.Request.Scheme;
|
||||
return _linkGenerator.GetUriByAction(_httpContextAccessor.HttpContext!, action: action, controller: controller, values: null, scheme: scheme);
|
||||
HttpContext? httpContext = _httpContextAccessor.HttpContext;
|
||||
if (httpContext is null)
|
||||
return null;
|
||||
string scheme = httpContext.Request.Scheme;
|
||||
string culture = httpContext.Request.RouteValues["culture"]?.ToString() ?? "en-US";
|
||||
// find the controller's route template
|
||||
ControllerActionDescriptor? controllerActionDescriptor = _actionDescriptorCollectionProvider
|
||||
.ActionDescriptors.Items
|
||||
.OfType<ControllerActionDescriptor>()
|
||||
.FirstOrDefault(ad =>
|
||||
ad.ControllerName.Equals(controller, StringComparison.OrdinalIgnoreCase) &&
|
||||
ad.ActionName.Equals(action, StringComparison.OrdinalIgnoreCase));
|
||||
if (controllerActionDescriptor is null)
|
||||
return null;
|
||||
// check if the route template contains {culture}
|
||||
string? routeTemplate = controllerActionDescriptor.AttributeRouteInfo?.Template;
|
||||
bool needsCulture = routeTemplate?.Contains("{culture}") == true;
|
||||
// create route values based on whether the controller is localized
|
||||
object values = needsCulture ? new { culture, action } : new { action };
|
||||
return _linkGenerator.GetUriByAction(httpContext: httpContext, action: action, controller: controller, values: values, scheme: scheme);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user