comparison babel/messages/catalog.py @ 104:22f222e23b86

Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
author cmlenz
date Wed, 13 Jun 2007 23:02:24 +0000
parents 7cdf89eb9007
children f744dd56573d
comparison
equal deleted inserted replaced
103:7cdf89eb9007 104:22f222e23b86
101 True 101 True
102 102
103 :type: `bool` 103 :type: `bool`
104 """) 104 """)
105 105
106 DEFAULT_HEADER = u"""\
107 # Translations template for PROJECT.
108 # Copyright (C) YEAR COPYRIGHT HOLDER
109 # This file is distributed under the same license as the PROJECT project.
110 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
111 #"""
106 112
107 class Catalog(object): 113 class Catalog(object):
108 """Representation of a message catalog.""" 114 """Representation of a message catalog."""
109 115
110 def __init__(self, locale=None, domain=None, project=None, version=None, 116 def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER,
117 project=None, version=None, copyright_holder=None,
111 msgid_bugs_address=None, creation_date=None, 118 msgid_bugs_address=None, creation_date=None,
112 revision_date=None, last_translator=None, charset='utf-8'): 119 revision_date=None, last_translator=None, charset='utf-8'):
113 """Initialize the catalog object. 120 """Initialize the catalog object.
114 121
115 :param locale: the locale identifier or `Locale` object, or `None` 122 :param locale: the locale identifier or `Locale` object, or `None`
116 if the catalog is not bound to a locale (which basically 123 if the catalog is not bound to a locale (which basically
117 means it's a template) 124 means it's a template)
118 :param domain: the message domain 125 :param domain: the message domain
126 :param header_comment: the header comment as string, or `None` for the
127 default header
119 :param project: the project's name 128 :param project: the project's name
120 :param version: the project's version 129 :param version: the project's version
121 :param msgid_bugs_address: the address to report bugs about the catalog 130 :param copyright_holder: the copyright holder of the catalog
131 :param msgid_bugs_address: the email address or URL to submit bug
132 reports to
122 :param creation_date: the date the catalog was created 133 :param creation_date: the date the catalog was created
123 :param revision_date: the date the catalog was revised 134 :param revision_date: the date the catalog was revised
124 :param last_translator: the name and email of the last translator 135 :param last_translator: the name and email of the last translator
136 :param charset: the encoding to use in the output
125 """ 137 """
126 self.domain = domain #: the message domain 138 self.domain = domain #: the message domain
127 if locale: 139 if locale:
128 locale = Locale.parse(locale) 140 locale = Locale.parse(locale)
129 self.locale = locale #: the locale or `None` 141 self.locale = locale #: the locale or `None`
142 self._header_comment = header_comment
130 self._messages = odict() 143 self._messages = odict()
131 144
132 self.project = project or 'PROJECT' #: the project name 145 self.project = project or 'PROJECT' #: the project name
133 self.version = version or 'VERSION' #: the project version 146 self.version = version or 'VERSION' #: the project version
147 self.copyright_holder = copyright_holder or 'ORGANIZATION'
134 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS' 148 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS'
135 self.last_translator = last_translator #: last translator name + email 149 self.last_translator = last_translator #: last translator name + email
136 self.charset = charset or 'utf-8' 150 self.charset = charset or 'utf-8'
137 151
138 if creation_date is None: 152 if creation_date is None:
144 revision_date = datetime.now(LOCALTZ) 158 revision_date = datetime.now(LOCALTZ)
145 elif isinstance(revision_date, datetime) and not revision_date.tzinfo: 159 elif isinstance(revision_date, datetime) and not revision_date.tzinfo:
146 revision_date = revision_date.replace(tzinfo=LOCALTZ) 160 revision_date = revision_date.replace(tzinfo=LOCALTZ)
147 self.revision_date = revision_date #: last revision date of the catalog 161 self.revision_date = revision_date #: last revision date of the catalog
148 162
149 def headers(self): 163 def get_header_comment(self):
164 comment = self._header_comment
165 comment = comment.replace('PROJECT', self.project) \
166 .replace('VERSION', self.version) \
167 .replace('YEAR', self.revision_date.strftime('%Y')) \
168 .replace('COPYRIGHT HOLDER', self.copyright_holder)
169 if self.locale:
170 comment = comment.replace('Translations template',
171 '%s translations' % self.locale.english_name)
172 return comment
173 def set_header_comment(self, string):
174 self._header_comment = string
175 header_comment = property(get_header_comment, set_header_comment, doc="""\
176 The header comment for the catalog.
177
178 >>> catalog = Catalog(project='Foobar', version='1.0',
179 ... copyright_holder='Foo Company')
180 >>> print catalog.header_comment
181 # Translations template for Foobar.
182 # Copyright (C) 2007 Foo Company
183 # This file is distributed under the same license as the Foobar project.
184 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
185 #
186
187 :type: `unicode`
188 """)
189
190 def mime_headers(self):
150 headers = [] 191 headers = []
151 headers.append(('Project-Id-Version', 192 headers.append(('Project-Id-Version',
152 '%s %s' % (self.project, self.version))) 193 '%s %s' % (self.project, self.version)))
153 headers.append(('Report-Msgid-Bugs-To', self.msgid_bugs_address)) 194 headers.append(('Report-Msgid-Bugs-To', self.msgid_bugs_address))
154 headers.append(('POT-Creation-Date', 195 headers.append(('POT-Creation-Date',
167 headers.append(('Content-Type', 208 headers.append(('Content-Type',
168 'text/plain; charset=%s' % self.charset)) 209 'text/plain; charset=%s' % self.charset))
169 headers.append(('Content-Transfer-Encoding', '8bit')) 210 headers.append(('Content-Transfer-Encoding', '8bit'))
170 headers.append(('Generated-By', 'Babel %s' % VERSION)) 211 headers.append(('Generated-By', 'Babel %s' % VERSION))
171 return headers 212 return headers
172 headers = property(headers, doc="""\ 213 mime_headers = property(mime_headers, doc="""\
173 The MIME headers of the catalog, used for the special ``msgid ""`` entry. 214 The MIME headers of the catalog, used for the special ``msgid ""`` entry.
174 215
175 The behavior of this property changes slightly depending on whether a locale 216 The behavior of this property changes slightly depending on whether a locale
176 is set or not, the latter indicating that the catalog is actually a template 217 is set or not, the latter indicating that the catalog is actually a template
177 for actual translations. 218 for actual translations.
179 Here's an example of the output for such a catalog template: 220 Here's an example of the output for such a catalog template:
180 221
181 >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC) 222 >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC)
182 >>> catalog = Catalog(project='Foobar', version='1.0', 223 >>> catalog = Catalog(project='Foobar', version='1.0',
183 ... creation_date=created) 224 ... creation_date=created)
184 >>> for name, value in catalog.headers: 225 >>> for name, value in catalog.mime_headers:
185 ... print '%s: %s' % (name, value) 226 ... print '%s: %s' % (name, value)
186 Project-Id-Version: Foobar 1.0 227 Project-Id-Version: Foobar 1.0
187 Report-Msgid-Bugs-To: EMAIL@ADDRESS 228 Report-Msgid-Bugs-To: EMAIL@ADDRESS
188 POT-Creation-Date: 1990-04-01 15:30+0000 229 POT-Creation-Date: 1990-04-01 15:30+0000
189 PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE 230 PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
198 239
199 >>> revised = datetime(1990, 8, 3, 12, 0, tzinfo=UTC) 240 >>> revised = datetime(1990, 8, 3, 12, 0, tzinfo=UTC)
200 >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0', 241 >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0',
201 ... creation_date=created, revision_date=revised, 242 ... creation_date=created, revision_date=revised,
202 ... last_translator='John Doe <jd@example.com>') 243 ... last_translator='John Doe <jd@example.com>')
203 >>> for name, value in catalog.headers: 244 >>> for name, value in catalog.mime_headers:
204 ... print '%s: %s' % (name, value) 245 ... print '%s: %s' % (name, value)
205 Project-Id-Version: Foobar 1.0 246 Project-Id-Version: Foobar 1.0
206 Report-Msgid-Bugs-To: EMAIL@ADDRESS 247 Report-Msgid-Bugs-To: EMAIL@ADDRESS
207 POT-Creation-Date: 1990-04-01 15:30+0000 248 POT-Creation-Date: 1990-04-01 15:30+0000
208 PO-Revision-Date: 1990-08-03 12:00+0000 249 PO-Revision-Date: 1990-08-03 12:00+0000
271 were added, yielding a `Message` object for every entry. 312 were added, yielding a `Message` object for every entry.
272 313
273 :rtype: ``iterator`` 314 :rtype: ``iterator``
274 """ 315 """
275 buf = [] 316 buf = []
276 for name, value in self.headers: 317 for name, value in self.mime_headers:
277 buf.append('%s: %s' % (name, value)) 318 buf.append('%s: %s' % (name, value))
278 yield Message('', '\n'.join(buf), flags=set(['fuzzy'])) 319 yield Message('', '\n'.join(buf), flags=set(['fuzzy']))
279 for key in self._messages: 320 for key in self._messages:
280 yield self._messages[key] 321 yield self._messages[key]
281 322
Copyright (C) 2012-2017 Edgewall Software