Files
tryton/trytond/i18n.py
T
Cédric Krier c2bcf9368a Return message id if pool does not have yet ir.message
When pool is initialized the ir.message may not yet be registered.

issue8631
review262061002
2019-09-02 18:21:41 +02:00

34 lines
1022 B
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.tools.string_ import LazyString
from trytond.transaction import Transaction
from trytond.pool import Pool
def gettext(message_id, *args, **variables):
"Returns the message translated into language"
if not Transaction().database:
return message_id
pool = Pool()
try:
Message = pool.get('ir.message')
except KeyError:
return message_id
if not args:
language = Transaction().language
else:
language, = args
try:
module, id_ = message_id.split('.')
except ValueError:
return message_id
try:
return Message.gettext(module, id_, language, **variables)
except KeyError:
return message_id
def lazy_gettext(message_id, *args, **variables):
"Like gettext but the string returned is lazy"
return LazyString(gettext, message_id, *args, **variables)