ushka.web.i18n.tools
Tools behind ushka i18n: find messages, write a .pot, compile .po to .mo.
Standard library plus Jinja2 only (its babel_extract reads templates, no
Babel needed). The .po reader and .mo writer are deliberately small:
they handle what real catalogs contain (context, plural forms, multi-line
strings, escapes, fuzzy and obsolete entries) and nothing more.
In this module
class Message | |
def extract_python() | (line, message) for every literal _("...")-style call in source. |
def extract_template() | The same for a Jinja template: _(), ngettext()... and {% trans %} blocks. |
def extract() | Every message under root: .py files anywhere and templates in template_dirs. |
def write_pot() | The text of a .pot template for messages. |
def parse_po() | {key: value} ready for a .mo: the entries that are translated and not fuzzy. |
def write_mo() | A GNU .mo file for entries (sorted by key, as the format requires). |
def compile_po() | Compiles po_path (UTF-8) into a .mo next to it; raises ValueError naming the file and line on bad input. |
class Message
def extract_python
def extract_python(
source: str,
filename: str = '<string>',
) -> list[tuple[int, Message]](line, message) for every literal _("...")-style call in source.
def extract_template
def extract_template(source: str) -> list[tuple[int, Message]]The same for a Jinja template: _(), ngettext()... and {% trans %} blocks.
def extract
def extract(
root: Path,
template_dirs: tuple[str, ...] = ('templates',),
skip: tuple[Path, ...] = (),
) -> list[Message]Every message under root: .py files anywhere and templates in template_dirs.
def write_pot
def write_pot(messages: list[Message], project: str = 'PROJECT') -> strThe text of a .pot template for messages.
def parse_po
def parse_po(text: str) -> dict[str, str]{key: value} ready for a .mo: the entries that are translated and not fuzzy.
A message with context has the key context\x04msgid; one with a plural,
msgid\x00plural and a value of the forms joined by \x00. The header
(empty msgid) is always kept, even when marked fuzzy.
def write_mo
def write_mo(entries: dict[str, str]) -> bytesA GNU .mo file for entries (sorted by key, as the format requires).
def compile_po
def compile_po(po_path: Path, mo_path: Path | None = None) -> PathCompiles po_path (UTF-8) into a .mo next to it; raises ValueError naming the file and line on bad input.