maboloshi / github-chinese

GitHub 汉化插件,GitHub 中文化界面。 (GitHub Translation To Chinese)
https://maboloshi.github.io/github-chinese/
GNU General Public License v3.0
5.69k stars 335 forks source link

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

Open pecasha opened 8 months ago

pecasha commented 8 months ago

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 commented 8 months ago

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

pecasha commented 8 months ago

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

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

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

[/(\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 commented 8 months ago

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

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 取消
TC999 commented 3 months ago

可以参考时间正则翻译