Skip to main content

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


Download files

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

Source Distribution

auto_i18n_lib-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

auto_i18n_lib-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file auto_i18n_lib-0.1.0.tar.gz.

File metadata

  • Download URL: auto_i18n_lib-0.1.0.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

Hashes for auto_i18n_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 05a16d406417f6e1b959e565ab43c720501f103dbf2b67ea1161f95ee24173da
MD5 3c0d0dd453ece496959c4a66ab81c40e
BLAKE2b-256 c335900f485091a0dbb392fcef3b5fee0aed6cde088aad5f85f86a89ae987a7f

See more details on using hashes here.

File details

Details for the file auto_i18n_lib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: auto_i18n_lib-0.1.0-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

Hashes for auto_i18n_lib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ce0d391d7144f846de5c6ae40fe7cb1b40c046eeea82bc86f6cdf26a7aec8c7
MD5 4fa366680842fae2fcae251a4303e8c2
BLAKE2b-256 29ca97343c3f8227853786dc5315b81ffdc106b1b672b4d5ea0b38ccd1a0c6a3

See more details on using hashes here.

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