This commit is contained in:
Oliver Booth 2023-08-06 02:27:16 +01:00
parent 75eed18bc8
commit b2ada7d720
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61

View File

@ -1,32 +1,10 @@
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Localization;
using OliverBooth;
using OliverBooth.Middleware;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddRazorPages().AddViewLocalization().AddDataAnnotationsLocalization(); builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddRouting(options => options.LowercaseUrls = true); builder.Services.AddRouting(options => options.LowercaseUrls = true);
var supportedCultures = new[]
{
CultureInfo.GetCultureInfo("en"),
CultureInfo.GetCultureInfo("fr")
};
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(supportedCultures[0]);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
builder.Services.Configure<LocalizationOptions>(options => options.ResourcesPath = "Resources");
WebApplication app = builder.Build(); WebApplication app = builder.Build();
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
@ -38,22 +16,11 @@ if (!app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.UseRequestLocalization(options =>
{
options.ApplyCurrentCultureToResponseHeaders = true;
options.DefaultRequestCulture = new RequestCulture(supportedCultures[0]);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new RouteCultureProvider(supportedCultures[0]));
});
app.MapControllers(); app.MapControllers();
app.UseCultureRedirect(); app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute("default", "{culture=en}/{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages(); app.MapRazorPages();
app.Run(); app.Run();