Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> 更多Linux

從 GB2312 到 Unicode:打造 Plone 的 GB2312 編碼支援

問題背景

在 Zope 上安裝 Plone 時,若使用 UTF-8 中文方案,瀏覽器檢視原始碼容易出現編碼混亂。若偏好 GB2312,就需要讓 Python 與 Plone 能正確處理 GB2312 編碼。

Python 缺少 GB2312 轉碼器

預設 Python 的 encodings 目錄可能沒有 GB2312 支援,執行 decode 時會出現 unknown encoding。解決方式是建立一個 gb2312.py 轉碼器,放到 Python 的 lib/encodings 目錄,讓 Python 能註冊並使用它。

轉碼器原理

轉碼器主要實作 encode 與 decode。encode 把 Unicode 轉成 GB2312,decode 把 GB2312 轉回 Unicode。實作時可建立編碼與解碼對照表,並在 ASCII 小於 0x80 時按單一字元處理,其餘則依雙位元組中文處理。

在 Plone 中套用

將 gb2312.py 複製到 Zope 的 encodings 目錄並重啟 Zope。接著在 Localizer 的 messagecatalog 中建立語言 Chinese/China,匯入改為 GB2312 的 po 檔,再把 Plone 語言設為 GB2312,即可正常顯示中文。

Copyright © Linux教程網 All Rights Reserved