Skip to content

MukundaKatta/TransLang

Repository files navigation

TransLang

CI Python 3.10+ License: MIT PyPI version

Multi-language text translator — a Python library with a built-in dictionary for common word and phrase translations between English and several languages (Spanish, French, German). Uses word-by-word lookup with phrase matching.


Architecture

graph TD
    A[User Code] -->|translate / detect / batch| B[TransLang Core]
    B --> C[Built-in Dictionaries]
    B --> D[Phrase Matcher]
    B --> E[Language Detector]
    C --> F[EN-ES Dictionary]
    C --> G[EN-FR Dictionary]
    C --> H[EN-DE Dictionary]
    D --> C
    E --> C
    B --> I[Config / Pydantic Models]
    I --> J[TranslationConfig]
    I --> K[TranslationResult]
Loading

Quickstart

Installation

pip install translang

Or install from source:

git clone https://github.com/officethree/TransLang.git
cd TransLang
pip install -e ".[dev]"

Basic Usage

from translang import TransLang

tl = TransLang()

# Translate English to Spanish
result = tl.translate("hello world", source="en", target="es")
print(result.translated_text)  # "hola mundo"

# Detect language
lang = tl.detect_language("bonjour le monde")
print(lang)  # "fr"

# Batch translate
results = tl.translate_batch(
    ["hello", "goodbye", "thank you"],
    source="en",
    target="de"
)
for r in results:
    print(r.translated_text)

# Get supported languages
langs = tl.get_supported_languages()
print(langs)  # ["en", "es", "fr", "de"]

# Look up a single word
word = tl.lookup_word("cat", source="en", target="fr")
print(word)  # "chat"

# Add custom dictionary entries
tl.add_dictionary("en", "es", {"laptop": "portátil", "keyboard": "teclado"})

# Get translation stats
stats = tl.get_stats()
print(stats)

Configuration

from translang import TransLang
from translang.config import TranslationConfig

config = TranslationConfig(
    default_source="en",
    default_target="es",
    case_sensitive=False,
    fallback_to_original=True,
)

tl = TransLang(config=config)
result = tl.translate("good morning")
print(result.translated_text)  # "buenos días"

Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
make test

# Lint
make lint

# Format
make format

Inspired by NLP translation trends

TransLang draws inspiration from modern NLP translation trends, providing a lightweight dictionary-based approach that works offline without requiring large ML models. Ideal for educational tools, simple localization tasks, and rapid prototyping.


Built by Officethree Technologies | Made with love and AI

About

Multi-language text translator — dictionary-based word/phrase translation for English, Spanish, French, German

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors