如何将 AspNetCore 内置消息本地化为法语?
How can I localize AspNetCore built in messages in French?
使用 AspNetCore 1.1 预览 1 位我可以在 documentation 演示中找到如何本地化我自己的资源的所有示例,我想本地化内置消息,例如验证消息。
我的应用程序仅供法国人使用,因此我的默认区域性是 fr-FR。
如何将所有内置资源本地化为法语?
自 1.1 预览版 1 起,没有 API 可以将 所有 框架消息转换为不同的语言。
对于数据标注验证,您可以在代码中自定义错误信息。鉴于您只是希望支持法语,您可能会发现覆盖 ValidationAttribute.ErrorMessage
属性.
最简单
示例:
public class LoginViewModel
{
[EmailAddress(ErrorMessage = "(Translation for 'this email is invalid' here)")]
public string Email { get; set; }
}
您还可以使用 ValidationAttribute.ErrorMessageResourceName
和 ValidationAttribute.ErrorMessageResourceType
属性。有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/core/api/system.componentmodel.dataannotations.validationattribute
如果您想要更底层的控制,则需要覆盖框架本地化。 API 这样的 IStringLocalizer
存在。 documentation you linked to introduces some of those extensibility points. (Checkout https://aspnetsource.azurewebsites.net 如果您想检查框架代码如何调用这些 API。)
使用 AspNetCore 1.1 预览 1 位我可以在 documentation 演示中找到如何本地化我自己的资源的所有示例,我想本地化内置消息,例如验证消息。
我的应用程序仅供法国人使用,因此我的默认区域性是 fr-FR。
如何将所有内置资源本地化为法语?
自 1.1 预览版 1 起,没有 API 可以将 所有 框架消息转换为不同的语言。
对于数据标注验证,您可以在代码中自定义错误信息。鉴于您只是希望支持法语,您可能会发现覆盖 ValidationAttribute.ErrorMessage
属性.
示例:
public class LoginViewModel
{
[EmailAddress(ErrorMessage = "(Translation for 'this email is invalid' here)")]
public string Email { get; set; }
}
您还可以使用 ValidationAttribute.ErrorMessageResourceName
和 ValidationAttribute.ErrorMessageResourceType
属性。有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/core/api/system.componentmodel.dataannotations.validationattribute
如果您想要更底层的控制,则需要覆盖框架本地化。 API 这样的 IStringLocalizer
存在。 documentation you linked to introduces some of those extensibility points. (Checkout https://aspnetsource.azurewebsites.net 如果您想检查框架代码如何调用这些 API。)