Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

仓库 commits 的 “提交检查” 状态有太多组合,是否有更好的方式处理翻译? #111

Open
pecasha opened this issue Mar 12, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@pecasha
Copy link
Contributor

pecasha commented Mar 12, 2024

PixPin_2024-03-12_18-18-20

仓库的提交检查状态是根据检查出的所有状态按任务顺序组合,所以是没有特定的顺序,会出现所有状态的排列组合,目前已知的状态有9种:

expected
successful
skipped
failing
pending
cancelled
action required
queued
in progress

所有任务都只有一种相同状态的时候会有复数形式,例如:

1 successful check
5 successful checks

1 failing check
3 failing checks

当有两种及以上的不同状态时,格式会有变化:

2 skipped and 1 failing checks
2 cancelled, 1 failing, and 3 successful checks
3 failing, 2 successful, 1 pending, and 2 cancelled checks
...

如果按照现在的方式添加翻译,要覆盖所有的状态和所有的排列,需要添加500多个词条,这样感觉是不太合适的

@maboloshi
Copy link
Owner

事实上我放弃翻译 😂
需要各种正则规则 增加负担

@pecasha
Copy link
Contributor Author

pecasha commented Mar 12, 2024

事实上我放弃翻译 😂 需要各种正则规则 增加负担

我有一种方法可以很大程度的简化这类词条的数量,不知道合不合适,只需要对正则匹配结果进行翻译替换。

对于这类词条我们现在的正则替换方式是:

[/(\d+) failing, (\d+) successful, (\d+) pending, and (\d+) cancelled checks/, "$1 失败,$2 成果,$3 待定,$4 取消"]

但是因为他这个句式是有固定格式的,实际我们需要翻译的只有目前这9种状态的名称,那我们把状态名称也通过正则匹配出来,然后再翻译状态名称。
这样就只需要9条规则就可以翻译所有状态的排列组合:

const translate = {
    expected: "缺失",
    successful: "成功",
    skipped: "跳过",
    failing: "失败",
    pending: "待定",
    cancelled: "取消",
    queued: "等待",
    "action required": "需要操作",
    "in progress": "运行中"
}
const str = "3 failing, 2 successful, 1 pending, and 2 cancelled checks";

const result = str.replace(
    /(\d+) ([a-z ]+), (\d+) ([a-z ]+), (\d+) ([a-z ]+), and (\d+) ([a-z ]+) checks/,
    (_, $1, $2, $3, $4, $5, $6, $7, $8) => {
        return `${$1} ${translate[$2]}, ${$3} ${translate[$4]}, ${$5} ${translate[$6]}, ${$7} ${translate[$8]}`;
    }
);

console.log(result); // 3 失败, 2 成功, 1 待定, 2 取消

这样就可以翻译同时含有4种状态提示语的所有排列,所以只需要9条正则就覆盖1到9种状态的全部提示语。

@pecasha
Copy link
Contributor Author

pecasha commented Mar 12, 2024

优化了一下,只需要一条正则就可以匹配所有的状态提示语

const translate = {
    expected: "缺失",
    successful: "成功",
    skipped: "跳过",
    failing: "失败",
    pending: "待定",
    cancelled: "取消",
    queued: "等待",
    "action required": "需要操作",
    "in progress": "运行中"
}
const str = "3 failing, 2 successful, 1 pending, and 2 cancelled checks";

const result = [...str.matchAll(/(\d+) ([a-z ]+)(?=,| and| checks| check)/g)].map(([_, $1, $2]) => `${$1} ${translate[$2]}`).join(", ");

console.log(result); // 3 失败, 2 成功, 1 待定, 2 取消

@maboloshi maboloshi reopened this Jul 30, 2024
@TC999
Copy link
Contributor

TC999 commented Aug 9, 2024

可以参考时间正则翻译

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@maboloshi @pecasha @TC999 and others