How to register a Pylons broadcast object?

I have an application that runs in several processes (one web server and several processes that are used for heavy computing). The goal is to make these computation processes return localized errors. For this, I made a dictionary that will be used by Babel:

errors = {
    'ERR_REQUEST_FORMAT': (1, _('ERR_REQUEST_FORMAT')),
    'ERR_REQUEST_TYPE': (2, _('ERR_REQUEST_TYPE')),
}

But when I try to run the application, I get

TypeError: No object (name: translator) has been registered for this thread

What is the correct way to load a translator object?

Thank you Ivan.

+5
source share
1 answer

I would recommend you translate to the main server thread, but you can register / use the translator object as follows:

import gettext
str_to_translate = u'String to Translate'
DOMAIN = 'example' # name of your translation babel translation file, here would be example.po
LOCALE_DIR = '/path/to/locale/dir' # directory containing language subdirectories
LANGUAGES = ['es']
CODESET = 'utf8'
translator = gettext.translation(DOMAIN, localedir=LOCALE_DIR, languages=LANGUAGES, codeset=CODESET)
translated_str = translator.gettext(str_to_translate)

pylons , - :

from pylons import config
from pylons.i18n.translation import set_lang
conf = config.current_conf()
if not conf['pylons.paths']['root']:
    conf['pylons.paths']['root'] = os.path.abspath(NAME_OF_YOUR_PROJECT)
if not conf.get('pylons.package'):
    conf['pylons.package'] = 'example' # same as domain above
set_lang(LANG, pylons_config=conf)

_ .

+1

All Articles