Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try with ASP.Core to have a multilanguages website. So, I have in my StartUp.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization();
    services.Configure<RequestLocalizationOptions>(
    opts =>
    {
        var supportedCultures = new[]
        {
            new CultureInfo("de-DE"),
            new CultureInfo("de"),
            new CultureInfo("fr-FR"),
            new CultureInfo("fr"),
        };
        opts.DefaultRequestCulture = new RequestCulture("fr-FR");
        // Formatting numbers, dates, etc.
        opts.SupportedCultures = supportedCultures;
        // UI strings that we have localized.
        opts.SupportedUICultures = supportedCultures;
    });
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
    services.AddMvc();
    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

In my _ViewImports.cs I have:

@using System.Threading.Tasks
@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options

@inject IHtmlLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

The errors:

An unhandled exception occurred while processing the request.

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer' has been registered.
like image 486
Yann Btd Avatar asked Oct 27 '16 13:10

Yann Btd


People also ask

What is localization in. net Core?

In this article Localization is the process of translating an application's resources into localized versions for each culture that the application will support.

What is localization in asp net?

Localization is the process of customizing the globalized web application to a specific locale and culture. Various resources such as images and text for the specific locale are created. The resource file in localization is scoped to a particular page in an application.


2 Answers

Add a type to the IHtmlLocalizer like the docs demonstrate.

@inject IHtmlLocalizer<MyType> MyTypeLocalizer

Also, I noticed that you have not registered the ViewLocalization service. You might need to do that too.

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

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

    ...
like image 102
Shaun Luttin Avatar answered Nov 18 '22 19:11

Shaun Luttin


Asp.net core 5:

services
.AddControllersWithViews()
.AddViewLocalization();
            
like image 4
foad abdollahi Avatar answered Nov 18 '22 18:11

foad abdollahi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!