Cynical Developer
07/15/2024, 10:02 PM/
and the Chinese on /zh/
I probably need to have the enflish on /en/
and setup a redirect (some how) to the correct language, Im running the sites in Azure Webapps currently.
Does anyone have any advice on how to achieve the above?Cynical Developer
07/15/2024, 10:14 PMcsharp
public class LanguageRedirectMiddleware(RequestDelegate next, IConfiguration configuration)
{
private readonly string? _defaultSiteLanguage = configuration["DefaultSiteLanguage"];
public async Task InvokeAsync(HttpContext context)
{
var path = context.Request.Path.Value;
// Check for root path
if (string.IsNullOrEmpty(path) || path == "/")
{
// Use the default site language from configuration
var newPath = $"/{_defaultSiteLanguage}";
// Redirect to the new path
context.Response.Redirect(newPath);
return;
}
// Continue to the next middleware
await next(context);
}
}
Matt Wise
07/16/2024, 6:25 AMMatt Wise
07/16/2024, 6:33 AMCynical Developer
07/16/2024, 8:19 AMMatt Wise
07/16/2024, 8:44 AMMatt Wise
07/16/2024, 8:45 AMCynical Developer
07/16/2024, 9:40 AMapp.UseMiddleware<LanguageRedirectMiddleware>();
using Umbraco.Cms.Core.Services;
public class LanguageRedirectMiddleware
{
private readonly RequestDelegate _next;
private readonly string? _defaultSiteLanguage;
private readonly ILocalizationService _localizationService;
private readonly string[] _supportedLanguages;
public LanguageRedirectMiddleware(RequestDelegate next, IConfiguration configuration, ILocalizationService localizationService)
{
_next = next;
_defaultSiteLanguage = configuration["DefaultSiteLanguage"];
_localizationService = localizationService;
_supportedLanguages = GetSupportedLanguages();
}
private string[] GetSupportedLanguages()
{
var languages = _localizationService.GetAllLanguages().Select(lang => lang.IsoCode.ToLower()).ToArray();
return languages;
}
public async Task InvokeAsync(HttpContext context)
{
var path = context.Request.Path.Value?.ToLower();
if (path is null or "/")
{
// Use the default site language from configuration
var newPath = $"/{_defaultSiteLanguage}";
// Redirect to the new path
context.Response.Redirect(newPath);
return;
}
// Check if the request path has a file extension
if (path.StartsWith("/umbraco", StringComparison.CurrentCultureIgnoreCase) || path.Contains('.') && !path.EndsWith("/"))
{
// Continue to the next middleware without redirecting
await _next(context);
return;
}
// Check if the path does not start with any supported language prefix
if (!_supportedLanguages.Any(lang => path.StartsWith($"/{lang.ToLower()}", StringComparison.CurrentCultureIgnoreCase)))
{
// Construct the new path based on the default language
var newPath = $"/{_defaultSiteLanguage}{path}";
// Redirect to the new path
context.Response.Redirect(newPath);
return;
}
// Continue to the next middleware
await _next(context);
}
}
Matt Wise
07/16/2024, 10:07 AMc#
options.AddFilter(new UmbracoPipelineFilter(
"languageRedirect",
prePipeline: applicationBuilder =>
{
applicationBuilder.UseMiddleware<LanguageRedirectMiddleware>();
}));
});
Cynical Developer
07/16/2024, 10:34 AMMatt Wise
07/16/2024, 10:55 AMMatt Wise
07/16/2024, 10:55 AMbuilder.Services.Configure<UmbracoPipelineOptions>(options =>
{
Matt Wise
07/16/2024, 10:55 AMCynical Developer
07/16/2024, 11:09 AM