Skip to main content

Strict-typed, dependency-free gettext-compatible i18n library (PO/MO parser, Plural-Forms evaluator, Catalog API).

Project description

pomo-i18n

English | 日本語

Tests PyPI version License

A minimal, strictly-typed gettext-compatible internationalization library for Python.

pomo-i18n provides .po and .mo parsing, safe plural-form evaluation, and a clean Pythonic API (gettext, ngettext, translation) — all without external dependencies.


Features

  • ✔ Parse .po and .mo files (GNU gettext format)
  • ✔ Full plural-forms support (Plural-Forms: header)
  • ✔ Safe plural expression evaluator (C-style → Python AST)
  • ✔ Strict typing (mypy / Pylance friendly)
  • ✔ Pure Python, no external libraries
  • ✔ Works on Linux / macOS / Windows

Installation

pip install pomo-i18n

Or vendor it directly:

src/
 └─ pypomo/

Documentation


Quick Example

from pypomo.gettext import translation

t = translation(
    domain="messages",
    localedir="locales",
    languages=["en"],
)

_ = t.gettext

print(_("Hello"))
print(t.ngettext("apple", "apples", 1))
print(t.ngettext("apple", "apples", 3))

Expected directory layout:

locales/
 └─ en/
     └─ LC_MESSAGES/
         └─ messages.po

.po Parsing

pomo-i18n contains a robust standalone .po parser:

  • msgid, msgstr

  • plurals ( msgid_plural, msgstr[n] )

  • multiline strings

  • Supports standard gettext comments:

    • # (translator comments)
    • #. (extracted comments)
    • #: (source references)
    • #, flags (parsed as regular comments; fuzzy will be supported in a future release)
  • header extraction (msgid "")


.mo Writing

Compile a Catalog into a .mo file:

from pypomo.catalog import Catalog
from pypomo.mo.writer import write_mo
from pypomo.parser.types import POEntry

entries = [
    POEntry(
        msgid="",
        msgstr=(
            "Language: en\n"
            "Plural-Forms: nplurals=2; plural=(n != 1);\n"
        ),
        msgid_plural=None,
        msgstr_plural={},
        comments=[],
    ),
    POEntry("Hello", "Hello!", None, {}, []),
]

cat = Catalog.from_po_entries(entries)
write_mo("messages.mo", cat)

The generated .mo file is fully compatible with Python’s built-in gettext.GNUTranslations.


.mo Loading

pomo-i18n can also load GNU gettext .mo files back into a Catalog:

from pypomo.mo.loader import load_mo

cat = load_mo("messages.mo")

_ = cat.gettext
print(_("Hello"))

Features:

  • verifies magic number (0x9504120E)
  • parses msgid/msgstr tables
  • handles plural entries correctly
  • extracts Plural-Forms: from the header

Plural Forms

pomo-i18n safely evaluates C-style plural rules:

Example:

Plural-Forms: nplurals=2; plural=(n != 1);

Russian example:

Plural-Forms: nplurals=3;
 plural=(n%10==1 && n%100!=11 ? 0 :
        n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);

Expressions are converted to safe Python AST and evaluated with an isolated environment.


Benchmarks

Plural-rule evaluation is optimized and supports multiple cache backends:

  • none — debugging only
  • weak — dict-based cache
  • lru — fastest (default)

Set via environment variable:

export PYPOMO_CACHE=lru
export PYPOMO_PLURAL_CACHE_SIZE=512

Roadmap

  • Message merging & locale fallbacks
  • Richer comment parsing (#, fuzzy, #. extracted tags)
  • CLI utilities for generating .pot files
  • Locale auto-detection helpers

License

MIT License © 2025 Kiminori Kato


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pomo_i18n-0.1.4.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pomo_i18n-0.1.4-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file pomo_i18n-0.1.4.tar.gz.

File metadata

  • Download URL: pomo_i18n-0.1.4.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pomo_i18n-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c48449c0efaab0eef9501d8b3a9166943acd76a42da9e64bd8e89f7ae59a6ecd
MD5 c21dd3800a479fae1596c4da7218a9fe
BLAKE2b-256 53fa90fd21ea210b42405ebbd7b7d1bd2c2c582eff7151b66ef2ff12f9926e37

See more details on using hashes here.

Provenance

The following attestation bundles were made for pomo_i18n-0.1.4.tar.gz:

Publisher: publish-pypi.yml on kimikato/pomo-i18n

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pomo_i18n-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pomo_i18n-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pomo_i18n-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 15076cb4c962557bfd55f34c34fa5b3d867f85e4328268d9ba42acd1c6c81186
MD5 69800fe1e4b4d0b964bfbd6114b7f7a7
BLAKE2b-256 287e65988c3d8cbdace96085dd1b2c1d60d62dba6f980bd3645746edf0297c79

See more details on using hashes here.

Provenance

The following attestation bundles were made for pomo_i18n-0.1.4-py3-none-any.whl:

Publisher: publish-pypi.yml on kimikato/pomo-i18n

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page