Base solution for your next web application
Open Closed

Localized Text in Models #1701


User avatar
0
djfuzzy created

Hi, I was wondering if you can steer me in the right direction towards being able to lookup localized texts in models, whether its an entity model, DTO or view model. The ABP documentation mentions that we should be doing validation at the domain/entity level, but I can't seem to figure out how to lookup a localized text instead of using a hard-coded string. Thanks in advance!


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

    Hi,

    How you are trying to do it? Via Display or DisplayName property? If so, see this discussion: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/582#issuecomment-245841609">https://github.com/aspnetboilerplate/as ... -245841609</a> BTW, I assume that you have read localization documentation: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Localization#DocInServer">http://www.aspnetboilerplate.com/Pages/ ... ocInServer</a>

  • User Avatar
    0
    djfuzzy created

    Hi, yes I have read the documentation but there doesn't seem to be anything in there about the use of localization in models. Here is an example model where I am trying to do this:

    public class CreateUserInput : ICustomValidate
        {
            [Required]
            [StringLength(User.MaxNicknameLength, MinimumLength = User.MinNicknameLength)]
            public string Nickname { get; set; }
            
            [Required]
            [EmailAddress]
            [StringLength(AbpUserBase.MaxEmailAddressLength)]
            public string EmailAddress { get; set; }
    
            [Required]
            [DisableAuditing]
            public string PasswordHashed { get; set; }
    
            public bool IsActive { get; set; }
    
            public void AddValidationErrors(List<ValidationResult> results)
            {
                if (Regex.IsMatch(Nickname, User.NicknameFormatValidationRegex))
                {
                    throw new UserFriendlyException(string.Format("Nickname must be between {0} and {1} characters, may " +
                        "only contain letters, numbers, spaces, hyphens, underscores and periods, and cannot begin or end " +
                        "with spaces.", User.MinNicknameLength, User.MaxNicknameLength));
                }
            }
    

    I would like the UserFriendlyException content in the AddValidation method to be something like:

    L("NicknameInvalidFormat", User.MinNicknameLength, User.MaxNicknameLength)
    
  • User Avatar
    0
    hikalkan created
    Support Team

    OK, understood now.

    That was not possible before. But with the release of v0.13 (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/releases/tag/v0.13.0.0">https://github.com/aspnetboilerplate/as ... /v0.13.0.0</a>) we now can resolve dependencies inside custom validation code (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1381">https://github.com/aspnetboilerplate/as ... ssues/1381</a>). So, you can resolve ILocalizationManager and use localization.

  • User Avatar
    0
    djfuzzy created

    OK, I got it working! Sorry for the late reply. Thanks for your help.