Localization of RequiredAttribute in ASP.NET Core 2.0

前端 未结 4 600
一生所求
一生所求 2021-01-02 03:37

I\'m struggling with localization in my new .NET Core project. I have 2 projects:

  • DataAccess project with Models and DataAnnotations (e.g. RequiredAttribute)
4条回答
  •  礼貌的吻别
    2021-01-02 04:10

    unfortunately, it is not that simple to localize all error messages for data attributes in one single place! because there are different types of error messages,

    Error messages for standard data attributes:

    [Required]
    [Range]
    [StringLength]
    [Compare]
    ...etc.
    

    Error messages for ModelBinding:

    ValueIsInvalid
    ValueMustNotBeNull
    PropertyValueMustBeANumber
    ...etc.
    

    and Identity error messages:

    DuplicateEmail
    DuplicateRoleName
    InvalidUserName
    PasswordRequiresLower
    PasswordRequiresUpper
    ...etc
    

    each must be configured in the startup file. Additionaly client side validation must be considered as well.

    you may check these articles for more details, it contains live demo and sample project on GitHub:

    Developing multicultural web application: http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application

    Localizing data annotations: http://www.ziyad.info/en/articles/16-Localizing_DataAnnotations

    Localizing ModelBinding error messages: http://www.ziyad.info/en/articles/18-Localizing_ModelBinding_Error_Messages

    Localizing identity error messages: http://www.ziyad.info/en/articles/20-Localizing_Identity_Error_Messages

    and client side validation: http://ziyad.info/en/articles/19-Configuring_Client_Side_Validation

    hope it helps :)

提交回复
热议问题