Python : No translation file found for domain usin

2019-02-17 02:52发布

问题:

I have the following structure :

/
|- main.py
|- brainz
|    |- __init__.py
|    |- Brainz.py
|- datas
     |- locale
          |- en_US
               |- LC_MESSAGES
                    |- brainz.mo
                    |- brainz.po

In my __init__.py there is the following lines :

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'

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 ?

Thanks,

Damien

回答1:

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()