mariob
mariob

Reputation: 1

ASP.NET Core 7.0 Razor Page View Model's localizated strings display only as Keys

I use ASP.NET Core 7.0 I'm trying to localize the login error message (or any other string) on my Identity/Account/Login razor page (it has been generated by the Visual Studio 2022 earlier on, so it resides in Root-Folder/Areas/Identity/Account/Login.cshtml) I've figured out what for my project's structure I've got to add an exact same folder structure for my resources files to make the IViewLocalizer correctly displaying localized text: MyPagesFolderStructure But then the problem arises with IStringLocalizer because, as I read in the docs, the IStringLocalizer requires some other resources file, placed elswhere. And this is where the problem arises, as I can't find an appropriate combination of folders and file name to make this working... errorMsg As it may be seen - IViewLocalizer which I use in my Razor Pages for displaying localized strings on the razor page itself (Polish signs) is working well, but the IStringLocalizer - don't.

Configuration

Program.cs:

        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

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

            builder.Services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new[] { "pl-PL", "en-US" };
                var localizationOptions = new RequestLocalizationOptions()
                    .SetDefaultCulture(supportedCultures[0])
                    .AddSupportedCultures(supportedCultures)
                    .AddSupportedUICultures(supportedCultures);

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

            (...)
            (and at the end...)

            var app = builder.Build();

            var localizationOptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>()?.Value;
            if (localizationOptions != null)
            {
                app.UseRequestLocalization(localizationOptions);
            }

            (...)

I tried mentioned in the docs localization of those resx files (instead of Resources/Areas/Identity/Pages/Account/resxFiles I used Resources/ViewModels/Identity/Account/resxFiles or even Resources/ViewModels/Identity/Pages/Account/resxFiles

None of the above seems to work

Actually as I reviewed this issue it gave me some hint, about something called "SharedResource" but actually C# comment of this IStringLocalizer seems misleading in this idea: comment

If there is someone who knows where is this IStringLocalizer looking for the resx files... Any help will be highly appreciated

Upvotes: 0

Views: 337

Answers (1)

mariob
mariob

Reputation: 1

I've found out that the easiest solution of this issue is to write a custom IStringLocalizer, which use JSON files instead of resx

https://codewithmukesh.com/blog/json-based-localization-in-aspnet-core/

Upvotes: 0

Related Questions