Module polib :: Class POFile
[hide private]
[frames] | no frames]

Class POFile

source code

object --+        
         |        
      list --+    
             |    
     _BaseFile --+
                 |
                POFile

Po (or Pot) file reader/writer. POFile objects inherit the list objects methods.

**Example**:

>>> po = POFile()
>>> entry1 = POEntry(
...     msgid="Some english text",
...     msgstr="Un texte en anglais"
... )
>>> entry1.occurrences = [('testfile', 12),('another_file', 1)]
>>> entry1.comment = "Some useful comment"
>>> entry2 = POEntry(
...     msgid="Peace in some languages",
...     msgstr="Pace سلام שלום Hasîtî 和平"
... )
>>> entry2.occurrences = [('testfile', 15),('another_file', 5)]
>>> entry2.comment = "Another useful comment"
>>> entry3 = POEntry(
...     msgid='Some entry with quotes " \"',
...     msgstr='Un message unicode avec des quotes " \"'
... )
>>> entry3.comment = "Test string quoting"
>>> po.append(entry1)
>>> po.append(entry2)
>>> po.append(entry3)
>>> po.header = "Some Header"
>>> print(po)
# Some Header
msgid ""
msgstr ""
<BLANKLINE>
#. Some useful comment
#: testfile:12 another_file:1
msgid "Some english text"
msgstr "Un texte en anglais"
<BLANKLINE>
#. Another useful comment
#: testfile:15 another_file:5
msgid "Peace in some languages"
msgstr "Pace سلام שלום Hasîtî 和平"
<BLANKLINE>
#. Test string quoting
msgid "Some entry with quotes \" \""
msgstr "Un message unicode avec des quotes \" \""
<BLANKLINE>
Instance Methods [hide private]
 
__str__(self)
Return the string representation of the po file
source code
 
save_as_mofile(self, fpath)
Save the binary representation of the file to *fpath*.
source code
 
percent_translated(self)
Convenience method that return the percentage of translated messages.
source code
 
translated_entries(self)
Convenience method that return a list of translated entries.
source code
 
untranslated_entries(self)
Convenience method that return a list of untranslated entries.
source code
 
fuzzy_entries(self)
Convenience method that return the list of 'fuzzy' entries.
source code
 
obsolete_entries(self)
Convenience method that return the list of obsolete entries.
source code
 
merge(self, refpot)
XXX this could not work if encodings are different, needs thinking and general refactoring of how polib handles encoding...
source code

Inherited from _BaseFile: __init__, __repr__, find, metadata_as_entry, ordered_metadata, save, to_binary

Inherited from list: __add__, __contains__, __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getitem__, __getslice__, __gt__, __iadd__, __imul__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reversed__, __rmul__, __setitem__, __setslice__, __sizeof__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __subclasshook__

Class Variables [hide private]

Inherited from list: __hash__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__str__(self)
(Informal representation operator)

source code 

Return the string representation of the po file

Overrides: object.__str__

save_as_mofile(self, fpath)

source code 

Save the binary representation of the file to *fpath*.

**Keyword arguments**:

  • *fpath*: string, full or relative path to the file.

percent_translated(self)

source code 

Convenience method that return the percentage of translated messages.

**Example**:

>>> import polib
>>> po = polib.pofile('tests/test_pofile_helpers.po')
>>> po.percent_translated()
50
>>> po = POFile()
>>> po.percent_translated()
100

translated_entries(self)

source code 

Convenience method that return a list of translated entries.

**Example**:

>>> import polib
>>> po = polib.pofile('tests/test_pofile_helpers.po')
>>> len(po.translated_entries())
6

untranslated_entries(self)

source code 

Convenience method that return a list of untranslated entries.

**Example**:

>>> import polib
>>> po = polib.pofile('tests/test_pofile_helpers.po')
>>> len(po.untranslated_entries())
6

fuzzy_entries(self)

source code 

Convenience method that return the list of 'fuzzy' entries.

**Example**:

>>> import polib
>>> po = polib.pofile('tests/test_pofile_helpers.po')
>>> len(po.fuzzy_entries())
2

obsolete_entries(self)

source code 

Convenience method that return the list of obsolete entries.

**Example**:

>>> import polib
>>> po = polib.pofile('tests/test_pofile_helpers.po')
>>> len(po.obsolete_entries())
4

merge(self, refpot)

source code 

XXX this could not work if encodings are different, needs thinking and general refactoring of how polib handles encoding...

Convenience method that merge the current pofile with the pot file provided. It behaves exactly as the gettext msgmerge utility:

  • comments of this file will be preserved, but extracted comments and occurrences will be discarded
  • any translations or comments in the file will be discarded, however dot comments and file positions will be preserved

**Keyword argument**:

  • *refpot*: object POFile, the reference catalog.

**Example**:

>>> import polib
>>> refpot = polib.pofile('tests/test_merge.pot')
>>> po = polib.pofile('tests/test_merge_before.po')
>>> po.merge(refpot)
>>> expected_po = polib.pofile('tests/test_merge_after.po')
>>> unicode(po) == unicode(expected_po)
True