changeset 606:c5dd3752bf2a trunk

ensure .mo file header contains the same information as the source .po file (#199)
author fschwarz
date Mon, 27 Aug 2012 21:15:27 +0000
parents 6dc7e067bafc
children fad20cd945e8
files ChangeLog babel/messages/catalog.py babel/messages/frontend.py babel/messages/tests/catalog.py
diffstat 4 files changed, 42 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -57,6 +57,8 @@
  * "init" and "update" commands support "--width" option (#284)
  * fix 'input_dirs' option for setuptools integration (#232, initial patch by 
    Étienne Bersac)
+ * ensure .mo file header contains the same information as the source .po file 
+   (#199)
 
 
 Version 0.9.6
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -14,7 +14,7 @@
 """Data structures for message catalogs."""
 
 from cgi import parse_header
-from datetime import datetime
+from datetime import datetime, time as time_
 from difflib import get_close_matches
 from email import message_from_string
 from copy import copy
@@ -255,7 +255,7 @@
             creation_date = creation_date.replace(tzinfo=LOCALTZ)
         self.creation_date = creation_date #: Creation date of the template
         if revision_date is None:
-            revision_date = datetime.now(LOCALTZ)
+            revision_date = 'YEAR-MO-DA HO:MI+ZONE'
         elif isinstance(revision_date, datetime) and not revision_date.tzinfo:
             revision_date = revision_date.replace(tzinfo=LOCALTZ)
         self.revision_date = revision_date #: Last revision date of the catalog
@@ -267,9 +267,12 @@
 
     def _get_header_comment(self):
         comment = self._header_comment
+        year = datetime.now(LOCALTZ).strftime('%Y')
+        if hasattr(self.revision_date, 'strftime'):
+            year = self.revision_date.strftime('%Y')
         comment = comment.replace('PROJECT', self.project) \
                          .replace('VERSION', self.version) \
-                         .replace('YEAR', self.revision_date.strftime('%Y')) \
+                         .replace('YEAR', year) \
                          .replace('ORGANIZATION', self.copyright_holder)
         if self.locale:
             comment = comment.replace('Translations template', '%s translations'
@@ -320,18 +323,20 @@
         headers.append(('POT-Creation-Date',
                         format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ',
                                         locale='en')))
-        if self.locale is None:
-            headers.append(('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE'))
-            headers.append(('Last-Translator', 'FULL NAME <EMAIL@ADDRESS>'))
-            headers.append(('Language-Team', 'LANGUAGE <LL@li.org>'))
-        else:
+        if isinstance(self.revision_date, (datetime, time_, int, long, float)):
             headers.append(('PO-Revision-Date',
                             format_datetime(self.revision_date,
                                             'yyyy-MM-dd HH:mmZ', locale='en')))
-            headers.append(('Last-Translator', self.last_translator))
+        else:
+            headers.append(('PO-Revision-Date', self.revision_date))
+        headers.append(('Last-Translator', self.last_translator))
+        if (self.locale is not None) and ('LANGUAGE' in self.language_team):
             headers.append(('Language-Team',
                            self.language_team.replace('LANGUAGE',
                                                       str(self.locale))))
+        else:
+            headers.append(('Language-Team', self.language_team))
+        if self.locale is not None:
             headers.append(('Plural-Forms', self.plural_forms))
         headers.append(('MIME-Version', '1.0'))
         headers.append(('Content-Type',
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -464,6 +464,7 @@
             infile.close()
 
         catalog.locale = self._locale
+        catalog.revision_date = datetime.now(LOCALTZ)
         catalog.fuzzy = False
 
         outfile = open(self.output_file, 'w')
--- a/babel/messages/tests/catalog.py
+++ b/babel/messages/tests/catalog.py
@@ -16,7 +16,9 @@
 import doctest
 import unittest
 
+from babel.dates import format_datetime
 from babel.messages import catalog
+from babel.util import FixedOffsetTimezone
 
 
 class MessageTestCase(unittest.TestCase):
@@ -282,6 +284,29 @@
         for key, value in localized.mime_headers:
             if key in ('POT-Creation-Date', 'PO-Revision-Date'):
                 self.assertEqual(value, '2009-03-09 15:47-0700')
+    
+    def test_mime_headers_contain_same_information_as_attributes(self):
+        cat = catalog.Catalog()
+        cat[''] = catalog.Message('', 
+                      "Last-Translator: Foo Bar <foo.bar@example.com>\n" +
+                      "Language-Team: de <de@example.com>\n" +
+                      "POT-Creation-Date: 2009-03-01 11:20+0200\n" +
+                      "PO-Revision-Date: 2009-03-09 15:47-0700\n")
+        self.assertEqual(None, cat.locale)
+        mime_headers = dict(cat.mime_headers)
+        
+        self.assertEqual('Foo Bar <foo.bar@example.com>', cat.last_translator)
+        self.assertEqual('Foo Bar <foo.bar@example.com>', 
+                         mime_headers['Last-Translator'])
+        
+        self.assertEqual('de <de@example.com>', cat.language_team)
+        self.assertEqual('de <de@example.com>', mime_headers['Language-Team'])
+        
+        dt = datetime.datetime(2009, 3, 9, 15, 47, tzinfo=FixedOffsetTimezone(-7 * 60))
+        self.assertEqual(dt, cat.revision_date)
+        formatted_dt = format_datetime(dt, 'yyyy-MM-dd HH:mmZ', locale='en')
+        self.assertEqual(formatted_dt, mime_headers['PO-Revision-Date'])
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software