Felix Schwarz avatar Felix Schwarz committed c5dd375

ensure .mo file header contains the same information as the source .po file (#199)

Comments (0)

Files changed (4)

  * "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


 """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
             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
     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'
                         format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ',
-        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)):
                                             '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):
+        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'))


         catalog.locale = self._locale
+        catalog.revision_date = datetime.now(LOCALTZ)
         catalog.fuzzy = False
         outfile = open(self.output_file, 'w')


 import doctest
 import unittest
+from babel.dates import format_datetime
 from babel.messages import catalog
+from babel.util import FixedOffsetTimezone
 class MessageTestCase(unittest.TestCase):
         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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.