Skip to content

Commit 87c9b6a

Browse files
fixed switching language
1 parent c91f6f8 commit 87c9b6a

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

AudioCuesheetEditor/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
using Microsoft.AspNetCore.Components.Web;
2727
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2828
using MudBlazor.Services;
29+
using System.Globalization;
30+
using System.Reflection;
2931
using Toolbelt.Blazor.Extensions.DependencyInjection;
3032

3133
var builder = WebAssemblyHostBuilder.CreateDefault(args);
@@ -64,6 +66,27 @@
6466

6567
builder.Services.AddHotKeys2();
6668

69+
// TODO: Remove this when https://github.com/dotnet/aspnetcore/issues/56824 is fixed
70+
71+
// Get current localization culture
72+
var currentCulture = CultureInfo.DefaultThreadCurrentCulture;
73+
74+
// Get WASM culture provider via reflection
75+
var type = Assembly.GetAssembly(typeof(WebAssemblyHost))!.GetType("Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyCultureProvider");
76+
var instance = type
77+
!.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static)
78+
?.GetValue(null);
79+
80+
// Swap out the "current culture" for the UI (localization) culture
81+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");
82+
// Load the satellite assemblies
83+
await (ValueTask)instance!
84+
.GetType()
85+
.GetMethod("LoadCurrentCultureResourcesAsync", BindingFlags.Public | BindingFlags.Instance)!
86+
.Invoke(instance, [])!;
87+
// Swap the culture back
88+
CultureInfo.DefaultThreadCurrentCulture = currentCulture;
89+
6790
var host = builder.Build();
6891

6992
await host.SetCultureFromConfigurationAsync();

AudioCuesheetEditor/Services/UI/LocalizationService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static IReadOnlyCollection<CultureInfo> AvailableCultures
4040

4141
public event EventHandler? LocalizationChanged;
4242

43-
public CultureInfo SelectedCulture { get; private set; } = CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentUICulture;
43+
public CultureInfo SelectedCulture => CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentUICulture;
4444

4545
public async Task SetCultureFromConfigurationAsync()
4646
{
@@ -58,14 +58,13 @@ public async Task ChangeLanguageAsync(string name)
5858
}
5959
}
6060

61-
private Boolean ChangeLanguage(string name)
61+
private static Boolean ChangeLanguage(string name)
6262
{
6363
var newCulture = AvailableCultures.SingleOrDefault(c => c.Name == name);
6464
if (newCulture != null)
6565
{
66-
SelectedCulture = newCulture;
67-
CultureInfo.DefaultThreadCurrentUICulture = SelectedCulture;
68-
CultureInfo.CurrentUICulture = SelectedCulture;
66+
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
67+
CultureInfo.CurrentUICulture = newCulture;
6968
}
7069
return newCulture != null;
7170
}

0 commit comments

Comments
 (0)