ASP .NET Core 1.0 RTM 本地化不起作用 [英] ASP .NET Core 1.0 RTM Localization not working

查看:14
本文介绍了ASP .NET Core 1.0 RTM 本地化不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试按照 微软文档,我就是无法让它工作.我遇到的问题是,无论我如何尝试设置文化,它总是显示来自英语资源文件的字符串.

I've been trying to implement localization for my asp .NET Core 1.0 RTM web app following microsofts documentation and I just can not get it to work. The problem I'm having is that it's always displaying strings from the english resource file no matter how I try setting the culture.

如果有人有 5 分钟的时间,我将非常感谢您的意见.

If anyone has 5 minutes of their time, I'd be really grateful for your input.

这是我关于本地化的 Startup.cs 内容:

Here's my Startup.cs content that regards the localization:

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

这就是我在 ActionFilter 中改变 OnActionExecuting 内部文化的方式

And this is how I'm changing the culture inside OnActionExecuting in my ActionFilter

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;

我也尝试在我的控制器上执行此操作,但没有成功.

I've also tried doing this on my controller with no luck.

然后在我看来,我使用 @inject IViewLocalizer Localizer 来显示带有 @Localizer["Name"] 的本地化字符串.

Then in my views I'm using @inject IViewLocalizer Localizer to display localized strings with @Localizer["Name"].

我的资源位于文件夹 Resources/Views/ControllerName 中,如下所示:

My resoucres are located in the folder Resources/Views/ControllerName like so:

  • 资源/视图/ControllerName/ViewName.en.resx
  • 资源/视图/ControllerName/ViewName.sl.resx
  • ...

无论我如何尝试改变文化,显示的字符串总是来自 .en 资源文件.有什么我想念的吗?还有什么我应该做的吗?看来我遇到的问题是设置语言.根据文档,您应该使用 Response.Cookies.Append 设置 cookie,但我也尝试使用 CultureInfo.CurrentCulture 作为 Thread.CurrentThread.CurentCulture 不再可用.

The displayed string is always from the .en resource file no matter how I try to thange the culture. Is there something I'm missing? Is there something more I'm supposed to do? It seems the problem I'm having is with setting the language. Based on the documentation you're just supposed to set the cookie with Response.Cookies.Append, but I've also tried with CultureInfo.CurrentCulture as Thread.CurrentThread.CurentCulture is not awailable anymore.

我真的不知道我错过了什么.有什么想法吗?

I really don't know what I'm missing. Any ideas at all?

推荐答案

您可能遇到以下问题:https://github.com/aspnet/Mvc/issues/4692

主要看评论:https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462

总结:您可以在 MVC 中创建一个 Resource 过滤器来解决该问题:

Summary: You can create a Resource filter in MVC to workaround the issue:

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

这篇关于ASP .NET Core 1.0 RTM 本地化不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