How to use localized RESX in Blazor WASM?

Rada Matěj 200 Reputation points
2024-05-22T07:01:50.83+00:00

Hi,

all I want is standard "old way" usage of generated static properties of my RESX to localize texts based on CultureInfo and .culture.resx files. I've just figured out, that only difference from .NET Framework is that now in .NET 8 culture RESX naming is case-sensitive. With that, my .en-US.resx file is properly selected when culture is changed:

CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
string ddd = CommonStrings.ADTPFollowingDayTooltip;

However any other culture is falling into default RESX. Both cs-CZ and sk-SK cultures being absolutely ignored and even false RESX naming to any other culture is not going to switch from default RESX. However .en-US.resx file is correctly selected without anything changed in my basic WASM project - no Nugets or anything. I've also figured out how to load other cultures into WASM, however I still believe, that should not have any impact on RESX loading and seeking ... If I name any RESX by en-US culture, it is properly selected, but DE, FR, ... Nothing else is working (and falling back to default RESX).

(I have them properly included in project as embedded resources - just name change to en-US is working, so why not any other culture ...) Loading any other cultures is done by CSPROJ configuration (should not be needed):

<InvariantGlobalization>false</InvariantGlobalization>
<WasmIcuDataFileName>icudt_no_CJK.dat</WasmIcuDataFileName>
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,525 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,283 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
{count} votes

Accepted answer
  1. Tiny Wang-MSFT 2,006 Reputation points Microsoft Vendor
    2024-05-27T09:35:12.7533333+00:00

    Hi @Rada Matěj, I reproduced your issue finally in my side with your resource file and I also test if within blazor server app, and I found the issue only occurred in blazor wsam project, but in server app, it worked well.

    User's image

    This shall be the expected behavior of blazor wsam as CultureInfo.GetCultureInfo is defined as retrieving a cached, read-only instance of a culture, which only work in the server side.

    When we want to get different results based on different culture in blazor wsam app, we need to follow this section. I had a test in my side which worked well. In the meantime, it requires to reload the application by using Navigation.NavigateTo(Navigation.Uri, forceLoad: true);

    User's image

    User's image

    ==============================================

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Tiny

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful