๐ TransX
English | ็ฎไฝไธญๆ
๐ 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
- ๐จ Flexible Formatting: Multiple string formatting styles
- ๐ Runtime Switching: Dynamic locale switching at runtime
- ๐ง Qt Integration: Built-in support for Qt translations
- ๐ Message Extraction: Advanced source code message extraction with context
- ๐ Multi-App Support: Multiple translation instances for different apps
GNU gettext Compatibility
TransX is fully compatible with the GNU gettext standard, providing seamless integration with existing translation workflows:
- Standard Formats: Full support for
.po
and .mo
file formats according to GNU gettext specifications
- File Structure: Follows the standard locale directory structure (
LC_MESSAGES/domain.{po,mo}
)
- Header Support: Complete support for gettext headers and metadata
- Plural Forms: Compatible with gettext plural form expressions and handling
- Context Support: Full support for msgctxt (message context) using gettext standard separators
- Encoding: Proper handling of character encodings as specified in PO/MO headers
- Tools Integration: Works with standard gettext tools (msgfmt, msginit, msgmerge, etc.)
- Binary Format: Implements the official MO file format specification with both little and big endian support
This means you can:
- Use existing PO editors like Poedit, Lokalize, or GTranslator
- Integrate with established translation workflows
- Migrate existing gettext-based translations seamlessly
- Use standard gettext tools alongside TransX
- Maintain compatibility with other gettext-based systems
๐ Quick Start
๐ฅ Installation
pip install transx
๐ Basic Usage
from transx import TransX
tx = TransX(locales_root="./locales")
print(tx.tr("Hello"))
print(tx.tr("Hello {name}!", name="ๅผ ไธ"))
print(tx.tr("Open", context="button"))
print(tx.tr("Open", context="menu"))
tx.switch_locale("ja_JP")
print(tx.tr("Hello"))
๐ Translation API
TransX provides two main methods for translation with different levels of functionality:
tr() - High-Level Translation API
The tr()
method is the recommended high-level API that provides all translation features:
tx.tr("Hello")
tx.tr("Hello {name}!", name="ๅผ ไธ")
tx.tr("Open", context="button")
tx.tr("Open", context="menu")
tx.tr("Home: $HOME")
tx.tr("Price: $$99.99")
tx.tr("Welcome to ${city}, {country}!", city="ๅไบฌ", country="ไธญๅฝ")
translate() - Low-Level Translation API
The translate()
method is a lower-level API that provides basic translation and parameter substitution:
tx.translate("Hello")
tx.translate("Open", context="button")
tx.translate("Hello {name}!", name="ๅผ ไธ")
The main differences between tr()
and translate()
:
Basic Translation | โ
| โ
|
Context Support | โ
| โ
|
Parameter Substitution | โ
| โ
|
Environment Variables | โ
| โ |
${var} Style Variables | โ
| โ |
$$ Escaping | โ
| โ |
Interpreter Chain | โ
| โ |
Choose tr()
for full functionality or translate()
for simpler use cases where you only need basic translation and parameter substitution.
๐ Advanced Parameter Substitution
tx.tr("Welcome to {city}, {country}!", city="ๅไบฌ", country="ไธญๅฝ")
tx.tr("File {0} of {1}", 1, 10)
tx.tr("Current user: ${USER}")
tx.tr("Path: $HOME/documents")
tx.tr("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")
print(f"Available locales: {tx.available_locales}")
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_MESSAGES
folder)
- Contain either
.po
or .mo
translation 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 command-line interface for common translation tasks. When no arguments are provided for commands, TransX will use the ./locales
directory in your current working directory as the default path.
transx extract
transx extract . --output ./locales/messages.pot
transx update
transx update ./locales
transx compile
transx compile ./locales
The default working directory structure:
./
โโโ locales/ # Default translation directory
โโโ messages.pot # Extracted messages template
โโโ en/ # English translations
โ โโโ LC_MESSAGES/
โ โโโ messages.po
โ โโโ messages.mo
โโโ zh_CN/ # Chinese translations
โโโ LC_MESSAGES/
โโโ messages.po
โโโ messages.mo
transx extract app.py -o messages.pot
transx extract ./src -o messages.pot -p "MyProject" -v "1.0"
transx extract ./src -l "en_US,zh_CN,ja_JP"
Update PO Files
transx update messages.pot -l "zh_CN,ja_JP,ko_KR"
transx update messages.pot
transx update messages.pot -o ./locales
Compile MO Files
transx compile path/to/messages.po
transx compile -d ./locales
transx compile file1.po file2.po
List Available Locales
transx list
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
๐ฅ๏ธ Qt Usage
TransX can be used with Qt applications in two ways:
Basic Integration
Use TransX directly in your Qt application:
from PySide2.QtWidgets import QMainWindow
from transx import get_transx_instance
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tx = get_transx_instance("myapp")
self.setWindowTitle(self.tx.tr("My Application"))
file_menu = self.menuBar().addMenu(self.tx.tr("&File"))
file_menu.addAction(self.tx.tr("&Open"))
file_menu.addAction(self.tx.tr("&Save"))
Qt Translator Integration
For Qt's built-in translation system, you'll need to:
- First convert your .po files to .qm format using Qt's lrelease tool
- Install the .qm files using TransX's Qt extension
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QTranslator
from transx.extensions.qt import install_qt_translator
app = QApplication([])
translator = QTranslator()
install_qt_translator(app, translator, "zh_CN", "./translations")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Application")
Converting .po to .qm files:
lrelease translations/zh_CN/LC_MESSAGES/messages.po -qm translations/qt_zh_CN.qm
Note: The lrelease
tool is part of Qt's Linguist tools:
The Qt integration supports:
- Loading .qm format translation files
- Multiple translator instances
- Note: Qt's built-in tr() function requires .qm files and won't work with .mo files
Extract translatable messages from your source code with powerful context support:
from transx.api.pot import PotExtractor
extractor = PotExtractor(pot_file="messages.pot")
extractor.add_source_file("app.py")
extractor.add_source_file("utils.py")
extractor.add_source_directory("src")
extractor.save_pot(
project="MyApp",
version="1.0.0",
copyright_holder="Your Name",
bugs_address="your.email@example.com"
)
๐ Multi-App Support
Manage multiple translation instances for different applications or components:
from transx import get_transx_instance
app1 = get_transx_instance("app1", default_locale="en_US")
app2 = get_transx_instance("app2", default_locale="zh_CN")
app1.tr("Hello")
app2.tr("Hello")
app1.switch_locale("ja_JP")
app2.switch_locale("ko_KR")
Multi-app support features:
- Independent translation catalogs
- Separate locale settings per instance
- Thread-safe operation
๐ค Context-Based Translations
print(tx.tr("Open", context="button"))
print(tx.tr("Open", context="menu"))
print(tx.tr("Post", context="verb"))
print(tx.tr("Post", context="noun"))
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
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}")
๐ Performance
TransX is designed with performance in mind. We continuously monitor and optimize its performance through automated benchmarks.
View our performance benchmarks at: TransX Benchmarks
Our benchmark suite includes:
- Translation lookup performance
- Parameter substitution performance
- Locale switching performance
- Cache efficiency
- Memory usage
- Concurrent operations
- And more...
๐ ๏ธ Development
๐ง Environment Setup
git clone https://github.com/loonghao/transx.git
cd transx
- Install development dependencies:
pip install -r requirements-dev.txt
๐ฆ Project Structure
TransX follows a well-organized package structure:
transx/
โโโ transx/ # Main package directory
โ โโโ __init__.py # Package initialization
โ โโโ __version__.py # Version information
โ โโโ api/ # Public API modules
โ โ โโโ __init__.py
โ โ โโโ mo.py # MO file operations
โ โ โโโ po.py # PO file operations
โ โ โโโ pot.py # POT file operations
โ โโโ app.py # Application management
โ โโโ cli.py # Command-line interface
โ โโโ constants.py # Constants and configurations
โ โโโ context/ # Translation context management
โ โ โโโ __init__.py
โ โ โโโ manager.py # Context manager implementation
โ โโโ core.py # Core functionality
โ โโโ exceptions.py # Custom exceptions
โ โโโ extensions/ # Framework integrations
โ โ โโโ __init__.py
โ โ โโโ qt.py # Qt support
โ โโโ internal/ # Internal implementation details
โ โโโ __init__.py
โ โโโ compat.py # Python 2/3 compatibility
โ โโโ filesystem.py # File system operations
โ โโโ logging.py # Logging utilities
โโโ examples/ # Example code
โโโ locales/ # Translation files
โโโ tests/ # Test suite
โโโ nox_actions/ # Nox automation scripts
โโโ CHANGELOG.md # Version history
โโโ LICENSE # MIT License
โโโ README.md # English documentation
โโโ README_zh.md # Chinese documentation
โโโ noxfile.py # Test automation config
โโโ pyproject.toml # Project configuration
โโโ requirements.txt # Production dependencies
โโโ requirements-dev.txt # Development dependencies
๐ Development Workflow
We use Nox to automate development tasks. Here are the main commands:
nox -s lint
nox -s lint-fix
nox -s pytest
๐งช Running Tests
Tests are written using pytest and can be run using nox:
nox -s pytest
For running specific tests:
nox -s pytest -- tests/test_core.py
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.