feat: Cookie-basierte Lokalisierung implementiert, ToLocal Erweiterungsmethoden hinzugefügt und Translation-Service entfernt. Stattdessen wird IStringLocalizer<T> verwendet, abhängig von der Situation wie Cookie-basierter Kultur oder Kultur basierend auf der Route.
This commit is contained in:
59
DigitalData.Core.API/LocalizationExtensions.cs
Normal file
59
DigitalData.Core.API/LocalizationExtensions.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using System.Globalization;
|
||||
|
||||
namespace DigitalData.Core.API
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for configuring localization services in an ASP.NET Core application.
|
||||
/// These methods simplify the integration of cookie-based localization by setting up resource paths
|
||||
/// and defining supported cultures.
|
||||
/// </summary>
|
||||
public static class LocalizationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds localized resources and view localization services to the application.
|
||||
/// </summary>
|
||||
/// <param name="services">The IServiceCollection to add services to.</param>
|
||||
/// <param name="resourcesPath">The path to the resource files used for localization.</param>
|
||||
/// <returns>The IServiceCollection for chaining.</returns>
|
||||
public static IServiceCollection AddCookieBasedLocalizer(this IServiceCollection services, string resourcesPath)
|
||||
{
|
||||
// Adds localization services with the specified resources path.
|
||||
services.AddLocalization(options => options.ResourcesPath = resourcesPath);
|
||||
|
||||
// Adds MVC services with view localization and data annotations localization.
|
||||
services.AddMvc().AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
|
||||
.AddDataAnnotationsLocalization();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the application to use cookie-based localization with support for multiple cultures.
|
||||
/// </summary>
|
||||
/// <param name="app">The IApplicationBuilder to configure.</param>
|
||||
/// <param name="supportedCultureNames">A params array of supported culture names.</param>
|
||||
/// <returns>The IApplicationBuilder for chaining.</returns>
|
||||
public static IApplicationBuilder UseCookieBasedLocalizer(this IApplicationBuilder app, params string[] supportedCultureNames)
|
||||
{
|
||||
// Converts supported culture names into CultureInfo objects and checks for null or empty array.
|
||||
IList<CultureInfo> supportedCultures = supportedCultureNames.Select(cn => new CultureInfo(cn)).ToList();
|
||||
var defaultCultureInfo = supportedCultures.FirstOrDefault() ??
|
||||
throw new ArgumentNullException(nameof(supportedCultureNames), "Supported cultures cannot be empty.");
|
||||
|
||||
// Configures localization options including default and supported cultures.
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture(culture: defaultCultureInfo.Name, uiCulture: defaultCultureInfo.Name),
|
||||
SupportedCultures = supportedCultures,
|
||||
SupportedUICultures = supportedCultures
|
||||
};
|
||||
options.RequestCultureProviders.Add(new QueryStringRequestCultureProvider());
|
||||
|
||||
// Applies the localization settings to the application.
|
||||
app.UseRequestLocalization(options);
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user