Skip to main content

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.

Python Version Nox PyPI Version Downloads Downloads Downloads License PyPI Format Maintenance


โœจ Features

Feature Description
๐Ÿš€ 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.
๐Ÿ“ Project Structure Well-organized and maintainable codebase
๐Ÿ”Œ Extensible Pluggable custom text interpreters system
๐ŸŽจ Flexible Formatting Support for various string format styles
๐Ÿ”„ Runtime Switching Dynamic locale switching at runtime

๐Ÿ“ Project Structure

transx/
โ”œโ”€โ”€ transx/                 # Main package directory
โ”‚   โ”œโ”€โ”€ api/               # Public API implementations
โ”‚   โ”œโ”€โ”€ internal/          # Internal implementation details
โ”‚   โ”œโ”€โ”€ core.py           # Core functionality
โ”‚   โ”œโ”€โ”€ cli.py            # Command-line interface
โ”‚   โ”œโ”€โ”€ constants.py      # Constants and configurations
โ”‚   โ””โ”€โ”€ exceptions.py     # Custom exceptions
โ”œโ”€โ”€ examples/              # Example code and usage demos
โ”œโ”€โ”€ tests/                # Test suite
โ”œโ”€โ”€ pyproject.toml        # Project configuration
โ””โ”€โ”€ noxfile.py           # Test automation configuration

๐Ÿš€ 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

๐ŸŒ Environment Variable Support

# Environment variables are automatically expanded
tx.tr("User home: ${HOME}")
tx.tr("Current path: ${PATH}")

# Mix with translation parameters
tx.tr("Welcome {name} to ${HOSTNAME}!", name="John")

๐ŸŽฏ Context-Aware Translation

# Same string, different contexts
tx.tr("Open", context="button")      # Translation for button label
tx.tr("Open", context="menu")        # Translation for menu item
tx.tr("Open", context="file_state")  # Translation for file status

# Context with parameters
tx.tr("Created {count} items", count=5, context="notification")

๐Ÿ› ๏ธ Advanced API Usage

๐Ÿ”Œ Custom Interpreter System

TransX provides a powerful and flexible interpreter system that allows you to customize text processing:

from transx import TextInterpreter, InterpreterExecutor

# Create a custom interpreter
class MyCustomInterpreter(TextInterpreter):
    name = "custom"
    description = "My custom text processor"

    def interpret(self, text, context=None):
        # Add your custom text processing logic here
        return text.replace("old", "new")

# Use built-in interpreters
tx = TransX(locales_root="./locales")
executor = tx.get_interpreter_executor()

# Add your custom interpreter
executor.add_interpreter(MyCustomInterpreter())

# Built-in interpreters include:
# - TextTypeInterpreter: Ensures correct text encoding
# - DollarVariableInterpreter: Handles ${var} style variables
# - ParameterSubstitutionInterpreter: Handles {name} style parameters
# - EnvironmentVariableInterpreter: Expands environment variables
# - TranslationInterpreter: Handles text translation

# Chain multiple interpreters
executor = InterpreterExecutor([
    MyCustomInterpreter(),
    tx.get_interpreter("env"),     # Environment variables
    tx.get_interpreter("dollar"),  # ${var} style variables
])

# Execute the interpreter chain
result = executor.execute("Hello ${USER}", {"name": "world"})

# Safe execution with fallback
result = executor.execute_safe(
    "Hello ${USER}",
    fallback_interpreters=[tx.get_interpreter("parameter")]
)

The interpreter system follows a chain-of-responsibility pattern where each interpreter can:

  • Transform text in its own specific way
  • Pass context information between interpreters
  • Be ordered to control processing sequence
  • Fail safely without affecting other interpreters
  • Have fallback options for robustness

Message Extraction and PO/MO File Management

from transx.api.pot import PotExtractor
from transx.api.po import POFile
from transx.api.mo import compile_po_file

# Extract messages from source code
extractor = PotExtractor(project="MyProject", version="1.0")
extractor.scan_file("app.py")
extractor.save("messages.pot")

