gpt4 book ai didi

.net - .NET 4.6.2 中的多语言数据注释

转载 作者:行者123 更新时间:2023-12-02 06:04:37 28 4
gpt4 key购买 nike

我有以下 View 模型:

public class LoginViewModel
{
[DisplayName("Email Address")]
[Required(ErrorMessage = "PleaseEnterYourEmail")]
public string EmailAddress { get; set; }

}

我有以下资源文件,名为:DataAnnotation.Localization.de-DE.resx,位于 App_LocalResources 文件夹内

App_LocalResources

Resource file

具有以下属性:

Properties

现在根据blog post announcing.net 4.6.2这应该可以正常工作,因为我应该将消息的本地化版本返回到 View 。

但是它只是显示:

View

我检查了我当前的文化,并将其设置为:de-DE,以便应用程序了解它需要显示的语言。 Target 框架也是 4.6.2。

我在这里缺少什么吗?

最佳答案

新的 DataAnnotations 本地化功能,如 blog post announcing .NET Framework 4.6.2 中所述开箱即用仅适用于 ASP.NET WebForms

Web 表单...

.NET Framework 4.6.2 中的新本地化功能在 System.Web.ModelBinding.DataAnnotationsModelValidator 类中实现,该类使用 StringLocalizerProviders.DataAnnotationStringLocalizerProvider.GetLocalizedString 来解析本地化字符串。

System.Web.Mvc.DataAnnotationsModelValidator 默认设置为 System.Web.Globalization.ResourceFileStringLocalizerProvider。此提供程序尝试在指定网站的Temporary ASP.NET Files文件夹中查找“App_LocalResources.root”dll。

但是,需要预先检查是否应该使用该功能:

private bool UseStringLocalizerProvider {
get {
// if developer already uses existing localization feature,
// then we don't opt in the new localization feature.
return (!string.IsNullOrEmpty(Attribute.ErrorMessage) &&
string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
Attribute.ErrorMessageResourceType == null);
}
}

上述检查意味着新的本地化功能仅在以下情况下才起作用:

[Required(ErrorMessage = "FirstName is required")]
public string FirstName { get; set; }

其中设置了ErrorMessage值。对于最常见的用例:

[Required]
public string FirstName { get; set; }

它会退回到旧名称解析。

在 MVC 5 中功能不起作用,因为...

MVC 5 使用它自己的特定实现System.Web.Mvc.DataAnnotationsModelValidator。此实现源自 Microsoft.AspNet.Mvc 版本 5.x.x,早于 .NET Framework 4.6.2。它没有实现新的本地化功能。

最重要的是,MVC 和 WebForms 的 ASP.NET 动态编译输出不同,因此 WebForms 使用的编译资源(例如:de\App_LocalResources.root.q_wjw-ce.resources.dll” )对于 ASP.NET MVC 应用程序的 App_LocalResources 甚至不存在。

MVC 和 WebForms 之间编译输出的这种差异排除了围绕 WebForms 实现编写包装器并在 MVC 应用程序中“按原样”使用它的可能性。

MVC 6 可以工作,但略有不同......

MVC 6 使用第三种实现,Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.DataAnnotationsModelValidator。此实现接受 IStringLocalizer stringLocalizer 作为构造函数参数。

可以在 Startup.cs 中添加默认本地化配置:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddDataAnnotationsLocalization();
}

并且需要在 Startup.cs Configure(...) 方法中进行请求本地化,例如:

// Configure the localization options
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(new CultureInfo("de-AT")),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("de")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("de-AT")
}
});

如果我们创建 ViewModel:

using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Models
{
public class User
{
[Required(ErrorMessage = "First name is required.")]
public string FirstName { get; set; }
}
}

我们必须在 WebApplication 的根目录中添加 Models.User.{culture}.resx 文件,其中包含“First name is required”键。和本地化的验证错误消息。

Model.User.resx file.

尽管 MVC 6 有不同的实现,但 ValidationAttribute 的条件与 WebForms 中相同。必须定义 ErrorMessage,而不应使用 ErrorMessageResourceNameErrorMessageResourceType

关于.net - .NET 4.6.2 中的多语言数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977353/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com