from babel.util import wraptext
from babel._compat import text_type
-__all__ = ['read_po', 'write_po']
-
def unescape(string):
r"""Reverse `escape` the given string.
<BLANKLINE>
:param string: the string to unescape
- :return: the unescaped string
"""
def replace_escapes(match):
m = match.group(1)
return m
return re.compile(r'\\([\\trn"])').sub(replace_escapes, string[1:-1])
+
def denormalize(string):
r"""Reverse the normalization done by the `normalize` function.
<BLANKLINE>
:param string: the string to denormalize
- :return: the denormalized string
- :rtype: `unicode` or `str`
"""
if '\n' in string:
escaped_lines = string.splitlines()
else:
return unescape(string)
+
def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=None):
"""Read messages from a ``gettext`` PO (portable object) file from the given
file-like object and return a `Catalog`.
:param domain: the message domain
:param ignore_obsolete: whether to ignore obsolete messages in the input
:param charset: the character set of the catalog.
- :return: a catalog object representing the parsed PO file
- :rtype: `Catalog`
"""
catalog = Catalog(locale=locale, domain=domain, charset=charset)
return catalog
+
WORD_SEP = re.compile('('
r'\s+|' # any whitespace
r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash
')')
+
def escape(string):
r"""Escape the given string so that it can be included in double-quoted
strings in ``PO`` files.
'"Say:\\n \\"hello, world!\\"\\n"'
:param string: the string to escape
- :return: the escaped string
"""
return '"%s"' % string.replace('\\', '\\\\') \
.replace('\t', '\\t') \
.replace('\n', '\\n') \
.replace('\"', '\\"')
+
def normalize(string, prefix='', width=76):
r"""Convert a string into a format that is appropriate for .po files.
:param prefix: a string that should be prepended to every line
:param width: the maximum line width; use `None`, 0, or a negative number
to completely disable line wrapping
- :return: the normalized string
"""
if width and width > 0:
prefixlen = len(prefix)
lines[-1] += '\n'
return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines])
+
def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
sort_output=False, sort_by_file=False, ignore_obsolete=False,
include_previous=False):