Search code examples
c#razorlocalizationinternationalizationrazor-pages

RazorPages with IStringLocalizer not working


I am currently creating a multilanguage Webapplication with Razorpages. I set everything up according to this JetBrains tutorial:

my current setup looks like this:

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            //services.AddMvc();

            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => options.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();

            services
                .AddRazorPages()
                .AddViewLocalization();

            services.Configure<RequestLocalizationOptions>(
                opts =>
                {
                    var culturesSupported = new[]
                    {
                        new CultureInfo("de"),
                        new CultureInfo("fr"),
                        new CultureInfo("it")
                    };
                    opts.DefaultRequestCulture = new RequestCulture("de");
                    opts.SupportedCultures = culturesSupported;
                    opts.SupportedUICultures = culturesSupported;

                });
        }

...

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRequestLocalization();

            app.UseRouting();

            //var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            //app.UseRequestLocalization(locOptions.Value);

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });
        }

and my razor View Page:

@page
@using Microsoft.Extensions.Localization
@model Template.Pages.EmployeeFormModel
@inject IStringLocalizer<EmployeeForm> Localizer
@{
    ViewData["Title"] = "EmployeeForm";
}

@section Styles  {
    <link href="~/css/formPages.css" rel="stylesheet" />
}

<div class="row align-items-center">
    <div class="col-md-6 offset-md-3">

        <img src="~/res/pictures/pic.png" class="form-picture" />

        <h1>Daten zum Neueintritt</h1>
        
        <hr />
        <p>
            @Localizer["text_willkommen"]
        </p>

But at the line with "@Inject IStringLocalizer Localizer" I get the error: "The type or namespace 'EmployeeForm' could not be found ..."

my file Structure looks like this:

Project
--Pages
----EmployeeForm.cshtml
----EmployeeForm.cshtml.cs
--Resources
----Pages
------EmployeeForm.resx
------EmployeeForm.it.resx
------EmployeeForm.fr.resx
--Startup.cs

Its my first time that I do something with Localisation, I followed the tutorial but it still wont work as I cant even build the application because of this error.


Solution

  • @model Template.Pages.EmployeeFormModel

    Your PageModel seems to be named EmployeeFormModel, not EmployeeForm. Change the localiser's type parameter accordingly:

    @inject IStringLocalizer<EmployeeFormModel> Localizer