DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Blazor wasm localization showing just keys

Followed Dynamically set the culture from the Accept-Language header to localize my blazor wasm app.

WebUI.csproj

<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>

Program.cs

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

Added folder:

Resources
   Shared.resx
   Shared.en.resx

_Imports.razor

@using WebUI.Resources

In view:

@using System.Globalization
@inject IStringLocalizer<Shared> loc
@CultureInfo.CurrentCulture
@loc["countries"]

Culture is showing DE-DE. @loc["countries"] just prints countries instead of the localization. Any ideas why?

Upvotes: 5

Views: 996

Answers (2)

pfx
pfx

Reputation: 23244

For (shared) resources, Blazor follows the same setup/rules as for ASP.NET Core.

In short, when you specify an options.ResourcesPath, its value is being added as a prefix to the name of your resource, which by default is namespace + resource name.

With that options.ResourcesPath set to Resources you get Resources.WebUI.Resources.Shared.resx instead of WebUI.Resources.Shared.resx.

To solve, don't set a ResourcesPath, just use

builder.Services.AddLocalization();

Link to ASP.NET Core question

Upvotes: 4

Quango
Quango

Reputation: 13458

You only specified an English localization file: Shared.en.resx.

When you specify @loc["countries"] in the component, it attempts to use the browser locale to translate the text "countries".

The localizer looks for a resource file Shared.de.resx but that doesn't exist, so it defaults to the base translation, in this case English.

Upvotes: 0

Related Questions