简体   繁体   中英

Embed view localization in ASP.NET Core

I have a ASP.NET website. I successfully managed to load embed views from another project, but I can't get localization to run. It works fine with views from the startup assembly but when loading the embed ones there is a error message:

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer' has been registered.

The code for the view is as following:

_ViewImports.cshtml (embed):

@using Microsoft.AspNetCore.Mvc.Localization
@inject IHtmlLocalizer Localizer
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Index.cshtml (embed)

@{
    ViewData["Title"] = Localizer["Home"];
}

<div>
    @Localizer["Test"]
</div>

My startup.cs (in the main project) looks like this:

namespace My.Name.Space //Not the real one
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            //Standard builder function
        }

        public IConfigurationRoot Configuration { get; set; }

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

            //Add EF DbContext, identity, etc.

            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

            //Localization

            var supportedCultures = GetListOfCultures();

            services.Configure<RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(locOptions.Value);

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

                // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                try
                {
                    using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                        .CreateScope())
                    {
                        serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                             .Database.Migrate();
                    }
                }
                catch { }
            }

            app.UseStaticFiles();

            app.UseIdentity();

            // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

            app.UseMvc(routes =>
            {
                // Areas support
                routes.MapRoute(
                    name: "areaRoute",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

Am I missing something or is it a bug in RC2?

Greetings,

I have the exact same problem with my application upgrading from RC1 to RC2.

I changed

services.AddLocalization(options => options.ResourcesPath = "Resources");

to

services.AddLocalization();

You need to have your *.resx files in the Resources folder of your Web project for this to work.

Turns out I put

@inject IHtmlLocalizer Localizer

instead of

@inject IViewLocalizer Localizer

Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM