]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
ensure .mo file header contains the same information as the source .po file (#199)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 27 Aug 2012 21:15:27 +0000 (21:15 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 27 Aug 2012 21:15:27 +0000 (21:15 +0000)
ChangeLog
babel/messages/catalog.py
babel/messages/frontend.py
babel/messages/tests/catalog.py

index 40de850a44f9f8ed9d0bb15a0eeae407f8ae2753..af872757fd186a7d4f25db58829d26ec59ce9114 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -57,6 +57,8 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
  * "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
index c81779eac86f732304823b64ebd838b2435dc59a..d149bfbc9bc0de398fb665123248c1f56b60b9f4 100644 (file)
@@ -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 @@ class Catalog(object):
             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 @@ class Catalog(object):
 
     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 @@ class Catalog(object):
         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',
index 41ef8939e5caa34ddf169105fb60f10f745590c7..c069530f6289ef6d483ab965b818d2b260b3bb0d 100755 (executable)
@@ -464,6 +464,7 @@ class init_catalog(Command):
             infile.close()
 
         catalog.locale = self._locale
+        catalog.revision_date = datetime.now(LOCALTZ)
         catalog.fuzzy = False
 
         outfile = open(self.output_file, 'w')
index 6d05ca7409c7f02308ca8dfb6011d520c15f5a05..64d112bef34cb83c843674d61c89c43bc8b4eac8 100644 (file)
@@ -16,7 +16,9 @@ import datetime
 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 @@ class CatalogTestCase(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()