# Create/Update PO file
po = POFile("zh_CN/LC_MESSAGES/messages.po")
po.add_translation("Hello", "ไฝ ๅฅฝ")
po.add_translation("Welcome", "ๆฌข่ฟŽ", context="greeting")
po.save()

# Compile PO to MO
compile_po_file("zh_CN/LC_MESSAGES/messages.po", "zh_CN/LC_MESSAGES/messages.mo")

Automatic Translation with Google Translate

from transx.api.translate import GoogleTranslator, translate_po_files

# Initialize Google Translator
translator = GoogleTranslator()

# Create and auto-translate PO files for multiple languages
translate_po_files(
    pot_file_path="messages.pot",
    languages=["zh_CN", "ja_JP", "ko_KR"],
    output_dir="locales",
    translator=translator
)

Implementing Custom Translation API

You can implement your own translation API by inheriting from the Translator base class:

from transx.api.translate import Translator
from transx.api.translate import translate_po_files


class MyCustomTranslator(Translator):
    def translate(self, text, source_lang="auto", target_lang="en"):
        """Implement your custom translation logic.

        Args:
            text (str): Text to translate
            source_lang (str): Source language code (default: auto)
            target_lang (str): Target language code (default: en)

        Returns:
            str: Translated text
        """
        # Add your translation logic here
        # For example, calling your own translation service:
        return my_translation_service.translate(
            text=text,
            from_lang=source_lang,
            to_lang=target_lang
        )


# Use your custom translator
translator = MyCustomTranslator()
translate_po_files(
    pot_file_path="messages.pot",
    languages=["zh_CN", "ja_JP"],
    translator=translator
)

๐Ÿ”Œ Implementing Custom Translation API

TransX provides a flexible way to implement your own translation service. You can either:

  • ๐Ÿ”ง Implement your own translation logic
  • ๐Ÿ”— Integrate with third-party translation libraries
  • ๐ŸŒ Use direct HTTP requests to translation services

Basic Implementation

The simplest way is to inherit from the Translator base class:

from transx.api.translate import Translator
from transx.api.translate import translate_po_files


class MyCustomTranslator(Translator):
    def translate(self, text, source_lang="auto", target_lang="en"):
        """Implement your custom translation logic.

        Args:
            text (str): Text to translate
            source_lang (str): Source language code (default: auto)
            target_lang (str): Target language code (default: en)

        Returns:
            str: Translated text
        """
        # Add your translation logic here
        return my_translation_service.translate(
            text=text,
            from_lang=source_lang,
            to_lang=target_lang
        )


# Use your custom translator
translator = MyCustomTranslator()
translate_po_files(
    pot_file_path="messages.pot",
    languages=["zh_CN", "ja_JP"],
    translator=translator
)

๐Ÿš€ Using Third-Party Libraries

For faster implementation, you can integrate with existing translation libraries:

๐Ÿ“š deep-translator (Python 3.x only)
from deep_translator import GoogleTranslator as DeepGoogleTranslator
from transx.api.translate import Translator

class DeepTranslator(Translator):
    """A powerful translator using deep-translator library.

    Supported Services:
    โœจ Google Translate
    โœจ DeepL
    โœจ Microsoft Translator
    โœจ PONS
    โœจ Linguee
    โœจ MyMemory
    And more...
    """
    def translate(self, text, source_lang="auto", target_lang="en"):
        try:
            # Convert language codes (e.g., zh_CN -> zh-cn)
            source = source_lang.lower().replace('_', '-')
            target = target_lang.lower().replace('_', '-')

            translator = DeepGoogleTranslator(
                source=source if source != "auto" else "auto",
                target=target
            )
            return translator.translate(text)
        except Exception as e:
            raise TranslationError(f"Translation failed: {str(e)}")
๐Ÿ“ฆ translate (Python 2.7 compatible)
from translate import Translator as PyTranslator
from transx.api.translate import Translator

