.Net核心本地化,我做错了什么?

时间:2017-07-06 14:44:47

标签: .net asp.net-core localization

我正在玩.Net核心和本地化。我已经关注了几篇文章,主要是https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization,但似乎无法从我的资源文件中获取数据。

Startup.ConfigureServices

   services.AddLocalization(opts => opts.ResourcesPath = "Resources");

        services.Configure<RequestLocalizationOptions>(
            opts =>
            {
                var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-GB"),
                };

                opts.DefaultRequestCulture = new RequestCulture("en-GB");
                // Formatting numbers, dates, etc.
                opts.SupportedCultures = supportedCultures;
                // UI strings that we have localized.
                opts.SupportedUICultures = supportedCultures;
            });

        services
            .AddMvc()
            .AddJsonOptions(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); })
            .AddViewLocalization(
                LanguageViewLocationExpanderFormat.Suffix,
                opts => opts.ResourcesPath = "Resources")
            .AddDataAnnotationsLocalization()
            .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

我希望本地化的ViewModel

[Validator(typeof(LoginViewModelValidator))]
public class Login
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class LoginViewModelValidator : AbstractValidator<Login>
{
    public LoginViewModelValidator(IStringLocalizer<LoginViewModelValidator> localizer)
    {
        RuleFor(login => login.Email).NotEmpty()
            .WithMessage(x => localizer["Email"])
            .EmailAddress()
            .WithMessage(x => localizer["Email address must be valid"]);
        RuleFor(reg => reg.Password).NotEmpty().WithMessage(x => localizer["Password required"]);
    }
}

资源文件的位置

Location of Resource file

资源文件内容

Resource file contents

据我所知,本地化程序应该能够找到本地化的字符串,因为我目前的文化是en-GB。

有谁知道我做错了什么。

我也很困惑,因为msdn文章解释了

  

如果未找到“关于标题”的本地化值,则为索引器   返回key,即字符串“About Title”。

但是如何使用“关于标题”作为名称进行查找,因为它有空格并且无法输入.resx名称?

由于

修改

我已从IServicesCollection中删除了文化配置,并将其添加到middlewrae管道中:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

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

            var supportedCultures = new[]
            {
                new CultureInfo("en-GB"),
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-GB"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

            app.UseStaticFiles();

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

我也确定资源的命名是正确的。 IE Full命名空间,减去程序集名称,然后是classname,然后是culture。

ViewModels.LoginViewModelValidator.en-GB.resx,现在一切正常。感谢大家的评论

1 个答案:

答案 0 :(得分:0)

我认为你在ConfigureServices中也需要这个

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

您是否在Configure中连接了RequestLocalization中间件?

var locOptions = 
app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);

我不确定您在Resources文件夹下的子文件夹我在我的项目中没有这样做,它对我有用。