Skip to content

Commit 73ee485

Browse files
authored
Changed initial culture detection (#63507)
1 parent c462180 commit 73ee485

File tree

8 files changed

+21
-10
lines changed

8 files changed

+21
-10
lines changed

src/Components/Web.JS/src/GlobalExports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export interface IBlazor {
7575
renderBatch?: (browserRendererId: number, batchAddress: Pointer) => void;
7676
getConfig?: (fileName: string) => Uint8Array | undefined;
7777
getApplicationEnvironment?: () => string;
78+
getApplicationCulture?: () => string;
7879
dotNetCriticalError?: any;
7980
loadLazyAssembly?: any;
8081
loadSatelliteAssemblies?: any;

src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ function prepareRuntimeConfig(options: Partial<WebAssemblyStartOptions>, onConfi
154154
}
155155

156156
Blazor._internal.getApplicationEnvironment = () => loadedConfig.applicationEnvironment!;
157+
Blazor._internal.getApplicationCulture = () => loadedConfig.applicationCulture!;
157158

158159
onConfigLoadedCallback?.(loadedConfig);
159160

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,19 @@ internal partial class WebAssemblyCultureProvider
1717
internal const string ReadSatelliteAssemblies = "window.Blazor._internal.readSatelliteAssemblies";
1818

1919
// For unit testing.
20-
internal WebAssemblyCultureProvider(CultureInfo initialCulture, CultureInfo initialUICulture)
20+
internal WebAssemblyCultureProvider(CultureInfo initialCulture)
2121
{
2222
InitialCulture = initialCulture;
23-
InitialUICulture = initialUICulture;
2423
}
2524

2625
public static WebAssemblyCultureProvider? Instance { get; private set; }
2726

2827
public CultureInfo InitialCulture { get; }
2928

30-
public CultureInfo InitialUICulture { get; }
31-
3229
internal static void Initialize()
3330
{
3431
Instance = new WebAssemblyCultureProvider(
35-
initialCulture: CultureInfo.CurrentCulture,
36-
initialUICulture: CultureInfo.CurrentUICulture);
32+
initialCulture: CultureInfo.GetCultureInfo(WebAssemblyCultureProviderInterop.GetApplicationCulture() ?? CultureInfo.InvariantCulture.Name));
3733
}
3834

3935
public void ThrowIfCultureChangeIsUnsupported()
@@ -48,8 +44,7 @@ public void ThrowIfCultureChangeIsUnsupported()
4844
// The current method is invoked as part of WebAssemblyHost.RunAsync i.e. after user code in Program.MainAsync has run
4945
// thus allows us to detect if the culture was changed by user code.
5046
if (Environment.GetEnvironmentVariable("__BLAZOR_SHARDED_ICU") == "1" &&
51-
((!CultureInfo.CurrentCulture.Name.Equals(InitialCulture.Name, StringComparison.Ordinal) ||
52-
!CultureInfo.CurrentUICulture.Name.Equals(InitialUICulture.Name, StringComparison.Ordinal))))
47+
(!CultureInfo.CurrentCulture.Name.Equals(InitialCulture.Name, StringComparison.Ordinal)))
5348
{
5449
throw new InvalidOperationException("Blazor detected a change in the application's culture that is not supported with the current project configuration. " +
5550
"To change culture dynamically during startup, set <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> in the application's project file.");
@@ -118,5 +113,8 @@ private partial class WebAssemblyCultureProviderInterop
118113
{
119114
[JSImport("INTERNAL.loadSatelliteAssemblies")]
120115
public static partial Task LoadSatelliteAssemblies(string[] culturesToLoad);
116+
117+
[JSImport("Blazor._internal.getApplicationCulture", "blazor-internal")]
118+
public static partial string GetApplicationCulture();
121119
}
122120
}

src/Components/WebAssembly/WebAssembly/src/Services/IInternalJSImportMethods.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ internal interface IInternalJSImportMethods
88
string GetPersistedState();
99

1010
string GetApplicationEnvironment();
11+
12+
string GetApplicationCulture();
1113

1214
void AttachRootComponentToElement(string domElementSelector, int componentId, int rendererId);
1315

src/Components/WebAssembly/WebAssembly/src/Services/InternalJSImportMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public static async Task<RootComponentOperationBatch> GetInitialComponentUpdate(
2727
public string GetApplicationEnvironment()
2828
=> GetApplicationEnvironmentCore();
2929

30+
public string GetApplicationCulture()
31+
=> GetApplicationCultureCore();
32+
3033
public void AttachRootComponentToElement(string domElementSelector, int componentId, int rendererId)
3134
=> AttachRootComponentToElementCore(domElementSelector, componentId, rendererId);
3235

@@ -72,6 +75,9 @@ public string RegisteredComponents_GetParameterValues(int id)
7275
[JSImport("Blazor._internal.getApplicationEnvironment", "blazor-internal")]
7376
private static partial string GetApplicationEnvironmentCore();
7477

78+
[JSImport("Blazor._internal.getApplicationCulture", "blazor-internal")]
79+
private static partial string GetApplicationCultureCore();
80+
7581
[JSImport("Blazor._internal.attachRootComponentToElement", "blazor-internal")]
7682
private static partial void AttachRootComponentToElementCore(string domElementSelector, int componentId, int rendererId);
7783

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICU
4747
try
4848
{
4949
// WebAssembly is initialized with en-US
50-
var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US"), new CultureInfo("en-US"));
50+
var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US"));
5151

5252
// Culture is changed to fr-FR as part of the app
5353
using var cultureReplacer = new CultureReplacer("fr-FR");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public ValueTask DisposeAsync()
9797
private class TestSatelliteResourcesLoader : WebAssemblyCultureProvider
9898
{
9999
internal TestSatelliteResourcesLoader()
100-
: base(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture)
100+
: base(CultureInfo.CurrentCulture)
101101
{
102102
}
103103

src/Components/WebAssembly/WebAssembly/test/TestInternalJSImportMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public TestInternalJSImportMethods(string environment = "Production")
1616

1717
public string GetApplicationEnvironment()
1818
=> _environment;
19+
20+
public string GetApplicationCulture()
21+
=> "en-US";
1922

2023
public string GetPersistedState()
2124
=> null;

0 commit comments

Comments
 (0)