Base solution for your next web application
Open Closed

How to use localization in DTO? #971


User avatar
0
tonid created

For example I have this DTO:

public class SampleDTO
{
 [Display("SomeValue")]
  public string SomeValue { get; set; }
}

How to achieve localization in display attribute? I know display attribute has resource parameter, but how to connect it in ABP implementation?


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    If you want to use like that, you can use Resource file as localization source. Add a new resource file and use it as you always do with ASP.NET MVC. ABP has no such a localization property currently.

  • User Avatar
    0
    easyest created

    In my test project, that was based on Abp 0.6.3 I used custom attributes for that (please check attached file).

    Few notes on those:

    1. This was done before localization was moved to Core project, so localization source name was set using configuration, now You should be able to get it directly.
    2. There are Display and DisplayName attributes - Display attribute cannot be extended, so You will need to use DisplayName attribute.
    3. For localized validation attributes to work You will need to register them in You Global.asax:
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter));
                DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedStringLengthAttribute), typeof(StringLengthAttributeAdapter));
    
    1. LocalizedDisplayName attribute can be used on enums also, original DisplayName attribute can not.

    Hope that helps :) Attributes.zip

  • User Avatar
    0
    tonid created

    @hikalkan Thanks for your suggestion

    @easyest Cool, I will try your attribute file and thanks for your help :)