Erweiterungsmethode hinzugefügt, um IStringLocalizer in ein dynamisches Objekt zu konvertieren.

This commit is contained in:
Developer 02
2024-06-24 14:34:24 +02:00
parent b6adf7ed8b
commit e554197089
4 changed files with 61 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Localization;
using System.Dynamic;
using System.Globalization;
namespace DigitalData.Core.API
@@ -62,5 +63,20 @@ namespace DigitalData.Core.API
/// <param name="localizer">The <see cref="IStringLocalizer"/> instance containing the localized strings.</param>
/// <returns>A dictionary containing all localized strings, where the key is the name of the string and the value is the localized value.</returns>
public static Dictionary<string, string> ToDictionary(this IStringLocalizer localizer) => localizer.GetAllStrings().ToDictionary(ls => ls.Name, ls => ls.Value);
/// <summary>
/// Converts the localized strings from an <see cref="IStringLocalizer"/> to a dynamic object.
/// </summary>
/// <param name="localizer">The string localizer to get localized strings from.</param>
/// <returns>A dynamic object containing all localized strings.</returns>
public static dynamic ToDynamic(this IStringLocalizer localizer)
{
var expando = new ExpandoObject() as IDictionary<string, object>;
foreach (var localizedString in localizer.GetAllStrings())
{
expando[localizedString.Name] = localizedString.Value;
}
return expando;
}
}
}

View File

@@ -0,0 +1,12 @@
{
"profiles": {
"DigitalData.Core.API": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:50245;http://localhost:50246"
}
}
}