Package babel :: Package messages :: Module pofile

Module pofile



Reading and writing of files in the gettext PO (portable object) format.


See Also: The Format of PO Files

Functions
str or unicode
unescape(string)
Reverse escape the given string.
unicode or str
denormalize(string)
Reverse the normalization done by the normalize function.
iterator
read_po(fileobj)
Read messages from a gettext PO (portable object) file from the given file-like object and return a Catalog.
str or unicode
escape(string)
Escape the given string so that it can be included in double-quoted strings in PO files.
unicode
normalize(string, width=76)
Convert a string into a format that is appropriate for .po files.
 
write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, sort_output=False, sort_by_file=False)
Write a gettext PO (portable object) template file for a given message catalog to the provided file-like object.
Function Details

unescape(string)

 

Reverse escape the given string.

>>> print unescape('"Say:\\n  \\"hello, world!\\"\\n"')
Say:
  "hello, world!"
<BLANKLINE>
Parameters:
  • string - the string to unescape
Returns: str or unicode
the unescaped string

denormalize(string)

 

Reverse the normalization done by the normalize function.

>>> print denormalize(r'''""
... "Say:\n"
... "  \"hello, world!\"\n"''')
Say:
  "hello, world!"
<BLANKLINE>
>>> print denormalize(r'''""
... "Say:\n"
... "  \"Lorem ipsum dolor sit "
... "amet, consectetur adipisicing"
... " elit, \"\n"''')
Say:
  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
<BLANKLINE>
Parameters:
  • string - the string to denormalize
Returns: unicode or str
the denormalized string

read_po(fileobj)

 

Read messages from a gettext PO (portable object) file from the given file-like object and return a Catalog.

>>> from StringIO import StringIO
>>> buf = StringIO('''
... #: main.py:1
... #, fuzzy, python-format
... msgid "foo %(name)s"
... msgstr ""
...
... # A user comment
... #. An auto comment
... #: main.py:3
... msgid "bar"
... msgid_plural "baz"
... msgstr[0] ""
... msgstr[1] ""
... ''')
>>> catalog = read_po(buf)
>>> catalog.revision_date = datetime(2007, 04, 01)
>>> for message in catalog:
...     if message.id:
...         print (message.id, message.string)
...         print ' ', (message.locations, message.flags)
...         print ' ', (message.user_comments, message.auto_comments)
(u'foo %(name)s', '')
  ([(u'main.py', 1)], set([u'fuzzy', u'python-format']))
  ([], [])
((u'bar', u'baz'), ('', ''))
  ([(u'main.py', 3)], set([]))
  ([u'A user comment'], [u'An auto comment'])
Parameters:
  • fileobj - the file-like object to read the PO file from
Returns: iterator
an iterator over (message, translation, location) tuples

escape(string)

 

Escape the given string so that it can be included in double-quoted strings in PO files.

>>> escape('''Say:
...   "hello, world!"
... ''')
'"Say:\\n  \\"hello, world!\\"\\n"'
Parameters:
  • string - the string to escape
Returns: str or unicode
the escaped string

normalize(string, width=76)

 

Convert a string into a format that is appropriate for .po files.

>>> print normalize('''Say:
...   "hello, world!"
... ''', width=None)
""
"Say:\n"
"  \"hello, world!\"\n"
>>> print normalize('''Say:
...   "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
... ''', width=32)
""
"Say:\n"
"  \"Lorem ipsum dolor sit "
"amet, consectetur adipisicing"
" elit, \"\n"
Parameters:
  • string - the string to normalize
  • width - the maximum line width; use None, 0, or a negative number to completely disable line wrapping
Returns: unicode
the normalized string

write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, sort_output=False, sort_by_file=False)

 

Write a gettext PO (portable object) template file for a given message catalog to the provided file-like object.

>>> catalog = Catalog()
>>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)],
...             flags=('fuzzy',))
>>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
>>> from StringIO import StringIO
>>> buf = StringIO()
>>> write_po(buf, catalog, omit_header=True)
>>> print buf.getvalue()
#: main.py:1
#, fuzzy, python-format
msgid "foo %(name)s"
msgstr ""
<BLANKLINE>
#: main.py:3
msgid "bar"
msgid_plural "baz"
msgstr[0] ""
msgstr[1] ""
<BLANKLINE>
<BLANKLINE>
Parameters:
  • fileobj - the file-like object to write to
  • catalog - the Catalog instance
  • width - the maximum line width for the generated output; use None, 0, or a negative number to completely disable line wrapping
  • no_location - do not emit a location comment for every message
  • omit_header - do not include the msgid "" entry at the top of the output