From 33cb48266b6ee786290f2cef04139a83c6c833b8 Mon Sep 17 00:00:00 2001 From: pomianowski <13592821+pomianowski@users.noreply.github.com> Date: Mon, 20 May 2024 23:02:40 +0200 Subject: [PATCH] Update tests --- src/Lepo.i18n/LocalizationBuilderExtensions.cs | 15 +++++++++++++-- .../GlobalUsings.cs | 1 + ...Lepo.i18n.DependencyInjection.UnitTests.csproj | 13 ++++++++++++- .../Resources/Test.cs | 2 +- .../Resources/Translations-en-US.yaml | 13 +++++++++++++ .../Resources/Translations-pl-PL.yaml | 13 +++++++++++++ .../StringLocalizerBuilderExtensionsTests.cs | 12 +++++++++--- 7 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-en-US.yaml create mode 100644 tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-pl-PL.yaml diff --git a/src/Lepo.i18n/LocalizationBuilderExtensions.cs b/src/Lepo.i18n/LocalizationBuilderExtensions.cs index 5ee3423..f3b5977 100644 --- a/src/Lepo.i18n/LocalizationBuilderExtensions.cs +++ b/src/Lepo.i18n/LocalizationBuilderExtensions.cs @@ -156,11 +156,16 @@ public static LocalizationBuilder FromResource( CultureInfo culture ) { + CultureInfo cultureToRestore = Thread.CurrentThread.CurrentCulture; + + // NOTE: Fix net framework satellite assembly loading try { - // NOTE: Fix net framework satellite assembly loading + Thread.CurrentThread.CurrentCulture = culture; + Thread.CurrentThread.CurrentUICulture = culture; ResourceManager resourceManager = new(baseName, assembly); + ResourceSet? resourceSet = resourceManager.GetResourceSet(culture, true, true); if (resourceSet is null) @@ -175,11 +180,17 @@ CultureInfo culture builder.AddLocalization(new LocalizationSet(baseName, culture, localizations)); + Thread.CurrentThread.CurrentCulture = cultureToRestore; + Thread.CurrentThread.CurrentUICulture = cultureToRestore; + return builder; } catch (MissingManifestResourceException ex) { - throw new LocalizationBuilderException("Failed to register translation resources.", ex); + throw new LocalizationBuilderException( + $"Failed to register translation resources for \"{culture}\".", + ex + ); } } } diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/GlobalUsings.cs b/tests/Lepo.i18n.DependencyInjection.UnitTests/GlobalUsings.cs index 6bc684f..f9b7824 100644 --- a/tests/Lepo.i18n.DependencyInjection.UnitTests/GlobalUsings.cs +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/GlobalUsings.cs @@ -5,6 +5,7 @@ global using System; global using System.Collections.Generic; +global using System.Globalization; global using System.Reflection; global using FluentAssertions; global using Microsoft.Extensions.DependencyInjection; diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/Lepo.i18n.DependencyInjection.UnitTests.csproj b/tests/Lepo.i18n.DependencyInjection.UnitTests/Lepo.i18n.DependencyInjection.UnitTests.csproj index cf3baa9..6af6b48 100644 --- a/tests/Lepo.i18n.DependencyInjection.UnitTests/Lepo.i18n.DependencyInjection.UnitTests.csproj +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/Lepo.i18n.DependencyInjection.UnitTests.csproj @@ -1,12 +1,23 @@ - net8.0 + net472;net8.0 false true false + + + + + + + + + + + diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Test.cs b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Test.cs index 1e6966a..cc29130 100644 --- a/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Test.cs +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Test.cs @@ -5,4 +5,4 @@ namespace Lepo.i18n.DependencyInjection.UnitTests.Resources; -public class Test; +public partial class Test; diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-en-US.yaml b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-en-US.yaml new file mode 100644 index 0000000..3dbcb33 --- /dev/null +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-en-US.yaml @@ -0,0 +1,13 @@ +# Comment +Test: 'Test in english' +main.languages: Languages +main.hello: "Hello world" + +namespace.test: + main.languages: Languages in namespace.test #yet another comment + main.hello: 'Hello world in namespace.test' + +# Some comment +other.namespace: + main.languages: Languages in other.namespace #yet another comment + main.hello: 'Hello world in other.namespace' \ No newline at end of file diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-pl-PL.yaml b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-pl-PL.yaml new file mode 100644 index 0000000..f285c10 --- /dev/null +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Translations-pl-PL.yaml @@ -0,0 +1,13 @@ +# Comment +Test: 'Test po polsku' +main.languages: Języki +main.hello: "Witaj świecie" + +namespace.test: + main.languages: Języki w namespace.test #yet another comment + main.hello: 'Witaj świecie w namespace.test' + +# Some comment +other.namespace: + main.languages: Języki w other.namespace #yet another comment + main.hello: 'Witaj świecie w other.namespace' \ No newline at end of file diff --git a/tests/Lepo.i18n.DependencyInjection.UnitTests/StringLocalizerBuilderExtensionsTests.cs b/tests/Lepo.i18n.DependencyInjection.UnitTests/StringLocalizerBuilderExtensionsTests.cs index 939b4ba..6025bd8 100644 --- a/tests/Lepo.i18n.DependencyInjection.UnitTests/StringLocalizerBuilderExtensionsTests.cs +++ b/tests/Lepo.i18n.DependencyInjection.UnitTests/StringLocalizerBuilderExtensionsTests.cs @@ -3,7 +3,7 @@ // Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. // All Rights Reserved. -using Lepo.i18n.DependencyInjection.UnitTests.Resources; +using Lepo.i18n.Yaml; namespace Lepo.i18n.DependencyInjection.UnitTests; @@ -18,8 +18,14 @@ public void FromResource_ShouldAddLocalizations_WhenResourceSetIsNotNull() _ = services.AddStringLocalizer(b => { - _ = b.FromResource("pl-PL"); - _ = b.FromResource("en-US"); + _ = b.FromYaml( + "Lepo.i18n.DependencyInjection.UnitTests.Resources.Translations-pl-PL.yaml", + new CultureInfo("pl-PL") + ); + _ = b.FromYaml( + "Lepo.i18n.DependencyInjection.UnitTests.Resources.Translations-en-US.yaml", + new CultureInfo("en-US") + ); }); ServiceProvider serviceProvider = services.BuildServiceProvider();