class SimpleTranslator(Translator):
    """A lightweight translator using the 'translate' library.

    Supported Services:
    โœจ Google Translate
    โœจ MyMemory
    โœจ Microsoft Translator
    """
    def translate(self, text, source_lang="auto", target_lang="en"):
        try:
            translator = PyTranslator(from_lang=source_lang, to_lang=target_lang)
            return translator.translate(text)
        except Exception as e:
            raise TranslationError(f"Translation failed: {str(e)}")

โš ๏ธ Python 2.7 Compatibility Note

While TransX supports Python 2.7 through 3.12, many modern translation libraries have dropped Python 2.7 support. Here are your options:

  1. ๐Ÿ“ฆ Use older versions of translation libraries that still support Python 2.7
  2. ๐ŸŒ Implement a simple HTTP client to directly call translation APIs
  3. โœจ Use TransX's built-in GoogleTranslator which maintains Python 2.7 compatibility
๐Ÿ”ง HTTP Client Example (Python 2.7 compatible)
import requests
from transx.api.translate import Translator
from transx.compat import urlencode

class SimpleGoogleTranslator(Translator):
    """A basic Google Translate implementation using requests."""

    def translate(self, text, source_lang="auto", target_lang="en"):
        try:
            # Convert language codes
            source = source_lang.lower().replace('_', '-')
            target = target_lang.lower().replace('_', '-')

            # Build URL
            params = {
                'sl': source,
                'tl': target,
                'q': text
            }
            url = 'https://translate.googleapis.com/translate_a/single?' + urlencode({
                'client': 'gtx',
                'dt': 't',
                **params
            })

            # Make request
            response = requests.get(url)
            data = response.json()

            # Extract translation
            return ''.join(part[0] for part in data[0])

        except Exception as e:
            raise TranslationError(f"Translation failed: {str(e)}")

๐ŸŒ Language Code Support

TransX provides flexible language code handling with automatic normalization:

๐Ÿ“ Example Usage
from transx import TransX

tx = TransX()

# Different language code formats are supported:
tx.current_locale = "zh-CN"    # Hyphen format
tx.current_locale = "zh_CN"    # Underscore format
tx.current_locale = "zh"       # Language only (will use default country code)
tx.current_locale = "Chinese"  # Language name

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

๐Ÿ”„ Default Behavior

TransX handles language codes in the following way:

  1. ๐Ÿ” Attempts to detect the system language automatically
  2. ๐Ÿ”„ Normalizes language codes to standard format (e.g., zh-CN โ†’ zh_CN)
  3. โšก Falls back to en_US if the system language is not supported or cannot be detected

๐Ÿ› ๏ธ 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
transx extract ./src -o messages.pot -p "MyProject" -v "1.0"

Update PO Files

# Update or create PO files for specific languages
transx update messages.pot -l zh_CN ja_JP ko_KR

# Auto-translate during update
transx update messages.pot -l zh_CN ja_JP ko_KR --translate

Compile MO Files

# Compile a single PO file
transx compile locales/zh_CN/LC_MESSAGES/messages.po

# Compile all PO files in a directory
transx compile locales

๐ŸŒ Supported Languages

The Google Translator supports a wide range of languages. Here are some commonly used language codes:

  • Chinese (Simplified): zh_CN
  • Japanese: ja_JP
  • Korean: ko_KR
  • French: fr_FR
  • Spanish: es_ES

For a complete list of supported languages, refer to the language code documentation.

๐ŸŽฏ 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 for various scenarios:

from transx import TransX
from transx.exceptions import LocaleNotFoundError, CatalogNotFoundError, TranslationError

# 1. Locale and Catalog Errors
tx = TransX(strict_mode=True)  # Enable strict mode to raise exceptions

try:
    tx.load_catalog("invalid_locale")
except LocaleNotFoundError as e:
    print(f"โŒ Locale error: {e.message}")
    print(f"  Missing locale: {e.locale}")

try:
    tx.load_catalog(None)
except ValueError as e:
    print("โŒ Invalid locale: Locale cannot be None")

# 2. Translation Errors
from transx.api.translate import GoogleTranslator

