class Message(object):
"""Representation of a single message in a catalog."""
- def __init__(self, id, string=None, locations=(), flags=()):
+ def __init__(self, id, string='', locations=(), flags=()):
"""Create the message object.
:param id: the message ID, or a ``(singular, plural)`` tuple for
:param flags: a set or sequence of flags
"""
self.id = id
+ if not string and self.pluralizable:
+ string = (u'', u'')
self.string = string
self.locations = locations
self.flags = set(flags)
"""Representation a message catalog."""
def __init__(self, locale=None, domain=None, project=None, version=None,
- creation_date=None, revision_date=None, last_translator=None):
+ creation_date=None, revision_date=None, last_translator=None,
+ charset='utf-8'):
"""Initialize the catalog object.
:param domain: the message domain
revision_date = revision_date.timetuple()
self.revision_date = revision_date #: last revision date of the catalog
self.last_translator = last_translator #: last translator name + email
+ self.charset = charset or 'utf-8'
def headers(self):
headers = []
headers.append(('Language-Team', '%s <LL@li.org>' % self.locale))
headers.append(('Plural-Forms', self.plural_forms))
headers.append(('MIME-Version', '1.0'))
- headers.append(('Content-Type', 'text/plain; charset=utf-8'))
+ headers.append(('Content-Type',
+ 'text/plain; charset=%s' % self.charset))
headers.append(('Content-Transfer-Encoding', '8bit'))
headers.append(('Generated-By', 'Babel %s' % VERSION))
return headers
:type: `list`
""")
+ def num_plurals(self):
+ num = 2
+ if self.locale:
+ if str(self.locale) in PLURALS:
+ num = PLURALS[str(self.locale)][0]
+ elif self.locale.language in PLURALS:
+ num = PLURALS[self.locale.language][0]
+ return num
+ num_plurals = property(num_plurals)
+
def plural_forms(self):
num, expr = ('INTEGER', 'EXPRESSION')
if self.locale:
if isinstance(id, (list, tuple)):
singular, plural = id
id = singular
+ assert isinstance(message.string, (list, tuple))
self._messages[id] = message
def add(self, id, string=None, locations=(), flags=()):
return catalog
POT_HEADER = """\
-# Translations template for %%(project)s.
-# Copyright (C) %%(year)s ORGANIZATION
+# Translations template for %(project)s.
+# Copyright (C) %(year)s ORGANIZATION
# This file is distributed under the same license as the
-# %%(project)s project.
+# %(project)s project.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
-#, fuzzy"""
-
-# msgid ""
-# msgstr ""
-# "Project-Id-Version: %%(project)s %%(version)s\\n"
-# "POT-Creation-Date: %%(creation_date)s\\n"
-# "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
-# "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
-# "Language-Team: LANGUAGE <LL@li.org>\\n"
-# "MIME-Version: 1.0\\n"
-# "Content-Type: text/plain; charset=%%(charset)s\\n"
-# "Content-Transfer-Encoding: 8bit\\n"
-# "Generated-By: Babel %s\\n"
-#
-# """ % VERSION
+"""
WORD_SEP = re.compile('('
r'\s+|' # any whitespace
text = text.encode(charset)
fileobj.write(text)
+ catalog.project = project
+ catalog.version = version
+ catalog.charset = charset
+
for message in catalog:
if not message.id: # This is the header "message"
if omit_header:
_write(POT_HEADER % {
'year': time.strftime('%Y'),
'project': project,
- 'version': version,
- 'creation_date': time.strftime('%Y-%m-%d %H:%M%z'),
- 'charset': charset,
})
if not no_location:
if isinstance(message.id, (list, tuple)):
_write('msgid %s\n' % _normalize(message.id[0]))
_write('msgid_plural %s\n' % _normalize(message.id[1]))
- _write('msgstr[0] ""\n')
- _write('msgstr[1] ""\n')
+ for i, string in enumerate(message.string):
+ _write('msgstr[%d] %s\n' % (i, _normalize(message.string[i])))
else:
_write('msgid %s\n' % _normalize(message.id))
- _write('msgstr ""\n')
+ _write('msgstr %s\n' % _normalize(message.string or ''))
_write('\n')
def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT',
>>> from StringIO import StringIO
>>> from babel import Locale
>>> locale_obj = Locale.parse('pt_PT')
- >>> inbuf = StringIO(r'''# Translations Template for FooBar.
+ >>> inbuf = StringIO(r'''# Translations template for FooBar.
... # Copyright (C) 2007 ORGANIZATION
... # This file is distributed under the same license as the
... # FooBar project.
in_header = True
for index in range(len(inlines)):
if in_header:
- if '# Translations Template' in inlines[index]:
+ if '# Translations template' in inlines[index]:
outlines.append('# %s Translations for %s\n' % \
(locale_obj.english_name, project))
elif '# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.' in inlines[index]: