using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Localization; using System.Dynamic; using System.Globalization; namespace DigitalData.Core.API { /// /// 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. /// public static class LocalizationExtensions { /// /// Adds localized resources and view localization services to the application. /// /// The IServiceCollection to add services to. /// The path to the resource files used for localization. /// The IServiceCollection for chaining. 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; } /// /// Configures the application to use cookie-based localization with support for multiple cultures. /// /// The IApplicationBuilder to configure. /// A params array of supported culture names. /// The IApplicationBuilder for chaining. 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 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; } /// /// Converts all localized strings to a dictionary. /// /// The instance containing the localized strings. /// A dictionary containing all localized strings, where the key is the name of the string and the value is the localized value. public static Dictionary ToDictionary(this IStringLocalizer localizer) => localizer.GetAllStrings().ToDictionary(ls => ls.Name, ls => ls.Value); /// /// Converts the localized strings from an to a dynamic object. /// /// The string localizer to get localized strings from. /// A dynamic object containing all localized strings. public static dynamic ToDynamic(this IStringLocalizer localizer) { var expando = new ExpandoObject() as IDictionary; foreach (var localizedString in localizer.GetAllStrings()) { expando[localizedString.Name] = localizedString.Value; } return expando; } } }