Skip to content

Commit 211770a

Browse files
committed
Added CultureUI import
1 parent 366296c commit 211770a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Globalization;
6+
using System.Linq;
67
using System.Runtime.InteropServices.JavaScript;
78

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

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

6768
if (culturesToLoad.Length == 0)
6869
{

src/Components/test/E2ETest/Tests/WebAssemblyLocalizationTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,23 @@ public void CanSetCultureAndReadLocalizedResources(string culture, string messag
3535
var messageDisplay = Browser.Exists(By.Id("message-display"));
3636
Assert.Equal(message, messageDisplay.Text);
3737
}
38+
39+
[Theory]
40+
[InlineData("en-US", "fr-FR", "Bonjour!")]
41+
[InlineData("fr-FR", "en-US", "Hello!")]
42+
public void CanSetCultureAndDifferentiateBetweenCurrentAndUICulture(string culture, string cultureUI, string message)
43+
{
44+
Navigate($"{ServerPathBase}/?culture={culture}&cultureUI={cultureUI}");
45+
46+
Browser.MountTestComponent<LocalizedText>();
47+
48+
var cultureDisplay = Browser.Exists(By.Id("culture-name-display"));
49+
Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
50+
51+
var cultureUIDisplay = Browser.Exists(By.Id("culture-ui-display"));
52+
Assert.Equal($"CultureUI is: {cultureUI}", cultureUIDisplay.Text);
53+
54+
var messageDisplay = Browser.Exists(By.Id("message-display"));
55+
Assert.Equal(message, messageDisplay.Text);
56+
}
3857
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<h3 id="culture-name-display">Culture is: @System.Globalization.CultureInfo.CurrentCulture.Name</h3>
2+
<h3 id="culture-ui-display">CultureUI is: @System.Globalization.CultureInfo.CurrentUICulture.Name</h3>
23
<p id="message-display">@Resources.Message</p>

src/Components/test/testassets/BasicTestApp/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private static void ConfigureCulture(WebAssemblyHost host)
6363
{
6464
// In the absence of a specified value, we want the culture to be en-US so that the tests for bind can work consistently.
6565
var culture = new CultureInfo("en-US");
66+
var cultureUI = new CultureInfo("en-US");
6667

6768
Uri uri = null;
6869
try
@@ -77,12 +78,18 @@ private static void ConfigureCulture(WebAssemblyHost host)
7778
if (uri != null && HttpUtility.ParseQueryString(uri.Query)["culture"] is string cultureName)
7879
{
7980
culture = new CultureInfo(cultureName);
81+
cultureUI = culture; // Default to the same culture for UI if not specified
82+
}
83+
84+
if (uri != null && HttpUtility.ParseQueryString(uri.Query)["cultureUI"] is string cultureUIName)
85+
{
86+
cultureUI = new CultureInfo(cultureUIName);
8087
}
8188

8289
// CultureInfo.CurrentCulture is async-scoped and will not affect the culture in sibling scopes.
8390
// Use CultureInfo.DefaultThreadCurrentCulture instead to modify the application's default scope.
8491
CultureInfo.DefaultThreadCurrentCulture = culture;
85-
CultureInfo.DefaultThreadCurrentUICulture = culture;
92+
CultureInfo.DefaultThreadCurrentUICulture = cultureUI;
8693
}
8794

8895
// Supports E2E tests in StartupErrorNotificationTest

0 commit comments

Comments
 (0)