Typing Stubs for polib

1.2.0.20260408 · active · verified Thu Apr 16

types-polib is a PEP 561-compliant type stub package that provides static type annotations for the polib library. polib is a pure Python library designed to manipulate, create, and modify gettext files (.pot, .po, and .mo files) used for internationalization and localization. This types-polib package, part of the larger typeshed project, enables static type checkers like MyPy, Pyright, or PyCharm to verify type correctness in code that uses polib, without altering runtime behavior. The package version 1.2.0.20260408 aims to provide accurate annotations for polib versions 1.2.*. Releases are frequent, reflecting updates from the typeshed project.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `polib` library to create and manipulate .po files. When `types-polib` is installed alongside `polib`, static type checkers (like MyPy, Pyright, or PyCharm) will automatically use the provided type hints to analyze this code for type consistency and potential errors, without requiring any changes to the import statements.

import polib

# Create a new PO file
pofile = polib.POFile()
pofile.metadata = {
    'Project-Id-Version': 'My Project 1.0',
    'Report-Msgid-Bugs-To': 'you@example.com',
    'POT-Creation-Date': '2023-01-01 00:00+0000',
    'PO-Revision-Date': '2023-01-01 00:00+0000',
    'Last-Translator': 'Your Name <you@example.com>',
    'Language-Team': 'English <en@example.com>',
    'MIME-Version': '1.0',
    'Content-Type': 'text/plain; charset=utf-8',
    'Content-Transfer-Encoding': '8bit',
}

# Add an entry
entry = polib.POEntry(
    msgid='Hello world!',
    msgstr='Hola mundo!',
    occurrences=[('main.py', '10')],
    comment='A greeting message'
)
pofile.append(entry)

# Add another entry with plural forms
entry_plural = polib.POEntry(
    msgid='{} item',
    msgid_plural='{} items',
    msgstr_plural={0: '{} artículo', 1: '{} artículos'},
    occurrences=[('items.py', '25')],
    comment='Number of items'
)
pofile.append(entry_plural)

# Save the PO file
# For a real application, replace '/tmp/output.po' with a proper path.
# Ensure the directory exists or handle FileNotFoundError.
try:
    # Using a temporary file for demonstration purposes
    import tempfile
    with tempfile.NamedTemporaryFile(suffix='.po', mode='w', delete=False, encoding='utf-8') as f:
        temp_po_path = f.name
    pofile.save(temp_po_path)
    print(f"PO file saved to: {temp_po_path}")
    # Optionally compile to MO
    temp_mo_path = temp_po_path.replace('.po', '.mo')
    pofile.save_as_mofile(temp_mo_path)
    print(f"MO file compiled to: {temp_mo_path}")
finally:
    # Cleanup temporary files (optional, for real use you'd keep them)
    import os
    if 'temp_po_path' in locals() and os.path.exists(temp_po_path):
        os.remove(temp_po_path)
    if 'temp_mo_path' in locals() and os.path.exists(temp_mo_path):
        os.remove(temp_mo_path)

# Type checkers will use 'types-polib' to ensure correct usage of polib objects.
# For example, they will verify that 'append' takes a POEntry and 'msgid' is a string.

view raw JSON →