translator = GoogleTranslator()
try:
    result = translator.translate("Hello", source_lang="invalid", target_lang="zh_CN")
except TranslationError as e:
    print(f"โŒ Translation failed: {e.message}")
    print(f"  Source text: {e.source_text}")
    print(f"  From: {e.source_lang} To: {e.target_lang}")

# 3. File Parsing Errors
from transx.exceptions import ParserError, ValidationError
from transx.api.po import POFile

try:
    po = POFile("invalid.po")
    po.parse()
except ParserError as e:
    print(f"โŒ Parse error in {e.file_path}")
    if e.line_number:
        print(f"  At line: {e.line_number}")
    if e.reason:
        print(f"  Reason: {e.reason}")

# 4. Non-Strict Mode Behavior
tx = TransX(strict_mode=False)  # Default behavior
# These operations will log warnings instead of raising exceptions
tx.load_catalog("missing_locale")  # Returns False, logs warning
tx.tr("missing_key")  # Returns the key itself, logs warning

Key error handling features:

  • ๐Ÿ”’ Strict mode for development/testing
  • ๐Ÿ“ Detailed error messages and context
  • ๐Ÿชต Fallback behavior in non-strict mode
  • ๐Ÿ“‹ Comprehensive logging

๐Ÿ”ง Error Handling

from transx.exceptions import TranslationError, LocaleError

try:
    tx.tr("Hello")
except TranslationError as e:
    print(f"Translation failed: {e}")
except LocaleError as e:
    print(f"Locale error: {e}")

Multiple Catalogs

tx = TransX()
tx.load_catalog("path/to/main.mo")     # Main catalog
tx.load_catalog("path/to/extra.mo")    # Extra translations

โšก 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

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ‘จโ€๐Ÿ’ป Developer Guide

๐Ÿ”ง Development Setup

  1. Clone the repository:
git clone https://github.com/loonghao/transx.git
cd transx
  1. Install development dependencies:
pip install -r requirements-dev.txt

๐Ÿ“ 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
โ”‚   โ””โ”€โ”€ constants.py       # Constants and configurations
โ”œโ”€โ”€ tests/                 # Test directory
โ”œโ”€โ”€ examples/              # Example code and usage
โ”œโ”€โ”€ nox_actions/          # Nox automation scripts
โ”‚   โ”œโ”€โ”€ codetest.py       # Test execution configuration
โ”‚   โ”œโ”€โ”€ lint.py          # Code linting and formatting
โ”‚   โ””โ”€โ”€ utils.py         # Shared utilities and constants
โ””โ”€โ”€ docs/                 # Documentation

๐Ÿ”„ 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

  1. Fork the repository
  2. Create a new branch for your feature
  3. Make your changes
  4. Run tests and linting
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transx-0.2.0.tar.gz (48.6 kB view details)

Uploaded Source

Built Distribution

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

transx-0.2.0-py2.py3-none-any.whl (50.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file transx-0.2.0.tar.gz.

File metadata

  • Download URL: transx-0.2.0.tar.gz
  • Upload date:
  • Size: 48.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for transx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2f9d2a1e90c7623f84b655d9094451f5d1d183fb74d2ee79ce1897d072d6421b
MD5 b04e101f9b592831d43078a4f54508f7
BLAKE2b-256 96e7981fe3bea5262d0f678eb8b03b077b55fe0048839028bcd72a82868a7ad5

See more details on using hashes here.

Provenance

The following attestation bundles were made for transx-0.2.0.tar.gz:

Publisher: python-publish.yml on loonghao/transx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file transx-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: transx-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 50.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for transx-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 32adc71dbcfb5a8744f42718944026361e1f782c3e5a480a98567b2c53aa5ff5
MD5 69a1c4bd539897269af38621d64bf14e
BLAKE2b-256 a269dbee9350c04144141498ca238e14c0dde571b2d3876ef7ae02cc5dca4286

See more details on using hashes here.

Provenance

The following attestation bundles were made for transx-0.2.0-py2.py3-none-any.whl:

Publisher: python-publish.yml on loonghao/transx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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