我有以下结构:
/
|- main.py
|- brainz
| |- __init__.py
| |- Brainz.py
|- datas
|- locale
|- en_US
|- LC_MESSAGES
|- brainz.mo
|- brainz.po
在我的__init__.py
中有以下几行:
import locale
import gettext
import os
current_locale, encoding = locale.getdefaultlocale()
locale_path = '../datas/locale/' + current_locale + '/LC_MESSAGES/'
language = gettext.translation ( 'brainz', locale_path )
language.install()
但是当我尝试运行我的程序时,我收到了这个错误:
Traceback (most recent call last):
File "main.py", line 3, in <module>
from brainz.Brainz import *
File "/home/damien/BrainZ/brainz/__init__.py", line 11, in <module>
language = gettext.translation ( 'brainz', locale_path )
File "/usr/lib/python2.6/gettext.py", line 484, in translation
raise IOError(ENOENT, 'No translation file found for domain', domain)
IOError: [Errno 2] No translation file found for domain: 'brainz'
当我提供gettext.translation
文件的完整路径时,我不明白.mo
期望哪条路径。
有人可以解释一下我要正确加载翻译文件需要做什么吗?
谢谢,
达明
答案 0 :(得分:7)
我认为你的__init__.py
应该是这样的:
import locale
import gettext
import os
current_locale, encoding = locale.getdefaultlocale()
locale_path = 'datas/locale/'
language = gettext.translation ('brainz', locale_path, [current_locale] )
language.install()