[MERGE] Fixed gettext translation.

bzr revid: jth@openerp.com-20101006092643-b91l3nazmrdf7bqn
This commit is contained in:
Julien Thewys 2010-10-06 11:26:43 +02:00
commit 049a203cc4
1 changed files with 44 additions and 18 deletions

View File

@ -29,6 +29,7 @@ import re
import tarfile import tarfile
import tempfile import tempfile
from os.path import join from os.path import join
import logging
from datetime import datetime from datetime import datetime
from lxml import etree from lxml import etree
@ -127,30 +128,55 @@ def translate(cr, name, source_type, lang, source=None):
res = res_trans and res_trans[0] or False res = res_trans and res_trans[0] or False
return res return res
logger = logging.getLogger('translate')
class GettextAlias(object): class GettextAlias(object):
def _get_cr(self, frame):
is_new_cr = False
cr = frame.f_locals.get('cr')
if not cr:
s = frame.f_locals.get('self', {})
cr = getattr(s, 'cr', False)
if not cr:
if frame.f_globals.get('pooler', False):
# TODO: we should probably get rid of the 'is_new_cr' case: no cr in locals -> no translation for you
dbs = frame.f_globals['pooler'].pool_dic.keys()
if len(dbs) == 1:
cr = pooler.get_db(dbs[0]).cursor()
is_new_cr = True
return cr, is_new_cr
def _get_lang(self, frame):
lang = frame.f_locals.get('context', {}).get('lang', False)
if not lang:
args = frame.f_locals.get('args', False)
if args:
lang = args[-1].get('lang', False)
if not lang:
s = frame.f_locals.get('self', {})
c = getattr(s, 'localcontext', {})
lang = c.get('lang', False)
return lang
def __call__(self, source): def __call__(self, source):
is_new_cr = False
res = source
try: try:
frame = inspect.stack()[1][0] frame = inspect.stack()[1][0]
cr, is_new_cr = self._get_cr(frame)
lang = self._get_lang(frame)
if lang and cr:
cr.execute('SELECT value FROM ir_translation WHERE lang=%s AND type IN (%s, %s) AND src=%s', (lang, 'code','sql_constraint', source))
res_trans = cr.fetchone()
res = res_trans and res_trans[0] or source
except: except:
return source logger.warn('translation went wrong for string %s', repr(source))
finally:
if is_new_cr:
cr.close()
return res
cr = frame.f_locals.get('cr')
try:
lang = (frame.f_locals.get('context') or {}).get('lang', False)
if not (cr and lang):
args = frame.f_locals.get('args',False)
if args:
lang = args[-1].get('lang',False)
if frame.f_globals.get('pooler',False):
cr = pooler.get_db(frame.f_globals['pooler'].pool_dic.keys()[0]).cursor()
if not (lang and cr):
return source
except:
return source
cr.execute('select value from ir_translation where lang=%s and type IN (%s,%s) and src=%s', (lang, 'code','sql_constraint', source))
res_trans = cr.fetchone()
return res_trans and res_trans[0] or source
_ = GettextAlias() _ = GettextAlias()