Erweiterungsmethode hinzugefügt, um IStringLocalizer in ein dynamisches Objekt zu konvertieren.
This commit is contained in:
parent
b6adf7ed8b
commit
e554197089
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
DigitalData.Core.API/Properties/launchSettings.json
Normal file
12
DigitalData.Core.API/Properties/launchSettings.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"DigitalData.Core.API": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:50245;http://localhost:50246"
|
||||
}
|
||||
}
|
||||
}
|
||||
31
DigitalData.Core.Tests/API/StringLocalizerExtensionsTests.cs
Normal file
31
DigitalData.Core.Tests/API/StringLocalizerExtensionsTests.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Moq;
|
||||
using DigitalData.Core.API;
|
||||
|
||||
namespace DigitalData.Core.Tests.API
|
||||
{
|
||||
[TestFixture]
|
||||
public class StringLocalizerExtensionsTests
|
||||
{
|
||||
[Test]
|
||||
public void ToDynamic_ShouldReturnDynamicObject_WithLocalizedStrings()
|
||||
{
|
||||
// Arrange
|
||||
var localizedStrings = new List<LocalizedString>
|
||||
{
|
||||
new ("Key1", "Value1"),
|
||||
new ("Key2", "Value2"),
|
||||
};
|
||||
|
||||
var mockLocalizer = new Mock<IStringLocalizer>();
|
||||
mockLocalizer.Setup(l => l.GetAllStrings(It.IsAny<bool>())).Returns(localizedStrings);
|
||||
|
||||
// Act
|
||||
dynamic result = mockLocalizer.Object.ToDynamic();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("Value1", result.Key1);
|
||||
Assert.AreEqual("Value2", result.Key2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||
<PackageReference Include="Moq" Version="4.20.70" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
@ -19,6 +20,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DigitalData.Core.API\DigitalData.Core.API.csproj" />
|
||||
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user