QrRapido/Middleware/LanguageRedirectionMiddleware.cs
Ricardo Carneiro a8faf0ef2f
Some checks failed
Deploy QR Rapido / test (push) Successful in 3m45s
Deploy QR Rapido / build-and-push (push) Failing after 7s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Has been skipped
feat: tema claro ou escuro
2025-07-29 22:42:39 -03:00

113 lines
4.0 KiB
C#

using System.Globalization;
namespace QRRapidoApp.Middleware
{
public class LanguageRedirectionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<LanguageRedirectionMiddleware> _logger;
private readonly string[] _supportedCultures = { "pt-BR", "es", "en" };
private const string DefaultCulture = "pt-BR";
public LanguageRedirectionMiddleware(RequestDelegate next, ILogger<LanguageRedirectionMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
var path = context.Request.Path.Value?.TrimStart('/') ?? "";
// Skip if already has culture in path, or if it's an API, static file, or special route
if (HasCultureInPath(path) || IsSpecialRoute(path))
{
await _next(context);
return;
}
// Detect browser language
var detectedCulture = DetectBrowserLanguage(context);
// Build redirect URL with culture
var redirectUrl = $"/{detectedCulture}";
if (!string.IsNullOrEmpty(path))
{
redirectUrl += $"/{path}";
}
// Add query string if present
if (context.Request.QueryString.HasValue)
{
redirectUrl += context.Request.QueryString.Value;
}
_logger.LogInformation("Redirecting to localized URL: {RedirectUrl} (detected culture: {Culture})",
redirectUrl, detectedCulture);
context.Response.Redirect(redirectUrl, permanent: false);
}
private bool HasCultureInPath(string path)
{
if (string.IsNullOrEmpty(path))
return false;
var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length == 0)
return false;
return _supportedCultures.Contains(segments[0]);
}
private bool IsSpecialRoute(string path)
{
var specialRoutes = new[]
{
"api/", "health", "_framework/", "lib/", "css/", "js/", "images/",
"favicon.ico", "robots.txt", "sitemap.xml",
"signin-microsoft", "signin-google", "signout-callback-oidc",
"Account/ExternalLoginCallback", "Account/Logout"
};
return specialRoutes.Any(route => path.StartsWith(route, StringComparison.OrdinalIgnoreCase));
}
private string DetectBrowserLanguage(HttpContext context)
{
var acceptLanguage = context.Request.GetTypedHeaders().AcceptLanguage;
if (acceptLanguage != null && acceptLanguage.Any())
{
// Check for exact matches first
foreach (var lang in acceptLanguage.OrderByDescending(x => x.Quality ?? 1.0))
{
var langCode = lang.Value.Value;
// Special case: es-PY should redirect to pt-BR
if (string.Equals(langCode, "es-PY", StringComparison.OrdinalIgnoreCase))
{
return DefaultCulture;
}
// Check exact match
if (_supportedCultures.Contains(langCode))
{
return langCode;
}
// Check language part only (e.g., 'es' from 'es-AR')
var languagePart = langCode.Split('-')[0];
var matchingCulture = _supportedCultures.FirstOrDefault(c => c.StartsWith(languagePart + "-") || c == languagePart);
if (matchingCulture != null)
{
return matchingCulture;
}
}
}
// Default fallback
return DefaultCulture;
}
}
}