Abp vNext DTO internationalization

I have learned about some official documents of Abp vNext about internationalization before, but I still don’t know how to use Istringlocalizer for internationalization in the DTO class, including the variable parameter passing of internationalization placeholders. Later, after searching for a long time in the project source code, I found that Many official projects also directly return to English, and finally found the following code later.

位置:abp-dev\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo\Abp\Application\Dtos

public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
      if (MaxResultCount > MaxMaxResultCount)
      {
           var localizer = validationContext.GetRequiredService<IStringLocalizer<AbpDddApplicationContractsResource>>();

           yield return new ValidationResult(
              localizer[
                  "MaxResultCountExceededExceptionMessage", 
                  nameof(MaxResultCount),
                  MaxMaxResultCount, 
                  typeof(LimitedResultRequestDto).FullName, 
                  nameof(MaxMaxResultCount)
              ],
           new[] { nameof(MaxResultCount) });
      }
}

As you can see, we can use validationContext.GetRequiredService<IStringLocalizer< xxxResource >>() to obtain the Istringlocalizer, and then use it in the returned message. The parameters behind the localizer can correspond to the placeholders in the internationalized message, such as in the above code

nameof(MaxResultCount),
MaxMaxResultCount, 
typeof(LimitedResultRequestDto).FullName, 
nameof(MaxMaxResultCount)

will replace the curly brace placeholder content in the internationalized message below, respectively.

"texts": {
    "MaxResultCountExceededExceptionMessage": "{0} can not be more than {1}! Increase {2}.{3} on the server side to allow more results."
}

おすすめ

転載: blog.csdn.net/csdn102347501/article/details/116524638