A library to manipulate gettext files (po and mo files).
—
Complete functionality for loading PO/POT files, MO files, and converting between formats. Includes automatic encoding detection, file format validation, and comprehensive file I/O operations.
Load and parse PO (Portable Object) or POT (Portable Object Template) files from filesystem paths or string content.
def pofile(pofile, **kwargs):
"""
Convenience function that parses the po or pot file and returns a POFile instance.
Parameters:
- pofile (str): Full or relative path to the po/pot file or its content (data)
- wrapwidth (int, optional): The wrap width, default 78
- encoding (str, optional): The encoding to use, default None (auto-detected)
- check_for_duplicates (bool, optional): Whether to check for duplicates, default False
- klass (class, optional): Class to instantiate return value, default None (POFile)
Returns:
POFile: A POFile instance containing the parsed entries
"""Usage Examples:
import polib
# Load from file path
po = polib.pofile('messages.po')
# Load with custom encoding
po = polib.pofile('messages.po', encoding='utf-8')
# Load with duplicate checking enabled
po = polib.pofile('messages.po', check_for_duplicates=True)
# Load from string content
po_content = '''
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\\n"
msgid "Hello"
msgstr "Hola"
'''
po = polib.pofile(po_content)Load and parse binary MO (Machine Object) files from filesystem paths or binary content.
def mofile(mofile, **kwargs):
"""
Convenience function that parses the mo file and returns a MOFile instance.
Parameters:
- mofile (str): Full or relative path to the mo file or its content (string or bytes)
- wrapwidth (int, optional): The wrap width, default 78
- encoding (str, optional): The encoding to use, default None (auto-detected)
- check_for_duplicates (bool, optional): Whether to check for duplicates, default False
- klass (class, optional): Class to instantiate return value, default None (MOFile)
Returns:
MOFile: A MOFile instance containing the parsed entries
"""Usage Examples:
import polib
# Load from file path
mo = polib.mofile('messages.mo')
# Load with custom settings
mo = polib.mofile('messages.mo', encoding='utf-8', wrapwidth=100)
# Load from binary content
with open('messages.mo', 'rb') as f:
mo_content = f.read()
mo = polib.mofile(mo_content)Save POFile instances to filesystem in PO format with complete metadata and formatting preservation.
class POFile(list):
def save(self, fpath=None, repr_method='__unicode__', newline=None):
"""
Save the PO file to disk.
Parameters:
- fpath (str, optional): File path to save to, uses self.fpath if None
- repr_method (str, optional): String representation method, default '__unicode__'
- newline (str, optional): Line ending style, default None (system default)
Returns:
None
"""
def save_as_mofile(self, fpath):
"""
Save the PO file as a binary MO file.
Parameters:
- fpath (str): Path where the MO file will be saved
Returns:
None
"""Usage Examples:
import polib
# Load and modify PO file
po = polib.pofile('messages.po')
entry = po.find('Hello')
if entry:
entry.msgstr = 'Hola'
# Save back to original file
po.save()
# Save to different file
po.save('messages_es.po')
# Convert and save as MO file
po.save_as_mofile('messages.mo')Save MOFile instances to filesystem in binary MO format or convert to PO format.
class MOFile(list):
def save(self, fpath=None):
"""
Save the MO file to disk in binary format.
Parameters:
- fpath (str, optional): File path to save to, uses self.fpath if None
Returns:
None
"""
def save_as_pofile(self, fpath):
"""
Save the MO file as a PO file.
Parameters:
- fpath (str): Path where the PO file will be saved
Returns:
None
"""Usage Examples:
import polib
# Load MO file
mo = polib.mofile('messages.mo')
# Save back to MO format
mo.save()
# Save to different MO file
mo.save('messages_backup.mo')
# Convert and save as PO file
mo.save_as_pofile('recovered.po')Access file metadata through dedicated methods that provide structured access to header information.
class POFile(list):
def metadata_as_entry(self):
"""
Return the file metadata as a POEntry instance.
Returns:
POEntry: Entry containing metadata with empty msgid and metadata as msgstr
"""
def ordered_metadata(self):
"""
Return ordered metadata as list of tuples.
Returns:
list: List of (metadata_name, metadata_value) tuples in order
"""Usage Examples:
import polib
po = polib.pofile('messages.po')
# Get metadata as POEntry
metadata_entry = po.metadata_as_entry()
print(metadata_entry.msgstr) # Contains formatted metadata
# Get ordered metadata tuples
metadata_list = po.ordered_metadata()
for name, value in metadata_list:
print(f"{name}: {value}")
# Example output:
# Content-Type: text/plain; charset=UTF-8
# Language: es_ES
# Last-Translator: John Doe <john@example.com>Access and modify file-level properties and metadata for both PO and MO files.
# POFile properties
class POFile(list):
fpath: str # File path
wrapwidth: int # Line wrap width (default 78)
encoding: str # File encoding
check_for_duplicates: bool # Whether to check for duplicates when adding entries
header: str # File header string
metadata: dict # Metadata dictionary (e.g., {'Language': 'es', 'MIME-Version': '1.0'})
metadata_is_fuzzy: bool # Whether metadata header is marked as fuzzy
# MOFile properties
class MOFile(list):
fpath: str # File path
wrapwidth: int # Line wrap width
encoding: str # File encoding
magic_number: int # MO file magic number (0x950412de)
version: int # MO file versionUsage Examples:
import polib
# Work with PO file metadata
po = polib.pofile('messages.po')
# Access metadata
print(po.metadata) # {'Language': 'es', 'Content-Type': 'text/plain; charset=UTF-8', ...}
print(po.encoding) # 'utf-8'
print(po.header) # Full header string
# Modify metadata
po.metadata['Last-Translator'] = 'John Doe <john@example.com>'
po.metadata['Language'] = 'es_ES'
# Work with MO file properties
mo = polib.mofile('messages.mo')
print(f"MO file magic number: {hex(mo.magic_number)}")
print(f"MO file version: {mo.version}")
# File paths
print(po.fpath) # Path to the loaded PO file
print(mo.fpath) # Path to the loaded MO fileConvert POFile instances to binary format for MO file generation.
class POFile(list):
def to_binary(self):
"""
Return the binary representation of the PO file (MO format).
Returns:
bytes: Binary MO file content
"""Usage Examples:
import polib
# Load PO file and get binary representation
po = polib.pofile('messages.po')
binary_data = po.to_binary()
# Write binary data to MO file manually
with open('messages.mo', 'wb') as f:
f.write(binary_data)
# This is equivalent to:
po.save_as_mofile('messages.mo')Install with Tessl CLI
npx tessl i tessl/pypi-polib