【问题标题】:Localization Model DataAnnotation in ASP.NET Core 6 MVCASP.NET Core 6 MVC 中的本地化模型 DataAnnotation
【发布时间】:2022-07-21 13:05:50
【问题描述】:

我有一个在 ASP.NET Core 6 MVC 上运行的多语言网站。

数据标注应基于用户语言;我可以使用sharedResource 类使网站成为双语。

问题是如何使模型数据标注错误双语;目前,我只得到了数据注解ErrorMessage

程序.cs

builder.Services.AddControllersWithViews()
             .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
              //.AddDataAnnotationsLocalization();// <--- for ERROR MSG -----
              .AddDataAnnotationsLocalization(
                 options => {
                     options.DataAnnotationLocalizerProvider = (type, factory) =>
                         factory.Create(typeof(DataAnnotationResource));
                 });// <---------- For ERROR MSG -----

工厂数据模型

public class FactoryData
{
    [Required(ErrorMessage = "General.RequiresMessageOOO")]
    public string NameInAr { get; set; }

    [Required(ErrorMessage = "General.RequiresMessageOOO")]
    [MaxLength(2, ErrorMessage = "General.MaxlengthExceededOOO")]
    public string NameInEn { get; set; }

    [Required]
    [Range(1,3)]
    public string Age { get; set; }
}

这是localizationResource 文件夹:

当前代码的输出

【问题讨论】:

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


    【解决方案1】:

    您可以通过这种方式在 DataAnnotations 中使用资源:

    [Required(ErrorMessageResourceName = "General.RequiresMessageOOO", ErrorMessageResourceType = typeof(SharedResource))]
    [MaxLength(2, ErrorMessageResourceName = "General.MaxlengthExceededOOO", ErrorMessageResourceType = typeof(SharedResource))]
    public string NameInEn { get; set; }
    

    在您的SharedResource.resx 文件中:

    <data name="General.RequiresMessageOOO" xml:space="preserve">
        <value>This field must not be empty</value>
    </data>
    <data name="General.MaxlengthExceededOOO" xml:space="preserve">
        <value>The value exceeded the max lenght</value>
    </data>
    

    在我的应用程序中,每种可用语言都有多个 SharedResource 文件:

    您可以在此处的 Microsoft 文档中获取有关本地化的更多详细信息: Globalization and localization in ASP.NET Core

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我必须进行以下更改

      来自

      builder.Services.AddControllersWithViews()
          .AddDataAnnotationsLocalization(opt =>
          {
              opt.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
          });
      

      builder.Services.AddControllersWithViews()
          .AddDataAnnotationsLocalization(opt =>
          {
              var type = typeof(SharedResource);
              var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
              var factory = builder.Services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
              var localizer = factory.Create("SharedResource", assemblyName.Name);
              opt.DataAnnotationLocalizerProvider = (t, f) => localizer;
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2022-01-21
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多