Search code examples
c#wpfblazor-webassembly.net-8.0asp.net-core-localization

How do I add localization to a WPF Blazor app?


I am trying to add localization to my WPF Blazor app but it's not working. The IStringLocalizer I inject with shared resource always returns the default English resource even though I set a different culture.

I have made WPF Blazor app before.

I am trying to do this again, but this time I tried adding localization as per Microsoft's documentation and others

(Except rather than relying on local storage, I rely on a configuration file. I can load the culture info from the configuration and set the DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture with the CultureInfo I want)

This works well for a standard Blazor WASM app but doesn't seem to work for a WPF Blazor app

When I try to get the localized string, it always returns the default English resource.

I suspect it has something to do with the removal of this line

- await builder.Build().RunAsync();

with replacement of

builder.Services.AddLocalization();

var host = builder.Build();

const string defaultCulture = "en-US";

var js = host.Services.GetRequiredService<IJSRuntime>();
var result = await js.InvokeAsync<string>("blazorCulture.get");
var culture = CultureInfo.GetCultureInfo(result ?? defaultCulture);

if (result == null)
{
    await js.InvokeVoidAsync("blazorCulture.set", defaultCulture);
}

CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

await host.RunAsync();

but I don't know how to access the host after it's built, but before it's run. When using WPF Blazor app I can just set up my services for my Blazor components like so

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var serviceCollection = new ServiceCollection();
        serviceCollection.AddWpfBlazorWebView();
        Resources.Add("services", serviceCollection.BuildServiceProvider());
    }
}

Does anyone know how to access the host after its built but before its run? Or more preferably how to get localization to work in a WPF Blazor app?

Coincidently, does anyone know why culture must be set after the host is built but before its run? I'm not sure I fully understand how it works.

I also investigated this as it gives access to the host, but I couldn't get the .NET 8 version to work https://github.com/DotNetExtension/BlazorDesktop

Edits:

Perhaps I should try to use Extensions.Hosting? would that even help?

aha

In this article, you will learn how to use the IStringLocalizer and IStringLocalizerFactory implementations. All of the example source code in this article relies on the Microsoft.Extensions.Localization and Microsoft.Extensions.Hosting NuGet packages. For more information on hosting, see .NET Generic Host.

Maybe it will help


Solution

  • As I mentioned in my edit the reason why it didn't work is in the documentation: I needed Microsoft.Extensions.Hosting hosted application I loosely followed this on how to convert my WPF to a hosted application

    Instead of

    using Host.CreateDefaultBuilder

    I used Host.CreateApplicationBuilder to avoid using the call back structure .NET is moving away from. I just made a Program.cs as my StartupObject in my project file, so it does feel very similar to a .NET 8 blazor app. Everything works now!