Launchpad automatic translations update.
[odoo/odoo.git] / bin / tools / translate.py
index 510ae71..07c4138 100644 (file)
@@ -32,6 +32,7 @@ import logging
 import tarfile
 import tempfile
 import threading
+import sys
 from os.path import join
 
 from datetime import datetime
@@ -161,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')
@@ -188,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):
@@ -294,11 +308,14 @@ class TinyPoFile(object):
                 if line.startswith('#~ '):
                     break
                 if line.startswith('#:'):
-                    if ' ' in line[2:].strip():
-                        for lpart in line[2:].strip().split(' '):
-                            tmp_tnrs.append(lpart.strip().split(':',2))
-                    else:
-                        tmp_tnrs.append( line[2:].strip().split(':',2) )
+                    for lpart in line[2:].strip().split(' '):
+                        trans_info = lpart.strip().split(':',2)
+                        if trans_info and len(trans_info) == 2:
+                            # looks like the translation type is missing, which is not
+                            # unexpected because it is not a GetText standard. Default: 'code'
+                            trans_info[:0] = ['code']
+                        if trans_info and len(trans_info) == 3:
+                            tmp_tnrs.append(trans_info)
                 elif line.startswith('#,') and (line[2:].strip() == 'fuzzy'):
                     fuzzy = True
                 line = self.lines.pop(0).strip()
@@ -873,6 +890,8 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
         # now, the serious things: we read the language file
         fileobj.seek(0)
         if fileformat == 'csv':
+            #Setting the limit of data while loading a CSV
+            csv.field_size_limit(sys.maxint)
             reader = csv.reader(fileobj, quotechar='"', delimiter=',')
             # read the first line of the file (it contains columns titles)
             for row in reader:
@@ -916,7 +935,6 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
                 ('lang', '=', lang),
                 ('type', '=', dic['type']),
                 ('name', '=', dic['name']),
-                ('src', '=', dic['src']),
             ]
             if dic['type'] == 'model':
                 if dic['res_id'] is False:
@@ -924,6 +942,9 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
                     args.append(('xml_id', '=', dic['xml_id']))
                 else:
                     args.append(('res_id', '=', dic['res_id']))
+            else:
+                args.append(('src', '=', dic['src']))
+
             ids = trans_obj.search(cr, uid, args)
             if ids:
                 if context.get('overwrite') and dic['value']:
@@ -991,7 +1012,7 @@ def resetlocale():
 def load_language(cr, lang):
     """Loads a translation terms for a language.
     Used mainly to automate language loading at db initialization.
-    
+
     :param lang: language ISO code with optional _underscore_ and l10n flavor (ex: 'fr', 'fr_BE', but not 'fr-BE')
     :type lang: str
     """