Python:使用自定义语言环境文件夹找不到域的翻译文件 [英] Python : No translation file found for domain using custom locale folder
本文介绍了Python:使用自定义语言环境文件夹找不到域的翻译文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下结构:
/
|- 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()
但是当我尝试运行程序时,出现此错误:
But when I try to run my program I got this error :
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'
由于我提供了.mo
文件的完整路径,因此我不理解gettext.translation
需要哪个路径.
I don't understand which path is expected by gettext.translation
as I give a complete path to the .mo
file.
有人可以向我解释如何正确加载翻译文件吗?
Could someone explain me what I have to do to load my translation files correctly ?
谢谢
达明(Damien)
推荐答案
我认为您的__init__.py
应该是这样的:
I think your __init__.py
should be something like:
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()
这篇关于Python:使用自定义语言环境文件夹找不到域的翻译文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文