Module polib
[hide private]
[frames] | no frames]

Module polib

source code

polib allows you to manipulate, create, modify gettext files (pot, po and mo files). You can load existing files, iterate through it's entries, add, modify entries, comments or metadata, etc... or create new po files from scratch.

polib provides a simple and pythonic API, exporting only three convenience functions (pofile, mofile and detect_encoding), and the four core classes, POFile, MOFile, POEntry and MOEntry for creating new files/entries.

Basic example:

>>> import polib
>>> # load an existing po file
>>> po = polib.pofile('tests/test_utf8.po')
>>> for entry in po:
...     # do something with entry...
...     pass
>>> # add an entry
>>> entry = polib.POEntry(msgid='Welcome', msgstr='Bienvenue')
>>> entry.occurences = [('welcome.py', '12'), ('anotherfile.py', '34')]
>>> po.append(entry)
>>> # to save our modified po file:
>>> # po.save()
>>> # or you may want to compile the po file
>>> # po.save_as_mofile('tests/test_utf8.mo')



Version: 0.3.0

Author: David JEAN LOUIS <izimobil@gmail.com>

Classes [hide private]
  _BaseFile
Common parent class for POFile and MOFile classes.
  POFile
Po (or Pot) file reader/writer.
  MOFile
Mo file reader/writer.
  _BaseEntry
Base class for POEntry or MOEntry objects.
  POEntry
Represents a po file entry.
  MOEntry
Represents a mo file entry.
  _POFileParser
A finite state machine to parse efficiently and correctly po file format.
  _MOFileParser
A class to parse binary mo files.
Functions [hide private]
 
_dictget(D, k, d=...)
d defaults to None.
source code
 
_listappend(L, object)
append object to end
 
_listpop(L, index=...)
remove and return item at index (default last)
 
_strjoin(S, sequence)
Return a string which is the concatenation of the strings in the sequence.
source code
 
_strsplit(S, sep=... , maxsplit=...)
Return a list of the words in the string S, using sep as the delimiter string.
source code
 
_strstrip(S, chars=...)
Return a copy of the string S with leading and trailing whitespace removed.
source code
 
_strreplace(...)
S.replace (old, new[, count]) -> string
source code
 
pofile(fpath, wrapwidth=78, autodetect_encoding=True)
Convenience function that parse the po/pot file fpath and return a POFile instance.
source code
 
mofile(fpath, wrapwidth=78, autodetect_encoding=True)
Convenience function that parse the mo file fpath and return a MOFile instance.
source code
 
detect_encoding(fpath)
Try to detect the encoding used by the file fpath.
source code
Variables [hide private]
  encoding = 'utf-8'
Function Details [hide private]

_dictget(D, k, d=...)

source code 
d defaults to None.
Returns:
D[k] if k in D, else d

_listpop(L, index=...)

 
remove and return item at index (default last)
Returns:
item

_strjoin(S, sequence)

source code 
Return a string which is the concatenation of the strings in the sequence. The separator between elements is S.
Returns:
string

_strsplit(S, sep=... , maxsplit=...)

source code 
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator.
Returns:
list of strings

_strstrip(S, chars=...)

source code 
Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping
Returns:
string or unicode

_strreplace(...)

source code 

S.replace (old, new[, count]) -> string

Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

pofile(fpath, wrapwidth=78, autodetect_encoding=True)

source code 

Convenience function that parse the po/pot file fpath and return a POFile instance.

Keyword arguments:
  • fpath: string, full or relative path to the po/pot file to parse
  • wrapwidth: integer, the wrap width, only useful when -w option was passed to xgettext, default to 78 (optional)
  • autodetect_encoding: boolean, if set to False the function will not try to detect the po file encoding

Example:

>>> import polib
>>> po = polib.pofile('tests/test_utf8.po')
>>> po #doctest: +ELLIPSIS
<POFile instance at ...>

mofile(fpath, wrapwidth=78, autodetect_encoding=True)

source code 

Convenience function that parse the mo file fpath and return a MOFile instance.

Keyword arguments:
  • fpath: string, full or relative path to the mo file to parse
  • wrapwidth: integer, the wrap width, only useful when -w option was passed to xgettext to generate the po file that was used to format the mo file, default to 78 (optional)
  • autodetect_encoding: boolean, if set to False the function will not try to detect the po file encoding

Example:

>>> import polib
>>> mo = polib.mofile('tests/test_utf8.mo')
>>> mo #doctest: +ELLIPSIS
<MOFile instance at ...>

detect_encoding(fpath)

source code 

Try to detect the encoding used by the file fpath. The function will return polib default encoding if it's unable to detect it.

Keyword argument:
  • fpath: string, full or relative path to the mo file to parse.

Examples:

>>> print detect_encoding('tests/test_noencoding.po')
utf-8
>>> print detect_encoding('tests/test_utf8.po')
UTF-8
>>> print detect_encoding('tests/test_utf8.mo')
UTF-8
>>> print detect_encoding('tests/test_iso-8859-15.po')
ISO_8859-15
>>> print detect_encoding('tests/test_iso-8859-15.mo')
ISO_8859-15