【问题标题】:what cases in which ABP can automatically localize the enums if we follow enum localization conventions如果我们遵循枚举本地化约定,ABP 在哪些情况下可以自动本地化枚举
【发布时间】:2021-03-09 13:43:24
【问题描述】:

我尝试在 DTO ABP 本地化文档说中本地化枚举属性

我们更喜欢针对特定文本类型的一些约定; 添加菜单:菜单项的前缀。 使用 Enum:: 命名约定来本地化枚举成员。当你这样做时,ABP 可以在某些适当的情况下自动本地化枚举。

ABP Documentation

但是我遵循这个约定并且值没有本地化返回枚举值

我的问题:是否有自动本地化枚举的方法或 ABP 自动本地化枚举的情况

【问题讨论】:

标签: enums localization abp


【解决方案1】:

CarType.cs: (枚举)

public enum CarType
{
    Sedan,
    Hatchback,
    Coupe
}

en.json: (本地化文件)

{
  "culture": "en",
  "texts": {
    "Enum:CarType:0": "Sedan Car",
    "Enum:CarType:1": "Hatchback Car",
    "Enum:CarType:2": "Coupe Car"
  }
}
        

index.js: (javascript用法)

var l = abp.localization.getResource("MyProject");
var localizationKey = "Enum:CarType:" + changeType;
var localized = l(localizationKey);

index.cshtml: (Razor 页面使用情况)

 var statuses = (from CarType ct in Enum.GetValues(typeof(ArticleContentSource))
                select new {Id = (int)ct, Name=L[$"Enum:ContentSource:{(int)ct }"].Value })
                .ToList();

当在 MVC 项目中使用 ABP Tag Helpers 时,它会通过诸如“Enum:EnumName:Value": "localized text"”之类的约定自动本地化

CarCreateModel.cs: (模型)

 public class CarCreateModel
 {
     [Required]
     [InputInfoText("Choose car type?")]
     public CarType MyCarType { get; set; }
 }

index.cshtml: (MVC 页面)

 <abp-select asp-for="@Model.CarCreateModel.MyCarType"/>

【讨论】:

    【解决方案2】:

    我查看了实际代码,我认为文档至少部分不正确。

    这是框架的代码:

    $"Enum:{enumType.Name}.{memberName}",
    $"{enumType.Name}.{memberName}",
    memberName
    

    所以,我认为正确的json文件格式是这样的:

    {
      "culture": "en",
      "texts": {
        "Enum:CarType.Sedan": "Sedan Car",
        "Enum:CarType.Hatchback": "Hatchback Car",
        "Enum:CarType.Coupe": "Coupe Car"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多