Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Components/Web.JS/src/GlobalExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function prepareRuntimeConfig(options: Partial<WebAssemblyStartOptions>, onConfi
}

Blazor._internal.getApplicationEnvironment = () => loadedConfig.applicationEnvironment!;
Blazor._internal.getApplicationCulture = () => loadedConfig.applicationCulture!;

onConfigLoadedCallback?.(loadedConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> in the application's project file.");
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ internal interface IInternalJSImportMethods
string GetPersistedState();

string GetApplicationEnvironment();

string GetApplicationCulture();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static async Task<RootComponentOperationBatch> GetInitialComponentUpdate(
public string GetApplicationEnvironment()
=> GetApplicationEnvironmentCore();

public string GetApplicationCulture()
=> GetApplicationCultureCore();

public void AttachRootComponentToElement(string domElementSelector, int componentId, int rendererId)
=> AttachRootComponentToElementCore(domElementSelector, componentId, rendererId);

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ValueTask DisposeAsync()
private class TestSatelliteResourcesLoader : WebAssemblyCultureProvider
{
internal TestSatelliteResourcesLoader()
: base(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture)
: base(CultureInfo.CurrentCulture)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public TestInternalJSImportMethods(string environment = "Production")

public string GetApplicationEnvironment()
=> _environment;

public string GetApplicationCulture()
=> "en-US";

public string GetPersistedState()
=> null;
Expand Down
Loading