Python Translation AI — Translate Python scripts or web pages on the fly to any human language. Features context-aware translations, smart caching, offline mode, and support for 40+ languages.
Available on PyPI: Install with pip install pytlai
Extract text nodes, skip code/scripts, reconstruct with lang/dir attributes.
Translate docstrings and comments while preserving code structure.
Captures surrounding context to disambiguate words like "Run", "Post", "Save".
Memory, Redis, or file-based caching to avoid re-translating identical content.
Export to JSON/YAML/PO files, ship with your project, no AI at runtime.
Full support for major languages including RTL (Arabic, Hebrew, etc.).
Deduplicate and batch API calls for efficiency.
Command-line interface for translating files and exporting to PO format.
pip install pytlai
# With optional dependencies
pip install pytlai[redis] # Redis cache support
pip install pytlai[yaml] # YAML file support
pip install pytlai[all] # All optional dependenciesfrom pytlai import Pytlai
from pytlai.providers import OpenAIProvider
translator = Pytlai(
target_lang="es_ES",
provider=OpenAIProvider(),
)
result = translator.process("<h1>Hello World</h1><p>Welcome to our site.</p>")
print(result.content)
# <html lang="es-ES" dir="ltr"><h1>Hola Mundo</h1><p>Bienvenido a nuestro sitio.</p></html>from pytlai import Pytlai
from pytlai.providers import OpenAIProvider
translator = Pytlai(
target_lang="ja_JP",
provider=OpenAIProvider(),
)
code = '''
def greet(name):
"""Return a greeting message."""
# Build the greeting
return f"Hello, {name}!"
'''
result = translator.process(code, content_type="python")
print(result.content)
# def greet(name):
# """挨拶メッセージを返します。"""
# # 挨拶を作成する
# return f"Hello, {name}!"# Auto-detects content type from extension
result = translator.process_file("app.py")
result = translator.process_file("index.html")# Translate a file
pytlai translate index.html -l es_ES -o index_es.html
# Translate Python script
pytlai translate app.py -l fr_FR -o app_fr.py -t python
# Extract strings for translation
pytlai extract app.py -o strings.json
# Export to gettext PO format
pytlai export translations.json -o locale/es.po -f poShip translations with your project without runtime AI dependencies.
from pytlai import Pytlai
from pytlai.providers import OpenAIProvider
from pytlai.export import TranslationExporter
translator = Pytlai(target_lang="es_ES", provider=OpenAIProvider())
result = translator.process(content)
# Export translations
exporter = TranslationExporter()
exporter.export_from_cache(translator._cache, "locale/es_ES.json", target_lang="es_ES")from pytlai import Pytlai
from pytlai.cache import FileCache
translator = Pytlai(
target_lang="es_ES",
cache=FileCache("locale/es_ES.json"),
provider=None, # No AI provider needed
)
result = translator.process(content)Single words can translate differently depending on context. pytlai automatically captures surrounding information to help the AI choose the correct translation.
"Run" in <button class="execute-btn">→ "Ejecutar" (execute)"Run" in a sports article→ "Correr" (physical running)"Post" in a blog interface→ "Publicar" (publish)"Post" in mail context→ "Correo" (postal mail)"Save" in a file dialog→ "Guardar" (save file)"Save" in a banking app→ "Ahorrar" (save money)This happens automatically — no configuration needed.
from pytlai import Pytlai, TranslationConfig, PythonOptions
from pytlai.providers import OpenAIProvider
from pytlai.cache import RedisCache
config = TranslationConfig(
target_lang="fr_FR",
source_lang="en",
excluded_terms=["API", "SDK", "pytlai"], # Never translate these
context="Technical documentation for a Python library",
python_options=PythonOptions(
translate_docstrings=True,
translate_comments=True,
translate_strings=False,
),
)
translator = Pytlai(
config=config,
provider=OpenAIProvider(model="gpt-4o"),
cache=RedisCache(url="redis://localhost:6379"),
)English, German, Spanish, French, Italian, Japanese, Portuguese, Chinese (Simplified/Traditional)
Arabic, Bengali, Czech, Danish, Greek, Finnish, Hebrew, Hindi, Hungarian, Indonesian, Korean, Dutch, Norwegian, Polish, Romanian, Russian, Swedish, Thai, Turkish, Ukrainian, Vietnamese
Bulgarian, Catalan, Persian, Croatian, Lithuanian, Latvian, Malay, Slovak, Slovenian, Serbian, Swahili, Filipino, Urdu
Pytlai(
target_lang: str, # Target language code (e.g., "es_ES")
provider: AIProvider = None, # AI provider (OpenAIProvider, etc.)
cache: TranslationCache = None, # Cache backend
config: TranslationConfig = None,
source_lang: str = "en",
excluded_terms: list[str] = None,
context: str = None,
)process(content, content_type=None) → ProcessedContent — Translate content
process_file(path, content_type=None) → ProcessedContent — Translate a file
translate_text(text) → str — Translate a single string
ProcessedContent(
content: str, # Translated content
translated_count: int, # Number of newly translated items
cached_count: int, # Number of cache hits
total_nodes: int, # Total translatable nodes found
)