Skip to main content

tilsit-i18n

tilsit-i18n is a lightweight wrapper around Python’s built‑in gettext module. Its purpose is to make internationalization simple and runtime‑switchable with a single global translation object.

from pathlib import Path
from tilsit_i18n import tr

tr.localedir = Path(__file__).parent / "locale"
tr.language = "pl"
print(tr("Hello"))

✨ Features

  • Simple, minimalistic interface: use tr("message") anywhere
  • Runtime language switching (tr.language = "pl")
  • Built on gettext — full .po/.mo compatibility
  • Safe fallbacks:
    • English ("en") as default
    • NullTranslations when .mo not found
  • Automatic logging of missing translation files
  • Zero dependencies beyond Python + loguru

📦 Installation

pip install tilsit-i18n

🧩 Usage

Initialize the translation directory

from pathlib import Path
from tilsit_i18n import tr

tr.localedir = Path(__file__).parent / "locale"

Set language

tr.language = "pl"

Translate messages

print(tr("Hello world"))

When the language is "en" (default), the message is returned unchanged.


🔧 Generating .mo files

Translations are edited in .po files and compiled to .mo.

Compile:

msgfmt.py -o messages.mo messages

🧪 Example .po

msgid "Hello"
msgstr "Cześć"

msgid "Exit"
msgstr "Wyjście"

⚠️ Error handling

tilsit-i18n logs common issues:

  • Missing localedir
  • Missing .mo file for selected language

Example log:

ERROR localedir is not initialized
ERROR .mo file is not found

💡 Internals (how it works)

The core object:

@dataclass
class Translation:
    default_language = "en"
    default_domain = "messages"

    localedir: Path | None = None
    _language: str = "en"
    _gnutranslations = gettext.NullTranslations()

It:

  • Tracks language
  • Loads the correct GNUTranslations object
  • Falls back to English when needed
  • Is callable: tr("message")

🤝 Contributing

Issues, pull requests and suggestions are welcome!


📝 License

MIT License — see LICENSE for details.

Metadata

Release files for tilsit-i18n 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tilsit-i18n 1.0.0
File Size Uploaded
tilsit_i18n-1.0.0.tar.gz 2.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tilsit-i18n 1.0.0
File Interpreter ABI Platform
tilsit_i18n-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.1 kB

Release files / tilsit_i18n-1.0.0.tar.gz

Download URL tilsit_i18n-1.0.0.tar.gz
Size 2.1 kB
Tags Source
SHA-256 checksum
How to use checksums
75014336a73bd5f9d03eb1ad8cd9b54a54ebc1af39f14cac9140e98ccdc0fa7d
BLAKE2b-256 checksum
How to use checksums
e47a1be1c170bdbff19f4c25e37e43d052a8e297dd9b24b0b876f166d8561e44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release files / tilsit_i18n-1.0.0-py3-none-any.whl

Download URL tilsit_i18n-1.0.0-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c888baaeba3e6c84e71cc4c299ec5caa96c797908def5b309a4b71aed2d1b02
BLAKE2b-256 checksum
How to use checksums
47d3d8d56b26ae1b9900bc25364ddf1ceeaf63609bb86ef65abbfcc2cc5300dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page