Dynamic HTML translation with OpenAI
Project description
autoi18n
Runtime HTML i18n with OpenAI: translate the page on first request, cache results to JSON, reuse on subsequent requests, and auto-sync when the source changes.
PyPI name: autoi18n | Import: autoi18n
Features
- 🔁 On-the-fly translation of raw HTML strings
- 🗂️ JSON cache per target language (on disk)
- 🔄 Auto-sync: stale entries removed when source changes
- 🧩 Chunking for model context limits (long pages split safely)
- 🛡️ Preserves
<script>/<style>as-is - 🎛️ Concise UI translation (e.g., buttons)
- 🌐 Helpers: detect browser lang, pick alternate lang
Installation
pip install autoi18n
Quick start
from autoi18n import Translator
html_in = "<h1>Добро пожаловать</h1><p>Это тест.</p>"
tr = Translator(
source_lang="ru",
cache_dir="./translations", # JSON cache folder
# api_key="sk-..." # or use OPENAI_API_KEY env var
)
html_out = tr.translate_html(html_in, target_lang="en", page_name="page")
print(html_out)
First call translates via OpenAI and writes ./translations/page.en.json.
Next calls reuse the cache and only translate new/changed strings.
Environment
OPENAI_API_KEY — used if api_key not passed to Translator.
API
Translator(source_lang="ru", cache_dir="./translations", api_key=None)
translate_html(html: str, target_lang: str, page_name: str = "page") -> str
Translates visible text nodes, preserves script/style, chunks long text, updates cache JSON.
translate_text(text: str, target_lang: str, page_name: str = "page") -> str
Low-level single-string translation with caching.
detect_browser_lang(accept_language_header: str) -> str
Parses Accept-Language → short code like ru, en, fr.
get_alternative_lang(current: str, browser_lang: str) -> str
Returns alternate code for a language toggle.
Cache layout
translations/ └─ page.en.json # UTF-8 JSON: { "source": "translated", ... } Example:
{
"Добро пожаловать": "Welcome",
"Выберите язык:": "Choose a language:"
}
Minimal FastAPI wiring (example) python
from fastapi import FastAPI, Request
from autoi18n import Translator
from pathlib import Path
app = FastAPI()
tr = Translator(source_lang="ru", cache_dir="./translations")
HTML_PATH = Path("index.html")
@app.get("/")
def home():
return HTML_PATH.read_text(encoding="utf-8")
@app.get("/detect_lang")
def detect_lang(request: Request):
return {"lang": tr.detect_browser_lang(request.headers.get("accept-language", ""))}
@app.get("/alt_lang")
def alt_lang(request: Request, current: str = "ru"):
browser = tr.detect_browser_lang(request.headers.get("accept-language", ""))
return {"lang": tr.get_alternative_lang(current, browser)}
@app.get("/translate")
def translate(lang: str = "en"):
html = HTML_PATH.read_text(encoding="utf-8")
return tr.translate_html(html, target_lang=lang, page_name="page")
Notes & limits
You control OpenAI usage/billing.
Chunking keeps requests within model limits; HTML structure preserved.
Short labels (buttons) use a concise prompt to avoid verbose text.
This package is for runtime translation with caching; static catalogs are out of scope.
Versioning
Semantic Versioning: MAJOR.MINOR.PATCH.
License
MIT © BONA
Support
Issues & feature requests → GitHub Issues (see project URLs in metadata).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auto_i18n_lib-0.1.1.tar.gz.
File metadata
- Download URL: auto_i18n_lib-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80ffa4b1979b5c1dae38bb692b256164081c4a13ebfab38deb61853a37da295d
|
|
| MD5 |
8a15536bff1d3c1431d44c25d71b56ec
|
|
| BLAKE2b-256 |
05164766ac70ef8c33122f089d36514abf0e15bfffd5ad7acb5516e71948e4d1
|
File details
Details for the file auto_i18n_lib-0.1.1-py3-none-any.whl.
File metadata
- Download URL: auto_i18n_lib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40d7087f1a538c79a3e8fee91e029cab8e43c84fd61380e7ffa3379501b87d79
|
|
| MD5 |
ade4ab63ea92b82cba3ddb8a975edbaa
|
|
| BLAKE2b-256 |
450a638bb4651e1f052fc2a27ba3d2290f1ef7f13a03d20c41aa7ebbdc6d1c31
|