Blazor Serverside Localization with IStringLocalizer not taking correct resource file

1.1k Views Asked by At

I'm trying to add Localization in my serverside Blazor app. I have a seperate project which contains my Resource files for NL and EN.

enter image description here

My Startup.cs of my UI project(ConfigureServices)

    services.AddLocalization();
    services.Configure<RequestLocalizationOptions>(options =>
        {
            List<CultureInfo> supportedCultures =
                new List<CultureInfo>
                {
                    new CultureInfo("nl"),
                    new CultureInfo("en")
                };

            options.DefaultRequestCulture = new RequestCulture("nl");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });

Blazor file in my UI project:

 @inject IStringLocalizer<Menu> MenuLocalizer

 @MenuLocalizer["Overview"]
 @MenuLocalizer["Add"]

But the Localization from the resources never gets taken. Changing the DefaultRequestCulture in startup doesn't also change anything.

I have searched on the net and it looks like I'm doing everything as it should, but no result.

EDIT: I've made some changes to my resource files, added an explicit NL file. It seems that the localization always takes the EN version although I specify NL in my StartUp.

enter image description here

enter image description here

So it looks like the localization is working, but not taking the correct file.

EDIT 2: Fixed it by adding:

options.RequestCultureProviders = new List<IRequestCultureProvider>
                    {
                        new QueryStringRequestCultureProvider(),
                        new CookieRequestCultureProvider()
                    };

Now it takes the file defined in the startup.

0

There are 0 best solutions below