Skip to content

[Feature Request]: 自定义属性添加本地化,但是labelPrefix #89

Description

@otomad

Problem Statement | 需求背景

我在使用枚举时,除了枚举值本身的文本标签之外,还一般需要一个描述标签来描述这个值是做什么用的。这就需要两个字段需要翻译。

刚才看到了 #82 正好添加自定义字段本地化的功能,正好可以一起说一下。

Package Scope

enum-plus (core)

Feature Description | 功能描述

假设 #82 方案实施的话,那么我的情况大概就像这样:

const WeekEnum = Enum({
    Sunday: { value: 0, label: "week.sunday", description: "descriptions.week.sunday" },
    Monday: { value: 1, label: "week.monday", description: "descriptions.week.monday" },
    Tuesday: { value: 2, label: "week.tuesday", description: "descriptions.week.tuesday" },
}, { autoLocalizeMeta: ["description"] });

但是我还想用 labelPrefix,此时理论上可以简化为:

const WeekEnum = Enum({
    Sunday: 0,
    Monday: 1,
    Tuesday: 2,
}, {
    autoLocalizeMeta: ["description"],
    labelPrefix: "week",
    descriptionPrefix: "descriptions.week",
});

但是直接逐个写 labelPrefix 和 descriptionPrefix 肯定是不现实的,肯定需要某种变通方案,但是我还没有方案思路。

Use Cases | 使用场景

只要需翻译字段的键名是保持规律的,就可以有效减少本地化的代码量。

Importance | 重要性评估

No response

Feature Level | 特性级别

Core Library (核心库)

Proposed Solution (Optional) | 建议方案(可选)

其它信息

不过我还有个其它需求,就是将 #82 中提到的 autoLocalizeMeta 属性添加到 enum plus 全局属性中,因为只要字段名是指向性的,其必定在所有枚举中都是可翻译的。像这里的 description 字段只要每个枚举中出现了就一定是可翻译的。因此我可以直接将其加入全局属性中,没必要每个枚举都写一遍 autoLocalizeMeta 属性。

此外,由于我的所有描述翻译键名都是严格在 labelPrefix 前面加上“descriptions.”,因此理论上还可以再进一步简化,例如在全局配置上添加某种配置后(比如类似于 autoLabel 再来个 autoDescription 之类的),那么在每个枚举中也都可以不用配置 descriptionPrefix 直接使用了。不过这个想法太过于抽象了,如果你没有太好的思路的可以先不实现。

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions