Integrierte Mehrsprachigkeit (Deutsch und Englisch) mit Cookie-basierter Sprachauswahl

This commit is contained in:
Developer 02
2024-05-15 16:11:26 +02:00
parent cf9286e4c3
commit 68714c2937
32 changed files with 282 additions and 253 deletions

View File

@@ -16,7 +16,7 @@ using EnvelopeGenerator.Web.Models;
using DigitalData.Core.DTO;
using System.Text.Encodings.Web;
using Ganss.Xss;
using EnvelopeGenerator.Web;
using Microsoft.Extensions.Options;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Logging initialized!");
@@ -156,8 +156,8 @@ try
builder.Services.AddCookieConsentSettings();
builder.Services.AddCookieBasedLocalizer();
builder.Services.AddCookieBasedLocalizer("Resources");
builder.Services.AddSingleton(HtmlEncoder.Default);
builder.Services.AddSingleton(UrlEncoder.Default);
builder.Services.AddSingleton(_ =>
@@ -167,6 +167,10 @@ try
return sanitizer;
});
// Register the FlagIconCssClass instance as a singleton
builder.Services.Configure<Cultures>(builder.Configuration.GetSection("Cultures"));
builder.Services.AddSingleton(sp => sp.GetRequiredService<IOptions<Cultures>>().Value);
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -177,6 +181,14 @@ try
app.UseHsts();
}
//Content-Security-Policy
if (config.GetValue<bool>("TestCSP") || !app.Environment.IsDevelopment())
{
var csp_list = config.GetSection("Content-Security-Policy").Get<string[]>();
if (csp_list is not null)
app.UseCSPMiddleware($"{string.Join("; ", csp_list)};");
}
if (config.GetValue<bool>("EnableSwagger"))
{
app.UseSwagger();
@@ -185,26 +197,33 @@ try
app.UseHttpsRedirection();
var csp_list = config.GetSection("Content-Security-Policy").Get<string[]>();
if(csp_list is not null)
app.UseCSPMiddleware($"{string.Join("; ", csp_list)};");
app.UseStaticFiles();
app.UseCookiePolicy();
//app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
var languages = config.GetSection("Languages").Get<string[]>() ??
throw new InvalidOperationException("Languages section is missing in the configuration.");
if(languages.Length == 0)
throw new InvalidOperationException("There is no languages in languages section.");
var cultures = app.Services.GetRequiredService<Cultures>();
if(cultures.Any())
throw new InvalidOperationException(@"Languages section is missing in the appsettings. Please configure like following.
Language is both a name of the culture and the name of the resx file such as Resource.de-DE.resx
FIClass is the css class (in wwwroot/lib/flag-icons-main) for the flag of country.
""Cultures"": [
{
""Language"": ""de-DE"",
""FIClass"": ""fi-de""
},
{
""Language"": ""en-US"",
""FIClass"": ""fi-us""
}
]");
if(!config.GetValue<bool>("DisableMultiLanguage"))
app.UseCookieBasedLocalizer(languages);
app.UseCookieBasedLocalizer(cultures.Languages.ToArray());
app.UseCors("SameOriginPolicy");