comparison babel/messages/catalog.py @ 95:008cd3f7d485

Fix for #11 (use local timezone in timestamps of generated POT).
author cmlenz
date Tue, 12 Jun 2007 18:40:39 +0000
parents f259182f9baf
children a02952b73cf1
comparison
equal deleted inserted replaced
94:b176f325d127 95:008cd3f7d485
22 import time 22 import time
23 23
24 from babel import __version__ as VERSION 24 from babel import __version__ as VERSION
25 from babel.core import Locale 25 from babel.core import Locale
26 from babel.messages.plurals import PLURALS 26 from babel.messages.plurals import PLURALS
27 from babel.util import odict, UTC 27 from babel.util import odict, LOCAL, UTC
28 28
29 __all__ = ['Message', 'Catalog'] 29 __all__ = ['Message', 'Catalog']
30 __docformat__ = 'restructuredtext en' 30 __docformat__ = 'restructuredtext en'
31 31
32 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?[diouxXeEfFgGcrs]').search 32 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?[diouxXeEfFgGcrs]').search
130 self._messages = odict() 130 self._messages = odict()
131 131
132 self.project = project or 'PROJECT' #: the project name 132 self.project = project or 'PROJECT' #: the project name
133 self.version = version or 'VERSION' #: the project version 133 self.version = version or 'VERSION' #: the project version
134 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS' 134 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS'
135 self.last_translator = last_translator #: last translator name + email
136 self.charset = charset or 'utf-8'
135 137
136 if creation_date is None: 138 if creation_date is None:
137 creation_date = time.localtime() 139 creation_date = datetime.now(LOCAL)
138 elif isinstance(creation_date, datetime): 140 elif isinstance(creation_date, datetime) and not creation_date.tzinfo:
139 if creation_date.tzinfo is None: 141 creation_date = creation_date.replace(tzinfo=LOCAL)
140 creation_date = creation_date.replace(tzinfo=UTC)
141 creation_date = creation_date.timetuple()
142 self.creation_date = creation_date #: creation date of the template 142 self.creation_date = creation_date #: creation date of the template
143 if revision_date is None: 143 if revision_date is None:
144 revision_date = time.localtime() 144 revision_date = datetime.now(LOCAL)
145 elif isinstance(revision_date, datetime): 145 elif isinstance(revision_date, datetime) and not revision_date.tzinfo:
146 if revision_date.tzinfo is None: 146 revision_date = revision_date.replace(tzinfo=LOCAL)
147 revision_date = revision_date.replace(tzinfo=UTC)
148 revision_date = revision_date.timetuple()
149 self.revision_date = revision_date #: last revision date of the catalog 147 self.revision_date = revision_date #: last revision date of the catalog
150 self.last_translator = last_translator #: last translator name + email
151 self.charset = charset or 'utf-8'
152 148
153 def headers(self): 149 def headers(self):
154 headers = [] 150 headers = []
155 headers.append(('Project-Id-Version', 151 headers.append(('Project-Id-Version',
156 '%s %s' % (self.project, self.version))) 152 '%s %s' % (self.project, self.version)))
157 headers.append(('Report-Msgid-Bugs-To', self.msgid_bugs_address)) 153 headers.append(('Report-Msgid-Bugs-To', self.msgid_bugs_address))
158 headers.append(('POT-Creation-Date', 154 headers.append(('POT-Creation-Date',
159 time.strftime('%Y-%m-%d %H:%M%z', self.creation_date))) 155 self.creation_date.strftime('%Y-%m-%d %H:%M%z')))
160 if self.locale is None: 156 if self.locale is None:
161 headers.append(('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE')) 157 headers.append(('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE'))
162 headers.append(('Last-Translator', 'FULL NAME <EMAIL@ADDRESS>')) 158 headers.append(('Last-Translator', 'FULL NAME <EMAIL@ADDRESS>'))
163 headers.append(('Language-Team', 'LANGUAGE <LL@li.org>')) 159 headers.append(('Language-Team', 'LANGUAGE <LL@li.org>'))
164 else: 160 else:
165 headers.append(('PO-Revision-Date', 161 headers.append(('PO-Revision-Date',
166 time.strftime('%Y-%m-%d %H:%M%z', self.revision_date))) 162 self.revision_date.strftime('%Y-%m-%d %H:%M%z')))
167 headers.append(('Last-Translator', self.last_translator)) 163 headers.append(('Last-Translator', self.last_translator))
168 headers.append(('Language-Team', '%s <LL@li.org>' % self.locale)) 164 headers.append(('Language-Team', '%s <LL@li.org>' % self.locale))
169 headers.append(('Plural-Forms', self.plural_forms)) 165 headers.append(('Plural-Forms', self.plural_forms))
170 headers.append(('MIME-Version', '1.0')) 166 headers.append(('MIME-Version', '1.0'))
171 headers.append(('Content-Type', 167 headers.append(('Content-Type',
180 is set or not, the latter indicating that the catalog is actually a template 176 is set or not, the latter indicating that the catalog is actually a template
181 for actual translations. 177 for actual translations.
182 178
183 Here's an example of the output for such a catalog template: 179 Here's an example of the output for such a catalog template:
184 180
181 >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC)
185 >>> catalog = Catalog(project='Foobar', version='1.0', 182 >>> catalog = Catalog(project='Foobar', version='1.0',
186 ... creation_date=datetime(1990, 4, 1, 15, 30)) 183 ... creation_date=created)
187 >>> for name, value in catalog.headers: 184 >>> for name, value in catalog.headers:
188 ... print '%s: %s' % (name, value) 185 ... print '%s: %s' % (name, value)
189 Project-Id-Version: Foobar 1.0 186 Project-Id-Version: Foobar 1.0
190 Report-Msgid-Bugs-To: EMAIL@ADDRESS 187 Report-Msgid-Bugs-To: EMAIL@ADDRESS
191 POT-Creation-Date: 1990-04-01 15:30+0000 188 POT-Creation-Date: 1990-04-01 15:30+0000
197 Content-Transfer-Encoding: 8bit 194 Content-Transfer-Encoding: 8bit
198 Generated-By: Babel ... 195 Generated-By: Babel ...
199 196
200 And here's an example of the output when the locale is set: 197 And here's an example of the output when the locale is set:
201 198
199 >>> revised = datetime(1990, 8, 3, 12, 0, tzinfo=UTC)
202 >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0', 200 >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0',
203 ... creation_date=datetime(1990, 4, 1, 15, 30), 201 ... creation_date=created, revision_date=revised,
204 ... revision_date=datetime(1990, 8, 3, 12, 0),
205 ... last_translator='John Doe <jd@example.com>') 202 ... last_translator='John Doe <jd@example.com>')
206 >>> for name, value in catalog.headers: 203 >>> for name, value in catalog.headers:
207 ... print '%s: %s' % (name, value) 204 ... print '%s: %s' % (name, value)
208 Project-Id-Version: Foobar 1.0 205 Project-Id-Version: Foobar 1.0
209 Report-Msgid-Bugs-To: EMAIL@ADDRESS 206 Report-Msgid-Bugs-To: EMAIL@ADDRESS
Copyright (C) 2012-2017 Edgewall Software