diff --git a/DigitalData.Core.API/LocalizationExtensions.cs b/DigitalData.Core.API/LocalizationExtensions.cs index ab1d1f4..021bb45 100644 --- a/DigitalData.Core.API/LocalizationExtensions.cs +++ b/DigitalData.Core.API/LocalizationExtensions.cs @@ -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 /// 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; + } } } \ No newline at end of file diff --git a/DigitalData.Core.API/Properties/launchSettings.json b/DigitalData.Core.API/Properties/launchSettings.json new file mode 100644 index 0000000..baac5b5 --- /dev/null +++ b/DigitalData.Core.API/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "DigitalData.Core.API": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:50245;http://localhost:50246" + } + } +} \ No newline at end of file diff --git a/DigitalData.Core.Tests/API/StringLocalizerExtensionsTests.cs b/DigitalData.Core.Tests/API/StringLocalizerExtensionsTests.cs new file mode 100644 index 0000000..64bd706 --- /dev/null +++ b/DigitalData.Core.Tests/API/StringLocalizerExtensionsTests.cs @@ -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 + { + new ("Key1", "Value1"), + new ("Key2", "Value2"), + }; + + var mockLocalizer = new Mock(); + mockLocalizer.Setup(l => l.GetAllStrings(It.IsAny())).Returns(localizedStrings); + + // Act + dynamic result = mockLocalizer.Object.ToDynamic(); + + // Assert + Assert.AreEqual("Value1", result.Key1); + Assert.AreEqual("Value2", result.Key2); + } + } +} diff --git a/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj b/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj index a4fb147..7d99120 100644 --- a/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj +++ b/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj @@ -10,6 +10,7 @@ + @@ -19,6 +20,7 @@ +