Skip to main content

Translation services

Project description

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.

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

tilsit_i18n-1.0.0.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

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

tilsit_i18n-1.0.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file tilsit_i18n-1.0.0.tar.gz.

File metadata

  • Download URL: tilsit_i18n-1.0.0.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for tilsit_i18n-1.0.0.tar.gz
Algorithm Hash digest
SHA256 75014336a73bd5f9d03eb1ad8cd9b54a54ebc1af39f14cac9140e98ccdc0fa7d
MD5 a59cc3f10f82ad682c342b56c634fcc2
BLAKE2b-256 e47a1be1c170bdbff19f4c25e37e43d052a8e297dd9b24b0b876f166d8561e44

See more details on using hashes here.

File details

Details for the file tilsit_i18n-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tilsit_i18n-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c888baaeba3e6c84e71cc4c299ec5caa96c797908def5b309a4b71aed2d1b02
MD5 812e969f96ef39f16c6d8b9b544d2cde
BLAKE2b-256 47d3d8d56b26ae1b9900bc25364ddf1ceeaf63609bb86ef65abbfcc2cc5300dc

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