2017-11-20 4 views
2

현재 다국어 ASP.NET Core 2.0 웹 사이트를 개발 중입니다. 나는 official documantion을 읽고 GitHub에 제공된 example을 조사했습니다.ASP.NET Core 2.0 번역 항상 영어로

enter image description here

집어 들고 코드 내 Startup.cs에서 : 아래

이 프로젝트의 폴더 구조 내보기에서

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 

     // Add application services. 
     services.AddTransient<IEmailSender, EmailSender>(); 

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

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

     // Configure supported cultures and localization options 
     services.Configure<RequestLocalizationOptions>(options => 
     { 
      var supportedCultures = new[] 
      { 
       new CultureInfo("nl"), 
       //new CultureInfo("en") 
      }; 

      // State what the default culture for your application is. This will be used if no specific culture 
      // can be determined for a given request. 
      options.DefaultRequestCulture = new RequestCulture(culture: "nl", uiCulture: "nl"); 

      // You must explicitly state which cultures your application supports. 
      // These are the cultures the app supports for formatting numbers, dates, etc. 
      options.SupportedCultures = supportedCultures; 

      // These are the cultures the app supports for UI strings, i.e. we have localized resources for. 
      options.SupportedUICultures = supportedCultures; 
     }); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     //use the configured localization options for each request. 
     var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); 
     app.UseRequestLocalization(localizationOptions.Value); 

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


     app.UseStaticFiles(); 

     app.UseAuthentication(); 

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

(예를 Home/Index에 대해) 나는 로컬 라이저를 호출 <h1>@Localizer["Welcome"]</h1>. 열쇠 WelcomeIndex.nl.resx- 파일에 있지만 불행히도 네덜란드어로 번역 된 적이 없습니다.

URL을 ?culture=nl으로 호출하여 명시 적으로 문화권을 변경하려고 시도했지만 브라우저 언어를 네덜란드어로 변경했지만 두 가지 모두 해당 작업을 수행하지 않았습니다.

내가 누락 된 항목이 있습니까? 내 Home/Index.cshtml 파일 아래

편집 :

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

<h1>@Localizer["Welcome"]</h1> 

주입은 _ViewImports.cshtml 파일로 수행됩니다

@using Microsoft.AspNetCore.Mvc.Localization 
@inject IViewLocalizer Localizer 
+0

당신이 할 수있는 너의 시야를 보여? @inject IViewLocalizer Localizer를 삽입 했습니까? 사용 가능한 리소스 파일입니까? – Tester

+0

내 의견을 추가했습니다. 보시다시피 조용한 간단합니다. 리소스 파일은 솔루션 탐색기의 스크린 샷에서 볼 수 있듯이'Translations/Views/Home/Index.nl.resx'에 있습니다. – user2810895

답변

0

this answer에 TMG에서 설명 된 기술을 사용해보십시오. 매개 변수없이 간단한 전화로 Configure() 기능에 app.UseRequsestLocalization

CultureInfo[] supportedCultures = new[] 
{ 
    new CultureInfo("en"), 
    new CultureInfo("nl") 
}; 

services.Configure<RequestLocalizationOptions>(options => 
{ 
    options.DefaultRequestCulture = new RequestCulture("nl"); 
    options.SupportedCultures  = supportedCultures; 
    options.SupportedUICultures  = supportedCultures; 
    options.RequestCultureProviders = new List<IRequestCultureProvider> 
    { 
     new QueryStringRequestCultureProvider(), 
     new CookieRequestCultureProvider() 
    }; 
}); 

을하고 전화를 대체 :

당신이 당신의 ConfigureServices() 함수에 다음 코드를 추가해야한다는 것을 의미

app.UseRequestLocalization(); 
관련 문제