DigitalData.Core/DigitalData.Core.API/LocalizationExtensions.cs
Developer 02 4b71836fea Erweiterung der DTOs und Implementierung der Lokalisierungsdienste
- Neue DTO-Extension-Methoden hinzugefügt, um die Verarbeitung und Zuweisung von Nachrichten und Benachrichtigungen in Ergebnisobjekten zu vereinfachen.
- Lokalisierungsunterstützung in der API-Schicht implementiert, einschließlich Cookie-basierter Lokalisierung und Konfiguration unterstützter Kulturen.
- Die Integration von StringLocalizer in die API-Schicht wurde durchgeführt, um eine nahtlose Mehrsprachigkeit zu ermöglichen.
- Fehlerbehandlung für fehlende Konfigurationseinstellungen verbessert.

Die Änderungen verbessern die Flexibilität und Wartbarkeit des Codes und unterstützen eine effizientere Internationalisierung der Anwendung.
2024-04-30 17:01:26 +02:00

60 lines
3.0 KiB
C#

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.
.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;
}
}
}