Skip to content

Commit 3434e4d

Browse files
committed
Test and feedback
1 parent 211770a commit 3434e4d

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Globalization;
6-
using System.Linq;
76
using System.Runtime.InteropServices.JavaScript;
87

98
namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -63,7 +62,7 @@ public virtual async ValueTask LoadCurrentCultureResourcesAsync()
6362
throw new PlatformNotSupportedException("This method is only supported in the browser.");
6463
}
6564

66-
var culturesToLoad = GetCultures(CultureInfo.CurrentCulture).Union(GetCultures(CultureInfo.CurrentUICulture)).ToArray();
65+
var culturesToLoad = GetCultures(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
6766

6867
if (culturesToLoad.Length == 0)
6968
{
@@ -73,25 +72,34 @@ public virtual async ValueTask LoadCurrentCultureResourcesAsync()
7372
await WebAssemblyCultureProviderInterop.LoadSatelliteAssemblies(culturesToLoad);
7473
}
7574

76-
internal static string[] GetCultures(CultureInfo cultureInfo)
75+
internal static string[] GetCultures(CultureInfo? cultureInfo, CultureInfo? uiCultureInfo = null)
7776
{
7877
var culturesToLoad = new List<string>();
7978

79+
if (uiCultureInfo == null)
80+
{
81+
uiCultureInfo = cultureInfo;
82+
}
83+
8084
// Once WASM is ready, we have to use .NET's assembly loading to load additional assemblies.
8185
// First calculate all possible cultures that the application might want to load. We do this by
8286
// starting from the current culture and walking up the graph of parents.
8387
// At the end of the the walk, we'll have a list of culture names that look like
8488
// [ "fr-FR", "fr" ]
85-
while (cultureInfo != null && cultureInfo != CultureInfo.InvariantCulture)
89+
while ((cultureInfo != null && cultureInfo != CultureInfo.InvariantCulture) || (uiCultureInfo != null && uiCultureInfo != CultureInfo.InvariantCulture))
8690
{
87-
culturesToLoad.Add(cultureInfo.Name);
91+
if (cultureInfo != null && cultureInfo != CultureInfo.InvariantCulture)
92+
{
93+
culturesToLoad.Add(cultureInfo.Name);
94+
}
8895

89-
if (cultureInfo.Parent == cultureInfo)
96+
if (uiCultureInfo?.Name != cultureInfo?.Name && uiCultureInfo != null && uiCultureInfo != CultureInfo.InvariantCulture)
9097
{
91-
break;
98+
culturesToLoad.Add(uiCultureInfo.Name);
9299
}
93100

94-
cultureInfo = cultureInfo.Parent;
101+
cultureInfo = cultureInfo?.Parent;
102+
uiCultureInfo = uiCultureInfo?.Parent;
95103
}
96104

97105
return culturesToLoad.ToArray();

src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public void GetCultures_ReturnsCultureClosure(string cultureName, string[] expec
2424
Assert.Equal(expected, actual);
2525
}
2626

27+
[Theory]
28+
[InlineData("fr-FR", "tzm-Latn-DZ", new[] { "fr-FR", "tzm-Latn-DZ", "fr", "tzm-Latn", "tzm" })]
29+
public void GetCultures_ReturnCultureClosureWithUICulture(string cultureName, string uiCultureName, string[] expected)
30+
{
31+
// Arrange
32+
var culture = new CultureInfo(cultureName);
33+
var uiCulture = new CultureInfo(uiCultureName);
34+
// Act
35+
var actual = WebAssemblyCultureProvider.GetCultures(culture, uiCulture);
36+
// Assert
37+
Assert.Equal(expected, actual);
38+
}
39+
2740
[Fact]
2841
public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICUShardingIsUsed()
2942
{

0 commit comments

Comments
 (0)