[FIX] tools.translate: use `request` to find `cr` and `lang` when other methods fail

Do it only after other direct methods fail,
to avoid ignoring a closer environement coming
from a calling frame.

+ Sign CLA

Closes #5148
This commit is contained in:
Kiril Vangelovski 2015-02-09 10:03:34 +01:00 committed by Olivier Dony
parent a8b88e4dd4
commit ea60fed97a
2 changed files with 25 additions and 2 deletions

View File

@ -0,0 +1,11 @@
R. Macedonia, 2015-02-08
I hereby agree to the terms of the Odoo Individual Contributor License
Agreement v1.0.
I declare that I am authorized and able to make this agreement and sign this
declaration.
Signed,
Kiril Vangelovski kiril@hacbee.com https://github.com/kirca

View File

@ -169,7 +169,8 @@ class GettextAlias(object):
return sql_db.db_connect(db_name)
def _get_cr(self, frame, allow_create=True):
# try, in order: cr, cursor, self.env.cr, self.cr
# try, in order: cr, cursor, self.env.cr, self.cr,
# request.env.cr
if 'cr' in frame.f_locals:
return frame.f_locals['cr'], False
if 'cursor' in frame.f_locals:
@ -179,6 +180,11 @@ class GettextAlias(object):
return s.env.cr, False
if hasattr(s, 'cr'):
return s.cr, False
try:
from openerp.http import request
return request.env.cr, False
except RuntimeError:
pass
if allow_create:
# create a new cursor
db = self._get_db()
@ -197,7 +203,7 @@ class GettextAlias(object):
def _get_lang(self, frame):
# try, in order: context.get('lang'), kwargs['context'].get('lang'),
# self.env.lang, self.localcontext.get('lang')
# self.env.lang, self.localcontext.get('lang'), request.env.lang
lang = None
if frame.f_locals.get('context'):
lang = frame.f_locals['context'].get('lang')
@ -212,6 +218,12 @@ class GettextAlias(object):
if not lang:
if hasattr(s, 'localcontext'):
lang = s.localcontext.get('lang')
if not lang:
try:
from openerp.http import request
lang = request.env.lang
except RuntimeError:
pass
if not lang:
# Last resort: attempt to guess the language of the user
# Pitfall: some operations are performed in sudo mode, and we