4

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?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • If you are seeing the key and not the Localization, it means it can't find the files. Also, de-DE is not part of your Localized files, maybe it doesn't fall back to the default? – Shuryno Jan 05 '22 at 15:00
  • 1
    Give it a try as `builder.Services.AddLocalization();` without setting `ResourcesPath`. – pfx Jan 06 '22 at 19:18
  • @pfx that worked. Can you make this an answer and eleborate why this works? – DarkLeafyGreen Jan 07 '22 at 13:03

2 Answers2

3

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

pfx
  • 20,323
  • 43
  • 37
  • 57
0

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.

Quango
  • 12,338
  • 6
  • 48
  • 83