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

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

问题描述

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

ASP.Net core features new support for localization.

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

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.

示例:

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

我尝试过手动设置区域性,但是语言仍然是英语.

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") }
});

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

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

推荐答案

列出的错误消息在ASP.NET Core Identity中定义,并由

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.

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

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?

Startup.csConfigure方法中,您可以连接从IdentityErrorDescriber继承的Error Describer类,如

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>();

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

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天全站免登陆