我有以下结构:

/
|- 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文件的完整路径。

有人可以解释一下如何正确加载我的翻译文件吗?

谢谢,

达米安

最佳答案

我认为您的__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()

关于Python : No translation file found for domain using custom locale folder,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3837683/

10-12 18:37