4

다국어 웹 사이트를 사용하려고 ASP.Core를 시도합니다. 그래서, 내 StartUp.cs에있는 내 _ViewImports.cs에서'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer'유형의 서비스가 등록되지 않았습니다.

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>(); 
} 

을 나는이 :

@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 

오류 :

An unhandled exception occurred while processing the request. 

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

답변

5

가 IHtmlLocalizer like the docs demonstrate.

에 유형을 추가하기
@inject IHtmlLocalizer<MyType> MyTypeLocalizer 

또한, 귀하는 ViewLocalization 서비스를 등록하지 않았습니다. 당신도 그렇게해야 할 수도 있습니다.

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

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

    ... 
+2

Thanks, services.AddMvc(). AddViewLocalization()이 도움이됩니다. – Rroman

+0

AddMvcLocalization()은 MvcLocalizationServices.AddMvcLocalizationServices를 사용합니다. IHtmlLocalizer <> 및 IViewLocalizer를 등록합니다. – Rroman

관련 문제