ASP .NET Core 1.0 RTM本地化无法正常工作

时间:2016-08-09 00:03:04

标签: asp.net-core asp.net-core-mvc asp.net-core-1.0 asp.net-core-localization

我一直试图在microsofts documentation之后为我的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);
}

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

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;

我也试过在我的控制器上做这件事而没有运气。

然后在我的观看中,我使用@inject IViewLocalizer Localizer显示@Localizer["Name"]的本地化字符串。

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

  • Resources / Views / ControllerName / ViewName.en.resx
  • 资源/查看/ ControllerName / ViewName.sl.resx
  • ...

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

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

3 个答案:

答案 0 :(得分:5)

您可能会遇到以下情况: 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());
});

答案 1 :(得分:1)

我尝试了i18n,当支持的文化与资源文件名中的文化匹配时,我的翻译工作

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

更改资源文件名
  

资源/查看/ ControllerName / ViewName.sl.resx

特定语言文化

  

资源/查看/ ControllerName / ViewName.sl-SI.resx

答案 2 :(得分:1)

编辑@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;
    });
}

在Configure(IApplicationBuilder应用程序,IHostingEnvironment环境,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}");
    });
}

我的资源就像:

  

{Your Project} / Resources / Views / {your controller} /ViewName.en-us.resx