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 3.7 through 3.12.

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 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.


Made with โค๏ธ by the LongHao

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.1.0.tar.gz (20.8 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.1.0-py2.py3-none-any.whl (24.6 kB view details)

Uploaded Python 2Python 3

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

Hashes for transx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eef0e4392d51a15ab450ec9bedc51219115ac83c17bf26c58136533fdf12791b
MD5 de9d47ce1432cdc229c56ba3caafbf71
BLAKE2b-256 aa88f0841b16c901e639a600724bb500958a6ffc8de8f61c494d5d8c5a30e7cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for transx-0.1.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.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

Hashes for transx-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 681f586763ca9f23ff5bee6c398b2f6d59b7f6f2fce6bb80011373f0f8e32149
MD5 c66b66ae0ba0b9453312371d58e25148
BLAKE2b-256 65fb5f56392ae99dc9d6bc21fc8b0a3d8d9d0b24470d30f206fbc0f989367c1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for transx-0.1.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