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 Codecov PyPI version PythonLicense

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.5.tar.gz (27.5 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.5-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pomo_i18n-0.1.5.tar.gz
  • Upload date:
  • Size: 27.5 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.5.tar.gz
Algorithm Hash digest
SHA256 1bc202ecbbe0b4269e7ae8423d991b537dfac0d55d636cff347d23b2c5febc3f
MD5 3791a8a46b62af718ad7077e289ede2f
BLAKE2b-256 fd040893e43136f53fb1a79e4ef4e3b11fa343c4e2a7df527ef50ec098e38ee5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pomo_i18n-0.1.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: pomo_i18n-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0003b75e7e0f10fca99294b550e64f172f2eb9233ee90d2a469d0fd968f97e9c
MD5 ceb62cc5f343db5858e2bb23b13fed6e
BLAKE2b-256 7a8ea903a889ea3a9de10cfc32f9c81454e3e6f57213ebb87439eb39bcc8ffa7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pomo_i18n-0.1.5-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