I've got an application that runs in several processes (one webserver and few processes that are used for heavy calculations). The goal is to make these calculation processes return localized errors. In order to do that, I've 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 to launch the application, I get
TypeError: No object (name: translator) has been registered for this thread
What is the right way to load the translator object?
Thanks in advance, Ivan.
I would recommend you translate in the main server thread, but you can register/use a translator object like so:
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)
If you want to make use of the pylons environment a bit more, you can do something like this:
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)
After that, _
will work as in the main thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With