Lachevre
Lachevre

Reputation: 612

Some resx files not loaded in blazor wasm application

I got a blazor wasm app using the IStringLocalizer for multilanguages. The resources files are located in a class library. Here is the project arborescence :

In each of the resource files is a string key Test translated in each language. I'm currently loading my culture like this (for testing purposes) in Program.cs:

        builder.Services.AddLocalization();

        CultureInfo defaultCulture = new CultureInfo("de-DE");
        CultureInfo.DefaultThreadCurrentCulture = defaultCulture;
        CultureInfo.DefaultThreadCurrentUICulture = defaultCulture;
       

A reference to the multilanguages project is also added. When I Inject my stringlocalizer in a test component like this :

        @inject IStringLocalizer<Resource> localizer
        <h3>@localizer[Resource.Test]</h3>

The Test key is correctly retrieved when the currentCulture is either set to fr-FR or en-US but not when set to de-DE, then it fallsback to en-US automatically.

I checked many times, the resources files are exactly the same except for the translation, set as public, embedded resources but the de-DE is not loading, same goes for es-ES if I add a new resx file. I don't know what I'm missing here...

Any ideas?

Upvotes: 1

Views: 1055

Answers (1)

Tore Aurstad
Tore Aurstad

Reputation: 3826

I have added a demo application for Blazor WASM and localization and this article of mine explains the demo app. It features a language picker where you on demand can set the language while the Blazor WASM app is running.

https://toreaurstad.blogspot.com/2023/07/localizing-blazor-wasm-applications.html?m=0

Github repo is here: https://github.com/toreaurstadboss/HelloBlazorLocalization

You mention that some languages get loaded, while others are not displaying. Could you check your Program.cs if you have forgotten some setup ?

If you check here: https://github.com/toreaurstadboss/HelloBlazorLocalization/blob/main/HelloBlazorLocalization/Program.cs

You can inspect in that code how I specifically set up the supported languages. Also, double check your browser's language setting. Perhaps you have set up a specific variant of German in your browser that does not seem to agree with de-DE default german language code. Also check specifically if the .resx file differs with other .resx files.

Sorry for not being able to further pinpoint what seems to be the error for you, but if you run my sample demo app you might see what is missing in your Blazor WASM app for getting localization in Blazor to work.

Upvotes: 0

Related Questions