[FIX] OPW 572856: translate: attempt to auto-detect user's language preferences when...
authorOlivier Dony <odo@openerp.com>
Fri, 29 Jun 2012 10:38:34 +0000 (12:38 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 29 Jun 2012 10:38:34 +0000 (12:38 +0200)
lp bug: https://launchpad.net/bugs/434266 fixed

bzr revid: odo@openerp.com-20120629103834-lntqzjdu7rza9ubl

bin/tools/translate.py

index aa69b39..07c4138 100644 (file)
@@ -162,19 +162,22 @@ class GettextAlias(object):
         if db_name:
             return pooler.get_db_only(db_name)
 
-    def _get_cr(self, frame):
+    def _get_cr(self, frame, allow_create=True):
         is_new_cr = False
         cr = frame.f_locals.get('cr', frame.f_locals.get('cursor'))
         if not cr:
             s = frame.f_locals.get('self', {})
             cr = getattr(s, 'cr', None)
-        if not cr:
+        if not cr and allow_create:
             db = self._get_db()
             if db:
                 cr = db.cursor()
                 is_new_cr = True
         return cr, is_new_cr
 
+    def _get_uid(self, frame):
+        return frame.f_locals.get('uid') or frame.f_locals.get('user')
+
     def _get_lang(self, frame):
         lang = None
         ctx = frame.f_locals.get('context')
@@ -189,11 +192,21 @@ class GettextAlias(object):
                 ctx = kwargs.get('context')
         if ctx:
             lang = ctx.get('lang')
+        s = frame.f_locals.get('self', {})
         if not lang:
-            s = frame.f_locals.get('self', {})
             c = getattr(s, 'localcontext', None)
             if c:
                 lang = c.get('lang')
+        if not lang:
+            # Last resort: attempt to guess the language of the user
+            # Pitfall: some operations are performed in sudo mode, and we 
+            #          don't know the originial uid, so the language may
+            #          be wrong when the admin language differs.
+            pool = getattr(s, 'pool', None)
+            (cr, dummy) = self._get_cr(frame, allow_create=False)
+            uid = self._get_uid(frame)
+            if pool and cr and uid:
+                lang = pool.get('res.users').context_get(cr, uid)['lang']
         return lang
 
     def __call__(self, source):