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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pomo_i18n-0.1.2.tar.gz
Algorithm Hash digest
SHA256 05aad164544839039129df69d1f29716525fa7a7c4b7d341aec68db47056c264
MD5 a44704e25593d50790e3777d9c23bb43
BLAKE2b-256 401b0c4b25852181c4c0424137f6cd1603b674f5afe8f24212ecaa15d7a28494

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pomo_i18n-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f71f72d9fad2a8986d0a1371eb9cf211cd942d42a28a9cf5240df71eea341092
MD5 88b41a923d0afbaad5af19a7380833dd
BLAKE2b-256 0e642c0535157c14fc4f0bfa4e6c1feded648df6e7d84194b4ea4f0b06fb5e92

See more details on using hashes here.

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