ASP.Net Core 本地化 [英] ASP.Net Core localization

查看:25
本文介绍了ASP.Net Core 本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net 核心功能对本地化的新支持..p>

在我的项目中,我只需要一种语言.对于大多数文本和注释,我可以用我的语言指定内容,但对于来自 ASP.Net Core 本身的文本,语言是英语.

例子:

  • 密码必须至少包含一个大写字母 ('A'-'Z').
  • 密码必须至少有一位数字('0'-'9').
  • 用户名x@x.com"已被占用.
  • E-post 字段不是有效的电子邮件地址.
  • 值 '' 无效.

我已尝试手动设置文化,但语言仍然是英语.

app.UseRequestLocalization(new RequestLocalizationOptions{DefaultRequestCulture = new RequestCulture("nb-NO"),SupportedCultures = 新列表;{ 新 CultureInfo("nb-NO") },SupportedUICultures = 新列表;{ 新 CultureInfo("nb-NO") }});

如何更改 ASP.Net Core 的语言,或覆盖其默认文本?

解决方案

列出的错误消息在 ASP.NET Core Identity 中定义并由 IdentityErrorDescriber.到目前为止我还没有找到翻译的资源文件,我认为它们没有本地化.在我的系统(德语语言环境)上,尽管 CultureInfo 设置正确,但它们也没有被翻译.

您可以配置自定义 IdentityErrorDescriber 以返回您自己的消息及其翻译.它被描述为例如在如何更改MVC的默认错误信息核心验证摘要?

Startup.csConfigure 方法中,您可以连接从 IdentityErrorDescriber 继承的错误描述器类,例如

 services.AddIdentity().AddErrorDescriber();

对于其他默认模型错误消息(如无效号码),您可以在 ModelBindingMessageProvider 中提供自己的访问器函数.此提供程序由 ASP.NET Core MVC 使用,也可以在 Startup.cs 中进行配置.

services.AddMvc(选项=>options.ModelBindingMessageProvider.ValueIsInvalidAccessor = s =>$"我对 {s} 的文本无效");

ASP.Net core features new support for localization.

In my project I need only one language. For most of the text and annotations I can specify things in my language, but for text coming from ASP.Net Core itself the language is English.

Examples:

  • Passwords must have at least one uppercase ('A'-'Z').
  • Passwords must have at least one digit ('0'-'9').
  • User name 'x@x.com' is already taken.
  • The E-post field is not a valid e-mail address.
  • The value '' is invalid.

I've tried setting the culture manually, but the language is still English.

app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("nb-NO"),
    SupportedCultures = new List<CultureInfo> { new CultureInfo("nb-NO") },
    SupportedUICultures = new List<CultureInfo> { new CultureInfo("nb-NO") }
});

How can I change the language of ASP.Net Core, or override its default text?

解决方案

The listed error messages are defined in ASP.NET Core Identity and provided by the IdentityErrorDescriber. I did not found translated resource files so far and I think they are not localized. On my system (German locale) they are not translated as well although the CultureInfo is set correctly.

You can configure a custom IdentityErrorDescriber to return your own messages as well as their translations. It is described e.g. in How to change default error messages of MVC Core ValidationSummary?

In the Configure method of the Startup.cs you can wire up your Error Describer class inherited from IdentityErrorDescriber like

 services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddErrorDescriber<TranslatedIdentityErrorDescriber>();

For other default model error messages (like invalid number) you can provide own accessor functions in the ModelBindingMessageProvider. This provider is used by ASP.NET Core MVC and can be configured in the Startup.cs as well.

services.AddMvc(
            options => 
            options.ModelBindingMessageProvider.ValueIsInvalidAccessor = s => $"My not valid text for {s}");

这篇关于ASP.Net Core 本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