Skip to content

retrieval.po_loader

Per-entry loader for gettext .po files, used to turn a PO repository into a translation memory. Stdlib-only (re, dataclasses, pathlib); no polib.

Each live entry becomes one chunk-granularity Document:

  • docid is "{path}:{start}-{end}"; the span covers the whole entry block (translator comments included), so it is a precise Read target.
  • text is msgctxt, msgid, msgid_plural and msgstr joined by newlines (an untranslated entry contributes only its English text).
  • metadata carries kind ("po_entry"), msgid, msgstr, fuzzy, translated, line (the msgid line) and references, plus msgctxt, msgid_plural and msgstr_plural when present.

The header entry (msgid "") and obsolete #~ entries are never indexed. Fuzzy and untranslated entries are indexed and flagged so callers can filter. .pot templates are not discovered.

The module registers itself as the .po record loader (see record_loaders). text combines msgid and msgstr by design, so entries match English and Chinese queries alike (cross-lingual).

Per-entry loader for gettext .po files (translation-memory indexing).

Stdlib-only (re, dataclasses, pathlib) — no polib. A lenient line parser turns each live PO entry into one chunk-granularity Document so a translator can search msgid/msgstr pairs and Read the exact entry.

Policy:

  • The header entry (msgid "" without msgctxt) is never indexed.
  • Obsolete entries (#~ lines) are never indexed.
  • Fuzzy and untranslated entries are indexed but flagged in metadata so callers can filter.
  • A Document's span (start_line..end_line) covers the whole entry block, translator comments included. context stays empty on purpose so the tree-sitter retriever does not prefix it into the ranked text.

The parser never raises on malformed input; unrecognised lines are skipped.

This module registers itself as the .po record loader (see retrieval.record_loaders) and is listed in record_loaders.BUILTIN_LOADER_MODULES.

is_po_path(path)

True if path has a .po suffix (case-insensitive).

Convenience predicate; the engine itself routes via record_loaders.lookup.

Source code in engine/retrieval/po_loader.py
50
51
52
53
54
55
def is_po_path(path: str) -> bool:
    """True if *path* has a ``.po`` suffix (case-insensitive).

    Convenience predicate; the engine itself routes via ``record_loaders.lookup``.
    """
    return Path(path).suffix.lower() in PO_SUFFIXES

parse_po(text)

Parse PO text into live entries (header included, obsolete excluded).

Source code in engine/retrieval/po_loader.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def parse_po(text: str) -> List[PoEntry]:
    """Parse PO *text* into live entries (header included, obsolete excluded)."""
    entries: List[PoEntry] = []
    block = _Block()

    def flush() -> None:
        nonlocal block
        entry = block.to_entry()
        if entry is not None:
            entries.append(entry)
        block = _Block()

    for lineno, raw in enumerate(_strip_bom(text.splitlines()), start=1):
        line = raw.strip()
        if not line:
            flush()
        elif _handle_line(block, line, lineno):
            flush()
            _handle_line(block, line, lineno)
    flush()
    return entries

po_entries_to_documents(rel_path, text, url='')

One Document per live, non-header entry of the PO file text.

Source code in engine/retrieval/po_loader.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def po_entries_to_documents(rel_path: str, text: str, url: str = "") -> List[Document]:
    """One ``Document`` per live, non-header entry of the PO file *text*."""
    documents: List[Document] = []
    for entry in parse_po(text):
        if entry.is_header:
            continue
        parts = (entry.msgctxt, entry.msgid, entry.msgid_plural, entry.msgstr)
        documents.append(
            Document(
                docid=f"{rel_path}:{entry.start_line}-{entry.end_line}",
                text="\n".join(p for p in parts if p),
                url=url,
                source_path=rel_path,
                start_line=entry.start_line,
                end_line=entry.end_line,
                context="",
                metadata=_entry_metadata(entry),
            )
        )
    return documents

unescape_po_string(quoted_body)

Decode a quoted PO string literal ("...") into its text.

Uses an explicit escape map rather than unicode_escape, which would corrupt non-ASCII (Chinese) text. Unknown escapes keep the backslash.

Source code in engine/retrieval/po_loader.py
64
65
66
67
68
69
70
71
72
73
74
75
def unescape_po_string(quoted_body: str) -> str:
    """Decode a quoted PO string literal (``"..."``) into its text.

    Uses an explicit escape map rather than ``unicode_escape``, which would
    corrupt non-ASCII (Chinese) text. Unknown escapes keep the backslash.
    """
    body = quoted_body.strip()
    if body.startswith('"'):
        body = body[1:]
        if body.endswith('"') and not _ends_with_escaped_quote(body):
            body = body[:-1]
    return _ESCAPE_RE.sub(lambda m: _ESCAPES.get(m.group(1), m.group(0)), body)

Next steps