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();
// Get current culture
var cultureFeature = context.Features.Get();
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 = $"{linkText}";
return new HtmlString(html);
}
}
}