A lightweight, zero-dependency Python library for internationalization and translation management.
Project description
๐ TransX
๐ A lightweight, zero-dependency Python internationalization library that supports Python 2.7 through 3.12.
The API is designed to be DCC-friendly, for example, works with Maya, 3DsMax, Houdini, etc.
โจ Features
TransX provides a comprehensive set of features for internationalization:
- ๐ Zero Dependencies: No external dependencies required
- ๐ Python Support: Full support for Python 2.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: Named, positional and ${var} style parameters
- ๐ซ Variable Support: Environment variable expansion support
- โก Performance: High-speed and thread-safe operations
- ๐ก๏ธ Error Handling: Comprehensive error management with fallbacks
- ๐งช Testing: 100% test coverage with extensive cases
- ๐ Auto Translation: Built-in Google Translate API support
- ๐ฅ DCC Support: Tested with Maya, 3DsMax, Houdini, etc.
- ๐ Extensible: Pluggable custom text interpreters system
- ๐จ Flexible Formatting: Support for various string format styles
- ๐ Runtime Switching: Dynamic locale switching at runtime
๐ Language Code Support
TransX provides flexible language code handling with automatic normalization. The library supports multiple formats for language codes, making it easy to use in different contexts.
Supported Language Codes
| Language | Standard Code | Alternative Formats |
|---|---|---|
| Chinese (Simplified) | zh_CN |
zh-CN, zh_Hans, Chinese, Chinese Simplified |
| Japanese | ja_JP |
ja, Japanese |
| Korean | ko_KR |
ko, Korean |
| English | en_US |
en, English |
| French | fr_FR |
fr, French |
| Spanish | es_ES |
es, Spanish |
| German | de_DE |
de, German |
| Italian | it_IT |
it, Italian |
| Russian | ru_RU |
ru, Russian |
For a complete list of supported languages, refer to the language code documentation.
from transx import TransX
tx = TransX()
# All these formats are valid:
tx.current_locale = "zh-CN" # Hyphen format
tx.current_locale = "zh_CN" # Underscore format
tx.current_locale = "zh" # Language only
tx.current_locale = "Chinese" # Language name
๐ Quick Start
๐ฅ Installation
pip install transx
๐ Basic Usage
from transx import TransX
# Initialize with locale directory
tx = TransX(locales_root="./locales")
# Basic translation
print(tx.tr("Hello")) # Output: ไฝ ๅฅฝ
# Translation with parameters
print(tx.tr("Hello {name}!", name="ๅผ ไธ")) # Output: ไฝ ๅฅฝ ๅผ ไธ๏ผ
# Context-based translation
print(tx.tr("Open", context="button")) # ๆๅผ
print(tx.tr("Open", context="menu")) # ๆๅผๆไปถ
# Switch language at runtime
tx.current_locale = "ja_JP"
print(tx.tr("Hello")) # Output: ใใใซใกใฏ
๐ Advanced Parameter Substitution
# Named parameters
tx.tr("Welcome to {city}, {country}!", city="ๅไบฌ", country="ไธญๅฝ")
# Positional parameters
tx.tr("File {0} of {1}", 1, 10)
# Dollar sign variables (useful in shell-like contexts)
tx.tr("Current user: ${USER}") # Supports ${var} syntax
tx.tr("Path: $HOME/documents") # Supports $var syntax
# Escaping dollar signs
tx.tr("Price: $$99.99") # Outputs: Price: $99.99
๐ Available Locales
TransX provides a convenient way to get a list of available locales in your project:
from transx import TransX
tx = TransX(locales_root="./locales")
# Get list of available locales
print(f"Available locales: {tx.available_locales}") # e.g. ['en_US', 'zh_CN', 'ja_JP']
# Check if a locale is available before switching
if "zh_CN" in tx.available_locales:
tx.current_locale = "zh_CN"
The available_locales property returns a sorted list of locale codes that:
- Have a valid locale directory structure (
LC_MESSAGESfolder) - Contain either
.poor.motranslation files - Are ready to use for translation
This is useful for:
- Building language selection interfaces
- Validating locale switches
- Checking translation file completeness
- Displaying supported languages to users
๐ ๏ธ Command Line Interface
TransX provides a powerful CLI for translation management:
Extract Messages
# Extract from a single file
transx extract app.py -o messages.pot
# Extract from a directory with project info
transx extract ./src -o messages.pot -p "MyProject" -v "1.0"
# Extract and specify languages
transx extract ./src -l "en_US,zh_CN,ja_JP"
Update PO Files
# Update or create PO files for specific languages
transx update messages.pot -l "zh_CN,ja_JP,ko_KR"
# Auto-discover and update all language files
transx update messages.pot
# Update with custom output directory
transx update messages.pot -o ./locales
Compile MO Files
# Compile a single PO file
transx compile path/to/messages.po
# Compile all PO files in a directory
transx compile -d ./locales
# Compile multiple specific files
transx compile file1.po file2.po
List Available Locales
# List all available locales in default directory
transx list
# List locales in a specific directory
transx list -d /path/to/locales
Common Options
-d, --directory: Specify working directory-o, --output: Specify output file/directory-l, --languages: Comma-separated list of language codes-p, --project: Project name (for POT generation)-v, --version: Project version (for POT generation)
For detailed help on any command:
transx <command> --help
๐ฏ 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
TransX provides comprehensive error handling with fallback mechanisms:
from transx import TransX
from transx.exceptions import LocaleNotFoundError, TranslationError
# Enable strict mode for development
tx = TransX(strict_mode=True)
try:
tx.load_catalog("invalid_locale")
except LocaleNotFoundError as e:
print(f"โ Locale error: {e.message}")
try:
result = tx.translate("Hello", target_lang="invalid")
except TranslationError as e:
print(f"โ Translation failed: {e.message}")
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ Project Structure
transx/
โโโ transx/ # Main package directory
โ โโโ api/ # Public API modules
โ โ โโโ locale.py # Locale handling
โ โ โโโ mo.py # MO file operations
โ โ โโโ po.py # PO file operations
โ โ โโโ translate.py # Translation services
โ โโโ core.py # Core functionality
โ โโโ cli.py # Command-line interface
โ โโโ constants.py # Constants and configurations
โ โโโ exceptions.py # Custom exceptions
โโโ tests/ # Test directory
โโโ examples/ # Example code and usage
โโโ nox_actions/ # Nox automation scripts
โโโ docs/ # Documentation
โโโ pyproject.toml # Project configuration
โโโ noxfile.py # Test automation configuration
โก Performance Features
- ๐ Uses compiled MO files for optimal speed
- ๐พ Automatic translation caching
- ๐ Thread-safe for concurrent access
- ๐ Minimal memory footprint
- ๐ Automatic PO to MO compilation
๐ง Development Setup
- Clone the repository:
git clone https://github.com/loonghao/transx.git
cd transx
- Install development dependencies:
pip install -r requirements-dev.txt
๐ Development Workflow
We use Nox to automate development tasks. Here are the main commands:
# Run linting
nox -s lint
# Fix linting issues automatically
nox -s lint-fix
# Run tests
nox -s pytest
๐งช Running Tests
Tests are written using pytest and can be run using nox:
nox -s pytest
For running specific tests:
# Run a specific test file
nox -s pytest -- tests/test_core.py
# Run tests with specific markers
nox -s pytest -- -m "not integration"
๐ Code Quality
We maintain high code quality standards using various tools:
- Linting: We use ruff and isort for code linting and formatting
- Type Checking: Static type checking with mypy
- Testing: Comprehensive test suite with pytest
- Coverage: Code coverage tracking with coverage.py
- CI/CD: Automated testing and deployment with GitHub Actions
๐ Documentation
Documentation is written in Markdown and is available in:
- README.md: Main documentation
- examples/: Example code and usage
- API documentation in source code
๐ค Contributing Guidelines
- Fork the repository
- Create a new branch for your feature
- Make your changes
- Run tests and linting
- Submit a pull request
Please ensure your PR:
- Passes all tests
- Includes appropriate documentation
- Follows our code style
- Includes test coverage for new features
๐ 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.4.0.tar.gz.
File metadata
- Download URL: transx-0.4.0.tar.gz
- Upload date:
- Size: 46.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ac6617ab255379f0fa911ce73737dab82a065c7af6b6f7c23a1096105595a71
|
|
| MD5 |
6a46a9f858eb17b301daf47f30e74b8d
|
|
| BLAKE2b-256 |
d75b0801e563e2fcae0dc973c87c2702ecd8d63520b9bf3748a7a12c97b1578b
|
Provenance
The following attestation bundles were made for transx-0.4.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.4.0.tar.gz -
Subject digest:
0ac6617ab255379f0fa911ce73737dab82a065c7af6b6f7c23a1096105595a71 - Sigstore transparency entry: 153600970
- Sigstore integration time:
-
Permalink:
loonghao/transx@cf6e7289f7372a81adaa924eb611fdc0ed9a4402 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/loonghao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@cf6e7289f7372a81adaa924eb611fdc0ed9a4402 -
Trigger Event:
push
-
Statement type:
File details
Details for the file transx-0.4.0-py2.py3-none-any.whl.
File metadata
- Download URL: transx-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 50.8 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 |
0944145eadd8c5aa55d45f2ed1795f5e3f2cd095d5eef6306caa342d4b061524
|
|
| MD5 |
c4b8f31ec281958806ef9d454b9eab49
|
|
| BLAKE2b-256 |
0c40e2464631be3e18b17d9922ed2d9be25dbccd99dda960230ca14826f9e971
|
Provenance
The following attestation bundles were made for transx-0.4.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.4.0-py2.py3-none-any.whl -
Subject digest:
0944145eadd8c5aa55d45f2ed1795f5e3f2cd095d5eef6306caa342d4b061524 - Sigstore transparency entry: 153600974
- Sigstore integration time:
-
Permalink:
loonghao/transx@cf6e7289f7372a81adaa924eb611fdc0ed9a4402 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/loonghao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@cf6e7289f7372a81adaa924eb611fdc0ed9a4402 -
Trigger Event:
push
-
Statement type: