From 1aee7a961df2482f01f3fe66cc76b051a288a621 Mon Sep 17 00:00:00 2001 From: Daria Tiurina Date: Tue, 2 Sep 2025 16:03:26 +0200 Subject: [PATCH 1/3] Changed initial culture detection --- src/Components/Web.JS/src/GlobalExports.ts | 1 + src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts | 1 + .../WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs | 8 +++++--- .../WebAssembly/src/Services/IInternalJSImportMethods.cs | 2 ++ .../WebAssembly/src/Services/InternalJSImportMethods.cs | 6 ++++++ .../WebAssembly/test/TestInternalJSImportMethods.cs | 3 +++ 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Components/Web.JS/src/GlobalExports.ts b/src/Components/Web.JS/src/GlobalExports.ts index d5f3128d424a..a62996e70fb3 100644 --- a/src/Components/Web.JS/src/GlobalExports.ts +++ b/src/Components/Web.JS/src/GlobalExports.ts @@ -74,6 +74,7 @@ export interface IBlazor { renderBatch?: (browserRendererId: number, batchAddress: Pointer) => void; getConfig?: (fileName: string) => Uint8Array | undefined; getApplicationEnvironment?: () => string; + getApplicationCulture?: () => string; dotNetCriticalError?: any; loadLazyAssembly?: any; loadSatelliteAssemblies?: any; diff --git a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts index 1465e5f79cc8..a8804598b903 100644 --- a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts +++ b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts @@ -154,6 +154,7 @@ function prepareRuntimeConfig(options: Partial, onConfi } Blazor._internal.getApplicationEnvironment = () => loadedConfig.applicationEnvironment!; + Blazor._internal.getApplicationCulture = () => loadedConfig.applicationCulture!; onConfigLoadedCallback?.(loadedConfig); diff --git a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs index 99d1476dc10d..2f8c8fc18f41 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs @@ -32,7 +32,7 @@ internal WebAssemblyCultureProvider(CultureInfo initialCulture, CultureInfo init internal static void Initialize() { Instance = new WebAssemblyCultureProvider( - initialCulture: CultureInfo.CurrentCulture, + initialCulture: CultureInfo.GetCultureInfo(WebAssemblyCultureProviderInterop.GetApplicationCulture() ?? CultureInfo.InvariantCulture.Name), initialUICulture: CultureInfo.CurrentUICulture); } @@ -48,8 +48,7 @@ public void ThrowIfCultureChangeIsUnsupported() // The current method is invoked as part of WebAssemblyHost.RunAsync i.e. after user code in Program.MainAsync has run // thus allows us to detect if the culture was changed by user code. if (Environment.GetEnvironmentVariable("__BLAZOR_SHARDED_ICU") == "1" && - ((!CultureInfo.CurrentCulture.Name.Equals(InitialCulture.Name, StringComparison.Ordinal) || - !CultureInfo.CurrentUICulture.Name.Equals(InitialUICulture.Name, StringComparison.Ordinal)))) + (!CultureInfo.CurrentCulture.Name.Equals(InitialCulture.Name, StringComparison.Ordinal))) { throw new InvalidOperationException("Blazor detected a change in the application's culture that is not supported with the current project configuration. " + "To change culture dynamically during startup, set true in the application's project file."); @@ -118,5 +117,8 @@ private partial class WebAssemblyCultureProviderInterop { [JSImport("INTERNAL.loadSatelliteAssemblies")] public static partial Task LoadSatelliteAssemblies(string[] culturesToLoad); + + [JSImport("Blazor._internal.getApplicationCulture", "blazor-internal")] + public static partial string GetApplicationCulture(); } } diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/IInternalJSImportMethods.cs b/src/Components/WebAssembly/WebAssembly/src/Services/IInternalJSImportMethods.cs index 83f14d1bd2b1..560f64fa1154 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Services/IInternalJSImportMethods.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Services/IInternalJSImportMethods.cs @@ -8,6 +8,8 @@ internal interface IInternalJSImportMethods string GetPersistedState(); string GetApplicationEnvironment(); + + string GetApplicationCulture(); void AttachRootComponentToElement(string domElementSelector, int componentId, int rendererId); diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/InternalJSImportMethods.cs b/src/Components/WebAssembly/WebAssembly/src/Services/InternalJSImportMethods.cs index 811a78dbc652..628d1c4f7186 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Services/InternalJSImportMethods.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Services/InternalJSImportMethods.cs @@ -27,6 +27,9 @@ public static async Task GetInitialComponentUpdate( public string GetApplicationEnvironment() => GetApplicationEnvironmentCore(); + public string GetApplicationCulture() + => GetApplicationCultureCore(); + public void AttachRootComponentToElement(string domElementSelector, int componentId, int rendererId) => AttachRootComponentToElementCore(domElementSelector, componentId, rendererId); @@ -72,6 +75,9 @@ public string RegisteredComponents_GetParameterValues(int id) [JSImport("Blazor._internal.getApplicationEnvironment", "blazor-internal")] private static partial string GetApplicationEnvironmentCore(); + [JSImport("Blazor._internal.getApplicationCulture", "blazor-internal")] + private static partial string GetApplicationCultureCore(); + [JSImport("Blazor._internal.attachRootComponentToElement", "blazor-internal")] private static partial void AttachRootComponentToElementCore(string domElementSelector, int componentId, int rendererId); diff --git a/src/Components/WebAssembly/WebAssembly/test/TestInternalJSImportMethods.cs b/src/Components/WebAssembly/WebAssembly/test/TestInternalJSImportMethods.cs index 74e1d6b676ac..9aa4ebd48f62 100644 --- a/src/Components/WebAssembly/WebAssembly/test/TestInternalJSImportMethods.cs +++ b/src/Components/WebAssembly/WebAssembly/test/TestInternalJSImportMethods.cs @@ -16,6 +16,9 @@ public TestInternalJSImportMethods(string environment = "Production") public string GetApplicationEnvironment() => _environment; + + public string GetApplicationCulture() + => "en-US"; public string GetPersistedState() => null; From 09ad06fca636b5cfd32f93525c528efb4f62fe22 Mon Sep 17 00:00:00 2001 From: Daria Tiurina Date: Tue, 2 Sep 2025 17:00:44 +0200 Subject: [PATCH 2/3] Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts index a8804598b903..efb8cb7d233b 100644 --- a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts +++ b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts @@ -154,7 +154,7 @@ function prepareRuntimeConfig(options: Partial, onConfi } Blazor._internal.getApplicationEnvironment = () => loadedConfig.applicationEnvironment!; - Blazor._internal.getApplicationCulture = () => loadedConfig.applicationCulture!; + Blazor._internal.getApplicationCulture = () => loadedConfig.applicationCulture!; onConfigLoadedCallback?.(loadedConfig); From 34425fa67ca0b9ecebcb62dedc43a307e55035bf Mon Sep 17 00:00:00 2001 From: Daria Tiurina Date: Wed, 3 Sep 2025 10:46:39 +0200 Subject: [PATCH 3/3] Deleted InitialUICulture from WebAssemblyCultureProvider --- .../WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs | 8 ++------ .../test/Hosting/WebAssemblyCultureProviderTest.cs | 2 +- .../WebAssembly/test/Hosting/WebAssemblyHostTest.cs | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs index 2f8c8fc18f41..4d9f51634da0 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyCultureProvider.cs @@ -17,23 +17,19 @@ internal partial class WebAssemblyCultureProvider internal const string ReadSatelliteAssemblies = "window.Blazor._internal.readSatelliteAssemblies"; // For unit testing. - internal WebAssemblyCultureProvider(CultureInfo initialCulture, CultureInfo initialUICulture) + internal WebAssemblyCultureProvider(CultureInfo initialCulture) { InitialCulture = initialCulture; - InitialUICulture = initialUICulture; } public static WebAssemblyCultureProvider? Instance { get; private set; } public CultureInfo InitialCulture { get; } - public CultureInfo InitialUICulture { get; } - internal static void Initialize() { Instance = new WebAssemblyCultureProvider( - initialCulture: CultureInfo.GetCultureInfo(WebAssemblyCultureProviderInterop.GetApplicationCulture() ?? CultureInfo.InvariantCulture.Name), - initialUICulture: CultureInfo.CurrentUICulture); + initialCulture: CultureInfo.GetCultureInfo(WebAssemblyCultureProviderInterop.GetApplicationCulture() ?? CultureInfo.InvariantCulture.Name)); } public void ThrowIfCultureChangeIsUnsupported() diff --git a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs index 22ad7d18fffc..2406a919b2f6 100644 --- a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs +++ b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs @@ -47,7 +47,7 @@ public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICU try { // WebAssembly is initialized with en-US - var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US"), new CultureInfo("en-US")); + var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US")); // Culture is changed to fr-FR as part of the app using var cultureReplacer = new CultureReplacer("fr-FR"); diff --git a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostTest.cs b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostTest.cs index 552ca8272707..2083c8230b1b 100644 --- a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostTest.cs +++ b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostTest.cs @@ -97,7 +97,7 @@ public ValueTask DisposeAsync() private class TestSatelliteResourcesLoader : WebAssemblyCultureProvider { internal TestSatelliteResourcesLoader() - : base(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) + : base(CultureInfo.CurrentCulture) { }