Skip to content

Commit 33cb482

Browse files
committed
Update tests
1 parent 47b1b39 commit 33cb482

7 files changed

+62
-7
lines changed

src/Lepo.i18n/LocalizationBuilderExtensions.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,16 @@ public static LocalizationBuilder FromResource(
156156
CultureInfo culture
157157
)
158158
{
159+
CultureInfo cultureToRestore = Thread.CurrentThread.CurrentCulture;
160+
161+
// NOTE: Fix net framework satellite assembly loading
159162
try
160163
{
161-
// NOTE: Fix net framework satellite assembly loading
164+
Thread.CurrentThread.CurrentCulture = culture;
165+
Thread.CurrentThread.CurrentUICulture = culture;
162166

163167
ResourceManager resourceManager = new(baseName, assembly);
168+
164169
ResourceSet? resourceSet = resourceManager.GetResourceSet(culture, true, true);
165170

166171
if (resourceSet is null)
@@ -175,11 +180,17 @@ CultureInfo culture
175180

176181
builder.AddLocalization(new LocalizationSet(baseName, culture, localizations));
177182

183+
Thread.CurrentThread.CurrentCulture = cultureToRestore;
184+
Thread.CurrentThread.CurrentUICulture = cultureToRestore;
185+
178186
return builder;
179187
}
180188
catch (MissingManifestResourceException ex)
181189
{
182-
throw new LocalizationBuilderException("Failed to register translation resources.", ex);
190+
throw new LocalizationBuilderException(
191+
$"Failed to register translation resources for \"{culture}\".",
192+
ex
193+
);
183194
}
184195
}
185196
}

tests/Lepo.i18n.DependencyInjection.UnitTests/GlobalUsings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
global using System;
77
global using System.Collections.Generic;
8+
global using System.Globalization;
89
global using System.Reflection;
910
global using FluentAssertions;
1011
global using Microsoft.Extensions.DependencyInjection;

tests/Lepo.i18n.DependencyInjection.UnitTests/Lepo.i18n.DependencyInjection.UnitTests.csproj

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<IsTestProject>true</IsTestProject>
77
<IsTrimmable>false</IsTrimmable>
88
</PropertyGroup>
99

10+
11+
<ItemGroup>
12+
<None Remove="Resources\Translations-pl-PL.yaml" />
13+
<None Remove="Resources\Translations-en-US.yaml" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<EmbeddedResource Include="Resources\Translations-pl-PL.yaml" />
18+
<EmbeddedResource Include="Resources\Translations-en-US.yaml" />
19+
</ItemGroup>
20+
1021
<ItemGroup>
1122
<PackageReference Include="FluentAssertions" />
1223
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />

tests/Lepo.i18n.DependencyInjection.UnitTests/Resources/Test.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
namespace Lepo.i18n.DependencyInjection.UnitTests.Resources;
77

8-
public class Test;
8+
public partial class Test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Comment
2+
Test: 'Test in english'
3+
main.languages: Languages
4+
main.hello: "Hello world"
5+
6+
namespace.test:
7+
main.languages: Languages in namespace.test #yet another comment
8+
main.hello: 'Hello world in namespace.test'
9+
10+
# Some comment
11+
other.namespace:
12+
main.languages: Languages in other.namespace #yet another comment
13+
main.hello: 'Hello world in other.namespace'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Comment
2+
Test: 'Test po polsku'
3+
main.languages: Języki
4+
main.hello: "Witaj świecie"
5+
6+
namespace.test:
7+
main.languages: Języki w namespace.test #yet another comment
8+
main.hello: 'Witaj świecie w namespace.test'
9+
10+
# Some comment
11+
other.namespace:
12+
main.languages: Języki w other.namespace #yet another comment
13+
main.hello: 'Witaj świecie w other.namespace'

tests/Lepo.i18n.DependencyInjection.UnitTests/StringLocalizerBuilderExtensionsTests.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
44
// All Rights Reserved.
55

6-
using Lepo.i18n.DependencyInjection.UnitTests.Resources;
6+
using Lepo.i18n.Yaml;
77

88
namespace Lepo.i18n.DependencyInjection.UnitTests;
99

@@ -18,8 +18,14 @@ public void FromResource_ShouldAddLocalizations_WhenResourceSetIsNotNull()
1818

1919
_ = services.AddStringLocalizer(b =>
2020
{
21-
_ = b.FromResource<Test>("pl-PL");
22-
_ = b.FromResource<Test>("en-US");
21+
_ = b.FromYaml(
22+
"Lepo.i18n.DependencyInjection.UnitTests.Resources.Translations-pl-PL.yaml",
23+
new CultureInfo("pl-PL")
24+
);
25+
_ = b.FromYaml(
26+
"Lepo.i18n.DependencyInjection.UnitTests.Resources.Translations-en-US.yaml",
27+
new CultureInfo("en-US")
28+
);
2329
});
2430

2531
ServiceProvider serviceProvider = services.BuildServiceProvider();

0 commit comments

Comments
 (0)