[Bug] \ o_traditional()\ mistranslates 94 of 450 characters on a default install (zh-Hant)
English is recommended so global developers can help. 推荐使用英文提交,谢谢 ❤️
- I'm on the latest version and searched existing issues (incl. closed) — no duplicate.
Environment
Version: current master (commit 08f64f7)
OS: any — the table is data, not platform-specific
Python: 3.11 / 3.13 (CI matrix)
Install: default pip install -r requirements.txt — note that opencc is only in requirements-optional.txt
Language: zh-Hant (cow_lang: "zh-Hant", or any zh-TW / zh-HK locale)
What happened?
to_traditional() produces wrong output for 94 of the 450 characters in the fallback table — 37 of them turn into an unrelated character and 57 stay Simplified.
to_traditional() builds its character map with dict(zip(_SIMPLIFIED, _TRADITIONAL)), so the two strings are read positionally. They are not the same length:
len(_SIMPLIFIED) = 450
len(_TRADITIONAL) = 393_TRADITIONAL ends with the _PHRASE_MAP values appended to it (檔案影片圖片連結資料資訊支援排程執行帳號密碼憑證埠服務啟用管道終端機控制台). Those appended terms do nothing for the character map — it only ever looks up _SIMPLIFIED keys — but they push the last 37 real pairings onto those words. The breakdown:
| Range | Count | Result |
|---|---|---|
_SIMPLIFIED[0:356] |
356 | correct |
_SIMPLIFIED[356:393] |
37 | converted to an unrelated character |
_SIMPLIFIED[393:450] |
57 | not converted at all |
(zip stops at the shorter of the two, so the last 57 characters never get a partner.)
Steps to reproduce
>>> from common.i18n import to_traditional
>>> to_traditional("转")
'圖' # expected '轉' (轉 = turn/rotate, 圖 = picture)
>>> to_traditional("边")
'執' # expected '邊'
>>> to_traditional("钟")
'台' # expected '鐘'
>>> to_traditional("赋")
'檔' # expected '賦'
>>> to_traditional("问题很长,需要钥匙")
'问题很长,需要钥匙' # expected '問題很長,需要鑰匙' — entirely unchangedExpected: Traditional Chinese throughout, as zh-Hant promises.
Actual: mixed or wrong script. Because the mispaired characters are extremely common (转 轮 软 轻 载 输 边 过 运 还 这 进 远 连 选 邮 邻 采 释 里 鉴 针 钟 钥 长 闭 问 间 队 难 页 风 马 鱼 鸡 麦 齐 ...), this affects ordinary sentences rather than an edge case, and it is worst for exactly the words a Chinese-language UI leans on (问题, 时间, 转换, 邮箱, 密钥, 长对话, 关闭).
Why the default install hits this
to_traditional() prefers OpenCC, but opencc-python-reimplemented lives in requirements-optional.txt, not requirements.txt. On a default install the character table is the conversion path, and the docstring already describes it as the no-dependency fallback ("This function is designed to work without external dependencies"). So the fallback is not an edge case — it is the default for every zh-Hant user who did not opt into the extra.
Suggested fix
Rebuild _TRADITIONAL so it pairs 1:1 with _SIMPLIFIED (all 450 entries, padding dropped), and add a test that pins the invariant so the two tables cannot silently drift apart again:
len(_SIMPLIFIED) == len(_TRADITIONAL)- no entry is paired with itself (a self-pairing means the lists drifted)
- the previously mispaired and previously unmapped characters convert correctly
Logs
Not applicable — the bug is in static data, reproducible in a plain interpreter as shown above. No runtime logs involved.
Source: zhayujie/CowAgent