42 种语言的 gettext Plural-Forms,逐一对照 CLDR 进行核对
语言
gettext 复数条目将各形式保存在编号槽位中,如 msgstr[0]、msgstr[1] 等,而文件头部的 Plural-Forms 行是一个小型 C 语言公式,用于为某个数字选择槽位。对于 Polyglot 1.4.0,我们需要为其翻译的 42 种语言都提供该公式,并且每个槽位必须保存 CLDR(Android、ICU 和我们自身检查所依据的复数数据)为相同数字提供的形式。以下是该表格,以及我们的核对方法。
每种语言的复数形式中提供了所有 CLDR 定义了复数规则的语言的标题,以及用于检查您自定义规则的校验器。
我们的核对方法
- 0 到 10,000 的每个整数:由 Python 自身的 gettext 评估每个公式,其选定的槽位必须保存 Babel 2.18 的 CLDR 数据为该数字提供的形式。41 种语言匹配;海地克里奥尔语是唯一的例外,且是有意为之(见下文)。
- 对 42 种语言中每种语言的单个文件运行 GNU msgfmt --check,每个槽位均包含一种形式:无错误。
- 实际运行:将示例应用的 gettext 文件由我们的本地模型翻译成九种语言,每个文件均编译并使用 Python 的 gettext 读回。俄语显示“Пропущена 21 привычка”但“Пропущено 22 привычки”,罗马尼亚语显示“21 de obiceiuri”但“101 obiceiuri”,阿拉伯语的六种形式均位于正确位置。
旧数据存在分歧之处
我们首先对照 Babel 2.10 进行核对,该版本包含 2022 年的 CLDR 41。三种语言的结果不同,而在每种情况下,较新的数据和语言本身都与表格一致:
- 罗马尼亚语:CLDR 41 将 101 与 20 和 100 归为一类,这些数字使用“de”(“100 de zile”);当前 CLDR 将其与 2 到 19 归为一类,这些数字不使用“de”。我们的模型写为“101 obiceiuri ratate”,未使用“de”。
- 希伯来语:CLDR 41 仍为 20、30、40 等保留“many”形式。后续 CLDR 版本已移除该形式,因此希伯来语有三个槽位:one、two 和 other。
- 海地克里奥尔语完全未包含在该数据中。当前 CLDR 为其提供两种形式,0 和 1 使用 one,其余使用 other,但海地克里奥尔语的名词不随数变化(“1 liv”、“2 liv”),因此我们保留单个槽位,正如 Polyglot 在其其他格式中所做的那样。
槽位内容
- 俄语、乌克兰语和波兰语有三个槽位,第三个槽位是 CLDR 的“many”(0、5 到 20、25……),而非“other”:“other”仅用于小数。波兰语的“one”仅为 1;21 属于“many”。
- 捷克语和斯洛伐克语:其“many”形式仅用于分数,因此第三个槽位为“other”。
- 西班牙语、法语、意大利语和葡萄牙语对精确的百万数有“many”形式;gettext 的两个槽位无法区分,因此共用“other”。
- 法语、葡萄牙语、波斯语、亚美尼亚语、阿姆哈拉语、卡纳达语和旁遮普语对 0 也使用“one”,因此
plural=(n > 1)。
表格
| 语言 | gettext | 复数形式 | msgstr[0], [1]… |
|---|---|---|---|
| 阿姆哈拉语 | am | nplurals=2; plural=(n > 1); | one, other |
| 阿拉伯语 | ar | nplurals=6; plural=(n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((n % 100 >= 3 && n % 100 <= 10) ? 3 : ((n % 100 >= 11) ? 4 : 5)))); | zero, one, two, few, many, other |
| 亚美尼亚语 | hy | nplurals=2; plural=(n > 1); | one, other |
| 保加利亚语 | bg | nplurals=2; plural=(n != 1); | one, other |
| 简体中文 | zh_CN | nplurals=1; plural=0; | other |
| 繁体中文 | zh_TW | nplurals=1; plural=0; | other |
| 捷克语 | cs | nplurals=3; plural=(n == 1) ? 0 : ((n >= 2 && n <= 4) ? 1 : 2); | one, few, other |
| 荷兰语 | nl | nplurals=2; plural=(n != 1); | one, other |
| 菲律宾语 | fil | nplurals=2; plural=(n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)); | one, other |
| 芬兰语 | fi | nplurals=2; plural=(n != 1); | one, other |
| 法语 | fr | nplurals=2; plural=(n > 1); | one, other |
| 德语 | de | nplurals=2; plural=(n != 1); | one, other |
| 希腊语 | el | nplurals=2; plural=(n != 1); | one, other |
| 海地克里奥尔语 | ht | nplurals=1; plural=0; | other |
| 希伯来语 | he | nplurals=3; plural=(n == 1) ? 0 : ((n == 2) ? 1 : 2); | one, two, other |
| 匈牙利语 | hu | nplurals=2; plural=(n != 1); | one, other |
| 印尼语 | id | nplurals=1; plural=0; | other |
| 意大利语 | it | nplurals=2; plural=(n != 1); | one, other |
| 日语 | ja | nplurals=1; plural=0; | other |
| 卡纳达语 | kn | nplurals=2; plural=(n > 1); | one, other |
| 高棉语 | km | nplurals=1; plural=0; | other |
| 韩语 | ko | nplurals=1; plural=0; | other |
| 马其顿语 | mk | nplurals=2; plural=(n % 10 == 1 && n % 100 != 11) ? 0 : 1; | one, other |
| 马来语 | ms | nplurals=1; plural=0; | other |
| 挪威博克马尔语 | nb | nplurals=2; plural=(n != 1); | one, other |
| 奥罗莫语 | om | nplurals=2; plural=(n != 1); | one, other |
| 波斯语 | fa | nplurals=2; plural=(n > 1); | one, other |
| 波兰语 | pl | nplurals=3; plural=(n == 1) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : 2); | one, few, many |
| 葡萄牙语(巴西) | pt_BR | nplurals=2; plural=(n > 1); | one, other |
| 旁遮普语 | pa | nplurals=2; plural=(n > 1); | one, other |
| 罗马尼亚语 | ro | nplurals=3; plural=(n == 1) ? 0 : ((n == 0 || n % 100 >= 1 && n % 100 <= 19) ? 1 : 2); | one, few, other |
| 俄语 | ru | nplurals=3; plural=(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : 2); | one, few, many |
| 苏格兰盖尔语 | gd | nplurals=4; plural=(n == 1 || n == 11) ? 0 : ((n == 2 || n == 12) ? 1 : ((n > 2 && n < 20) ? 2 : 3)); | one, two, few, other |
| 塞尔维亚语(西里尔字母) | sr | nplurals=3; plural=(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : 2); | one, few, other |
| 斯洛伐克语 | sk | nplurals=3; plural=(n == 1) ? 0 : ((n >= 2 && n <= 4) ? 1 : 2); | one, few, other |
| 斯洛文尼亚语 | sl | nplurals=4; plural=(n % 100 == 1) ? 0 : ((n % 100 == 2) ? 1 : ((n % 100 == 3 || n % 100 == 4) ? 2 : 3)); | one, two, few, other |
| 西班牙语 | es | nplurals=2; plural=(n != 1); | one、other |
| 瑞典语 | sv | nplurals=2; plural=(n != 1); | one、other |
| 泰语 | th | nplurals=1; plural=0; | other |
| 土耳其语 | tr | nplurals=2; plural=(n != 1); | one、other |
| 乌克兰语 | uk | nplurals=3; plural=(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : 2); | one、few、many |
| 越南语 | vi | nplurals=1; plural=0; | other |
实际运行发现的问题
阿拉伯语的零数和双数形式起初因未包含 %(n)s 而被拒绝。表示单一数字的形式可以省略该占位符,且阿拉伯语的双数形式本身已表达“两个”的含义,但我们的检查机制此前仅识别 %d 和 {count} 作为计数占位符。现在它也识别 Python 的 %(n)s 和 %(count)d,这意味着俄语中未包含数字的“one”形式也会被拒绝,因为俄语的“one”同样适用于 21 和 31。在同一次运行中,我们的模型读取了其翻译内容以检查含义,并发现捷克语的“Unsubscribe”被误译为“log out”。
代码位置
Polyglot 1.4.0 会将这些头部信息写入其生成的每个 .po 文件,并将每种复数形式放入对应的位置。为何用于多个数字的复数形式必须显示数字,请参见早前的说明;检查机制本身可在 translation-checks 中免费获取,自 1.3.0 版本起,它还能读取您已有的 .po 文件,并指出其 Plural-Forms 将 CLDR 规定为不同复数形式的数字归入同一位置的情况。