Some checks failed
Deploy ASP.NET MVC to OCI / build-and-deploy (push) Has been cancelled
41 lines
1.7 KiB
C#
41 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Html;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Convert_It_Online.Services;
|
|
using System.Globalization;
|
|
|
|
namespace Convert_It_Online.Extensions
|
|
{
|
|
public static class HtmlHelperExtensions
|
|
{
|
|
public static string LocalizedUrl(this IHtmlHelper htmlHelper, string area, string controller, string action = "Index")
|
|
{
|
|
var context = htmlHelper.ViewContext.HttpContext;
|
|
var urlTranslationService = context.RequestServices.GetRequiredService<IUrlTranslationService>();
|
|
|
|
// Get current culture
|
|
var cultureFeature = context.Features.Get<Microsoft.AspNetCore.Localization.IRequestCultureFeature>();
|
|
var culture = cultureFeature?.RequestCulture.UICulture ?? new CultureInfo("pt-BR");
|
|
|
|
// Get localized URL
|
|
var localizedUrl = urlTranslationService.GetLocalizedUrl(area, controller, action, culture);
|
|
|
|
return localizedUrl;
|
|
}
|
|
|
|
public static IHtmlContent LocalizedLink(this IHtmlHelper htmlHelper, string linkText, string area, string controller, string action = "Index", object? htmlAttributes = null)
|
|
{
|
|
var url = htmlHelper.LocalizedUrl(area, controller, action);
|
|
|
|
var attributes = "";
|
|
if (htmlAttributes != null)
|
|
{
|
|
var props = htmlAttributes.GetType().GetProperties();
|
|
var attrList = props.Select(p => $"{p.Name.Replace('_', '-')}=\"{p.GetValue(htmlAttributes)}\"");
|
|
attributes = " " + string.Join(" ", attrList);
|
|
}
|
|
|
|
var html = $"<a href=\"{url}\"{attributes}>{linkText}</a>";
|
|
return new HtmlString(html);
|
|
}
|
|
}
|
|
} |