Reviewers: ,


Please review this at http://codereview.tryton.org/125002/

Affected files:
  M trytond/ir/translation.py
  M trytond/modules/__init__.py


Index: trytond/ir/translation.py
===================================================================

--- a/trytond/ir/translation.py
+++ b/trytond/ir/translation.py
@@ -6,6 +6,8 @@
 except ImportError:
     import StringIO
 import zipfile
+import polib
+import operator
 from xml import dom
 from xml.dom import minidom
 from difflib import SequenceMatcher
@@ -46,6 +48,13 @@
 csv.register_dialect("TRYTON", TRYTON)


+class TrytonPOFile(polib.POFile):
+
+    def sort(self):
+        return super(TrytonPOFile, self).sort(
+            key=operator.attrgetter('msgctxt'))
+
+
 class Translation(ModelSQL, ModelView, Cacheable):
     "Translation"
     _name = "ir.translation"
@@ -453,9 +462,7 @@
fs_id2model_data[model_data.model][model_data.fs_id] = model_data

         translation_ids = []
-        reader = csv.reader(datas)
-        for row in reader:
-            break
+        pofile = polib.pofile(datas)

         id2translation = {}
         key2ids = {}
@@ -475,13 +482,11 @@
             key2ids.setdefault(key, []).append(translation.id)
             id2translation[translation.id] = translation

-        for row in reader:
-            ttype = row[0].decode('utf-8')
-            name = row[1].decode('utf-8')
-            res_id = row[2].decode('utf-8')
-            src = row[3].decode('utf-8')
-            value = row[4].decode('utf-8')
-            fuzzy = bool(int(row[5]))
+        for entry in pofile:
+            ttype, name, res_id = entry.msgctxt.split(':')
+            src = entry.msgid
+            value = entry.msgstr
+            fuzzy = 'fuzzy' in entry.flags
             noupdate = False

             model = name.split(',')[0]
@@ -552,10 +557,12 @@
             db_id2fs_id.setdefault(model_data.model, {})
db_id2fs_id[model_data.model][model_data.db_id] = model_data.fs_id

-        buf = StringIO.StringIO()
-        writer = csv.writer(buf, 'TRYTON')
-        writer.writerow(HEADER)
-        rows = []
+        pofile = TrytonPOFile()
+        pofile.metadata = {
+            'MIME-Version': '1.0',
+            'Content-Type': 'text/plain; charset=utf-8',
+            'Content-Transfer-Encoding': '8bit',
+            }

         with Transaction().set_context(language='en_US'):
             translation_ids = self.search([
@@ -563,32 +570,22 @@
                 ('module', '=', module),
                 ], order=[])
         for translation in self.browse(translation_ids):
-            row = []
-            for field in HEADER:
-                if field == 'res_id':
-                    res_id = translation[field]
-                    if res_id:
-                        model = translation.name.split(',')[0]
-                        if model in db_id2fs_id:
-                            res_id = db_id2fs_id[model].get(res_id)
-                        else:
-                            break
-                    row.append(res_id)
-                elif field == 'fuzzy':
-                    row.append(int(translation[field]))
+            flags = [] if not translation['fuzzy'] else ['fuzzy']
+            trans_ctxt = '%(type)s:%(name)s' % translation
+            res_id = translation['res_id']
+            if res_id:
+                model, _ = translation.name.split(',')
+                if model in db_id2fs_id:
+                    res_id = db_id2fs_id[model].get(res_id)
                 else:
-                    value = translation[field] or ''
-                    value = value.encode('utf-8')
-                    row.append(value)
-            if len(row) == len(HEADER):
-                rows.append(row)
+                    break
+            trans_ctxt += ':%s' % res_id
+            entry = polib.POEntry(msgid=translation.src,
+                msgstr=translation.value, msgctxt=trans_ctxt, flags=flags)
+            pofile.append(entry)

-        rows.sort()
-        writer.writerows(rows)
-
-        file_data = buf.getvalue()
-        buf.close()
-        return file_data
+        pofile.sort()
+        return unicode(pofile).encode('utf-8')

 Translation()


Index: trytond/modules/__init__.py
===================================================================

--- a/trytond/modules/__init__.py
+++ b/trytond/modules/__init__.py
@@ -260,9 +260,9 @@
                 if lang2 not in lang:
                     continue
                 logger.info('%s:loading %s' % (module, filename))
-                with tools.file_open(OPJ(module, filename)) as trans_file:
-                    translation_obj = pool.get('ir.translation')
- translation_obj.translation_import(lang2, module, trans_file)
+                po_path = OPJ(MODULES_PATH, module, filename)
+                translation_obj = pool.get('ir.translation')
+                translation_obj.translation_import(lang2, module, po_path)

cursor.execute("UPDATE ir_module_module SET state = 'installed' " \
                     "WHERE name = %s", (package.name,))



--
[email protected] mailing list

Reply via email to