42 種語言的 gettext Plural-Forms,逐一核對數字與 CLDR 資料
語言
gettext 的複數條目將各變體存放在編號槽位中,例如 msgstr[0]、msgstr[1] 依此類推,而檔案標頭中的 Plural-Forms 行是一個小型 C 語言公式,用於為某個數字選擇對應的槽位。對於 Polyglot 1.4.0,我們需要為它所翻譯的 42 種語言都取得該公式,且每個槽位必須存放 CLDR 為相同數字所提供的變體;CLDR 是 Android、ICU 以及我們自身檢查背後的複數資料來源。以下是該表格,以及我們的檢查方式。
每個 CLDR 有複數規則的語言的標題,以及您自己的檢查器,都在每個語言的 Plural-Forms。
我們的檢查方式
- 0 到 10,000 的每個整數:由 Python 內建的 gettext 計算每個公式,其選取的槽位必須存放 Babel 2.18 的 CLDR 資料為該數字提供的變體。41 種語言相符;海地克里奧爾語是唯一例外,且是刻意為之(詳見下文)。
- GNU msgfmt --check:針對 42 種語言各一份檔案進行檢查,每個槽位均填入變體:無錯誤。
- 實際執行:將範例應用程式的 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,另一種用於其餘數字,但海地克里奧爾語的名詞不隨數字變化(「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」被誤譯為「登出」。
存放位置
Polyglot 1.4.0 會將這些標頭寫入其產生的每個 .po 檔案,並將每個形式放入對應的欄位。為何用於多個數字的單一形式必須顯示數字,請見先前的說明;檢查機制本身免費提供於 translation-checks,自 1.3.0 版起,它也能讀取您現有的 .po 檔案,並指出其 Plural-Forms 是否將 CLDR 規定應有不同形式的數字放入同一欄位。