A lightweight, zero-dependency Python library for internationalization and translation management.
Project description
๐ TransX
๐ A lightweight, zero-dependency Python internationalization library that supports Python 3.7 through 3.12.
โจ Features
| Feature | Description |
|---|---|
| ๐ Zero Dependencies | No external dependencies required |
| ๐ Python Support | Full support for Python 3.7-3.12 |
| ๐ Context-based | Accurate translations with context support |
| ๐ฆ Standard Format | Compatible with gettext .po/.mo files |
| ๐ฏ Simple API | Clean and intuitive interface |
| ๐ Auto Management | Automatic translation file handling |
| ๐ String Extraction | Built-in source code string extraction |
| ๐ Unicode | Complete Unicode support |
| ๐ Parameters | Dynamic parameter substitution |
| โก Performance | High-speed and thread-safe operations |
| ๐ก๏ธ Error Handling | Comprehensive error management |
| ๐งช Testing | Extensive test coverage |
๐ Quick Start
๐ฅ Installation
pip install transx
๐ Basic Usage
from transx import TransX
# Initialize translator
tx = TransX(locales_root='locales')
tx.current_locale = 'zh_CN'
# Basic translation
print(tx.tr('Hello')) # Output: ไฝ ๅฅฝ
# Translation with context
print(tx.tr('Open', context='button')) # Output: ๆๅผ
print(tx.tr('Open', context='menu')) # Output: ๆๅผๆไปถ
# Translation with parameters
print(tx.tr('Hello {name}!', name='ๅผ ไธ')) # Output: ไฝ ๅฅฝ ๅผ ไธ๏ผ
๐ ๏ธ Command Line Interface
TransX comes with powerful CLI tools for translation management:
๐ค Extract Messages
# Extract from a single file
transx extract app.py
# Extract from a directory with custom options
transx extract ./src \
--output locales/custom.pot \
--project "My Project" \
--version "1.0"
๐ Update Translations
# Update multiple languages
transx update locales/messages.pot en zh_CN ja_JP
# Custom output directory
transx update messages.pot en zh_CN --output-dir ./translations
โ๏ธ Compile Translations
# Compile translations
transx compile locales/*/LC_MESSAGES/messages.po
๐ Project Structure
your_project/
โโโ ๐ locales/
โ โโโ ๐ zh_CN/
โ โ โโโ ๐ LC_MESSAGES/
โ โ โโโ ๐ messages.po # Source translations
โ โ โโโ ๐ฆ messages.mo # Compiled translations
โ โโโ ๐ ja_JP/
โ โโโ ๐ LC_MESSAGES/
โ โโโ ๐ messages.po
โ โโโ ๐ฆ messages.mo
โโโ ๐ your_code.py
๐ฏ Advanced Features
๐ Context-Based Translations
# UI Context
print(tx.tr('Open', context='button')) # ๆๅผ
print(tx.tr('Open', context='menu')) # ๆๅผๆไปถ
# Part of Speech
print(tx.tr('Post', context='verb')) # ๅๅธ
print(tx.tr('Post', context='noun')) # ๆ็ซ
# Scene Context
print(tx.tr('Welcome', context='login')) # ๆฌข่ฟ็ปๅฝ
print(tx.tr('Welcome', context='home')) # ๆฌข่ฟๅๆฅ
๐ก๏ธ Error Handling
from transx.exceptions import LocaleNotFoundError, CatalogNotFoundError
try:
tx.current_locale = 'invalid_locale'
except LocaleNotFoundError:
print("โ Locale not found")
try:
tx.load_catalog('missing_catalog.mo')
except CatalogNotFoundError:
print("โ Catalog not found")
๐ Multiple Catalogs
tx = TransX()
tx.load_catalog('path/to/main.mo') # Main catalog
tx.load_catalog('path/to/extra.mo') # Extra translations
โก Performance Tips
- ๐ Uses compiled MO files for optimal speed
- ๐พ Automatic translation caching
- ๐ Thread-safe for concurrent access
- ๐ Minimal memory footprint
๐ค Contributing
Contributions are welcome! Here's how you can help:
- ๐ Report bugs
- ๐ก Suggest features
- ๐ Improve documentation
- ๐ง Submit pull requests
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file transx-0.1.0.tar.gz.
File metadata
- Download URL: transx-0.1.0.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eef0e4392d51a15ab450ec9bedc51219115ac83c17bf26c58136533fdf12791b
|
|
| MD5 |
de9d47ce1432cdc229c56ba3caafbf71
|
|
| BLAKE2b-256 |
aa88f0841b16c901e639a600724bb500958a6ffc8de8f61c494d5d8c5a30e7cc
|
Provenance
The following attestation bundles were made for transx-0.1.0.tar.gz:
Publisher:
python-publish.yml on loonghao/transx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
transx-0.1.0.tar.gz -
Subject digest:
eef0e4392d51a15ab450ec9bedc51219115ac83c17bf26c58136533fdf12791b - Sigstore transparency entry: 152701695
- Sigstore integration time:
-
Permalink:
loonghao/transx@ce361beb3424175e1cde98b7681968d9c00a781c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/loonghao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce361beb3424175e1cde98b7681968d9c00a781c -
Trigger Event:
push
-
Statement type:
File details
Details for the file transx-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: transx-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
681f586763ca9f23ff5bee6c398b2f6d59b7f6f2fce6bb80011373f0f8e32149
|
|
| MD5 |
c66b66ae0ba0b9453312371d58e25148
|
|
| BLAKE2b-256 |
65fb5f56392ae99dc9d6bc21fc8b0a3d8d9d0b24470d30f206fbc0f989367c1a
|
Provenance
The following attestation bundles were made for transx-0.1.0-py2.py3-none-any.whl:
Publisher:
python-publish.yml on loonghao/transx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
transx-0.1.0-py2.py3-none-any.whl -
Subject digest:
681f586763ca9f23ff5bee6c398b2f6d59b7f6f2fce6bb80011373f0f8e32149 - Sigstore transparency entry: 152701697
- Sigstore integration time:
-
Permalink:
loonghao/transx@ce361beb3424175e1cde98b7681968d9c00a781c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/loonghao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce361beb3424175e1cde98b7681968d9c00a781c -
Trigger Event:
push
-
Statement type: