6

microsofts documentation 다음에 나오는 asp .NET Core 1.0 RTM 웹 응용 프로그램의 현지화를 구현하려했으나 제대로 작동하지 않습니다. 내가 가지고있는 문제는 문화를 설정하려고해도 영어 리소스 파일의 문자열을 항상 표시한다는 것입니다.ASP .NET Core 1.0 RTM 지역화가 작동하지 않습니다.

누구나 시간이 5 분이라면 귀하의 의견에 진심으로 감사드립니다.

다음은 현지화에 관하여 내 Startup.cs 내용이다 :

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 
    services.AddMvc() 
    .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder) 
    .AddDataAnnotationsLocalization(); 
    services.AddScoped<LocalizationFilter>(); 
    services.AddLocalization(options => options.ResourcesPath = "Resources"); 

    var supportedCultures = new List<CultureInfo>{ 
    new CultureInfo("en-US"), 
    new CultureInfo("sl-SI"), 
    new CultureInfo("de-DE"), 
    new CultureInfo("hr-HR") 
    }; 
    services.Configure<RequestLocalizationOptions>(options => 
    { 
    options.SupportedCultures = supportedCultures; 
    options.SupportedUICultures = supportedCultures; 
    options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"), 
     new CultureInfo("en-US")); 
    }); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    ILoggerFactory loggerFactory) 
{ 
     ... 
     var supportedCultures = new List<CultureInfo>{ 
     new CultureInfo("en-US"), 
     new CultureInfo("sl-SI"), 
     new CultureInfo("de-DE"), 
     new CultureInfo("hr-HR") 
     }; 
     var requestLocalizationOptions = new RequestLocalizationOptions 
     { 
     SupportedCultures = supportedCultures, 
     SupportedUICultures = supportedCultures, 
     DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"), 
      new CultureInfo("en-US")) 
     }; 
     app.UseRequestLocalization(requestLocalizationOptions); 
} 

을 그리고 이것은 내가 또한 일을 시도했습니다

actionContext.HttpContext.Response.Cookies.Append(
    CookieRequestCultureProvider.DefaultCookieName, 
    CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
    new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }); 
CultureInfo.CurrentCulture = culture; 
CultureInfo.CurrentUICulture = culture; 

내 ActionFilter에 OnActionExecuting 내부의 문화를 변화하고있어 어떻게 내 컨트롤러에 운이없는이.

내 의견으로는 @inject IViewLocalizer Localizer을 사용하여 현지화 된 문자열을 @Localizer["Name"]과 함께 표시하고 있습니다.

내 resoucres은과 같이/폴더에 컨트롤러 명을 자료/조회수 위치 :

  • 자료/뷰/컨트롤러 명/ViewName.en.resx
  • 자료/뷰/컨트롤러 명/ViewName.sl 의 .resx
  • ...

가 표시되는 문자열은 .EN 리소스에서 항상 아무리 내가 문화를 고수하려고해도 제가 누락 된 것이 있습니까? 내가해야 할 일이 더 있니? 내가 갖고있는 문제는 언어를 정하는 것 같습니다. 설명서에 따라 쿠키를 Response.Cookies.Append으로 설정해야하지만 CultureInfo.CurrentCulture으로 시도했지만 Thread.CurrentThread.CurentCulture은 더 이상 사용할 수 없습니다.

정말 실종 된지 모르겠습니다. 어떤 아이디어라도?

답변

4

당신은 아마 타격 다음 https://github.com/aspnet/Mvc/issues/4692

주로 주석에서 살펴 : https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462

요약 :

: 당신은 문제를 해결하기 위해 MVC에 Resource 필터를 만들 수 있습니다
public class CultureSettingResourceFilter : IResourceFilter, IOrderedFilter 
{ 
    public int Order 
    { 
     get 
     { 
      return int.MinValue; 
     } 
    } 

    public void OnResourceExecuted(ResourceExecutedContext context) 
    { 
     // By this time the response would already have been started, so do not try to modify the response 
    } 

    public void OnResourceExecuting(ResourceExecutingContext context) 
    { 
     var culture = httpContext.GetRouteValue("your-culture-key-name")?.ToString(); 
     // set your culture here 
     CultureInfo.CurrentCulture = culture; 
     CultureInfo.CurrentUICulture = culture; 
    } 
} 

services.AddMvc(o => 
{ 
    o.Filters.Add(new CultureSettingResourceFilter()); 
}); 
+0

정말 고마워요. OnResourceExecuting에서 일부 지역화를 하드 코딩하면 내 로컬라이제이션이 실제로 시작됩니다. 지금 내가 갖고있는 문제는 원하는 문화를 얻는 것입니다. http : // localhost : 5000/sl/otherStuff와 같은 url에서 문화권을 얻으려고합니다. OnResourceExecuting에서 가져올 수있는 방법이 있습니까? –

+1

필터를 사용하면 현재 요청의 경로 데이터에 액세스 할 수 있으므로'httpContext.GetRouteValue ("culture-key-name")'('Microsoft.AspNetCore.Routing' 네임 스페이스를 추가하여' GetRouteValue' 및'GetRouteData' 확장자 –

+0

많은 도움을 주셔서 감사 드리며 모든 것을 테스트 했으므로 작동하는 것으로 확인되었습니다. 감사합니다. –

1

나는 국제화 실험 나는 리소스 파일 이름의 문화와 일치 문화를 지원하는 경우 내 번역이

var supportedCultures = new List<CultureInfo>{ 
    new CultureInfo("en-US"), 
    new CultureInfo("sl-SI"), 
    new CultureInfo("de-DE"), 
    new CultureInfo("hr-HR") 
    }; 

변경 리소스 파일 이름

자료/뷰/컨트롤러 명에서 작동있어 /ViewName.sl.resx

특정 언어의 문화에

리소스/Views/ControllerName/ViewName.sl-SI.RESX

+0

시간과 응답에 감사드립니다. 그러나 이것은 내 문제를 해결하지 못했습니다. –

0

@Siim Haas에서 편집하십시오.
좋은 결과입니다. 내 경우
: 나는 LanguageViewLocationExpanderFormat.SubFolder ConfigureServices에서

(IServiceCollection 서비스) 구성에서

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

    services.AddMvc() 
     .AddViewLocalization(
      Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.SubFolder, 
      opts => { opts.ResourcesPath = "Resources"; } 
     ) 
     .AddDataAnnotationsLocalization(); 

    services.Configure<RequestLocalizationOptions>(opts => 
    { 
     var supportedCultures = new[] 
     { 
      new CultureInfo("en-AU"), 
      new CultureInfo("en-GB"), 
      new CultureInfo("en-US"), 
      new CultureInfo("en"), 
      new CultureInfo("zh-cn"), 
      new CultureInfo("zh"), 
     }; 
     opts.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US"); 
     opts.SupportedCultures = supportedCultures; 
     opts.SupportedUICultures = supportedCultures; 
    }); 
} 

(IApplicationBuilder 응용 프로그램, IHostingEnvironment의 ENV, ILoggerFactory loggerFactory)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    app.UseRequestLocalization(); 

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

내 리소스를 사용하여 stucts 좋아해 :

{귀하의 프로젝트}/리소스/뷰/{귀하의 컨트롤러} /ViewName.en-us.resx

관련 문제