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..efb8cb7d233b 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..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.CurrentCulture, - initialUICulture: CultureInfo.CurrentUICulture); + initialCulture: CultureInfo.GetCultureInfo(WebAssemblyCultureProviderInterop.GetApplicationCulture() ?? CultureInfo.InvariantCulture.Name)); } public void ThrowIfCultureChangeIsUnsupported() @@ -48,8 +44,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 +113,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/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) { } 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;