【问题标题】:Localization not picking specific resource culture file for validation messsages in .net core本地化没有为 .net 核心中的验证消息选择特定的资源文化文件
【发布时间】:2020-04-24 11:30:56
【问题描述】:

我在 .net 核心中添加了验证消息的代码。每次为消息加载默认文件时。

我添加了如下资源文件 1. 资源.resx 2.Resource.de.resx

每次从第一个默认文件加载消息时,默认情况下我都将文化设置为“DE”。

ViewModel 
   [Required(ErrorMessageResourceName = "the_first_name_field_is_required", ErrorMessageResourceType = typeof(OneView.Library.Resources.Resource))]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required(ErrorMessageResourceName = "the_last_name_field_is_required", ErrorMessageResourceType = typeof(OneView.Library.Resources.Resource))]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

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

            services.AddMvc()
                        .AddViewLocalization(
            LanguageViewLocationExpanderFormat.Suffix,
            opts => { opts.ResourcesPath = "Resources"; })

        .AddDataAnnotationsLocalization();
       services.Configure<RequestLocalizationOptions>(
        opts =>
        {
            var supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("EN-US"),
                new CultureInfo("de"),
            };

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

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(options.Value);
        }

【问题讨论】:

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


    【解决方案1】:

    您将“de”设置为DefaultRequestCulture,但如果没有内置提供程序可以确定请求文化,则使用DefaultRequestCulture。在documentation 中,默认提供程序是:

    • QueryStringRequestCultureProvider
    • CookieRequestCultureProvider
    • AcceptLanguageHeaderRequestCultureProvider

    您可以覆盖RequestLocalizationOptionsRequestCultureProviders 列表并仅使用其他两个提供程序。在Startup.cs:

     public void ConfigureServices(IServiceCollection services)
     {
    
            services.Configure<RequestLocalizationOptions>(
            opts =>
            {
            var supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("EN-US"),
                new CultureInfo("de"),
            };
    
            opts.DefaultRequestCulture = new RequestCulture("de");
            //// Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;
            opts.RequestCultureProviders = new List<IRequestCultureProvider>
            {
                new QueryStringRequestCultureProvider(),
                new CookieRequestCultureProvider()
            };
           });
     }
    

    参考: https://stackoverflow.com/a/44496575/10201850

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多