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: auto-i18n-lib | 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 auto-i18n-lib

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.2.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.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: auto_i18n_lib-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2051bb95fbfc55e4fbae9d5f56d8b45cc2ea11b03261e2733b113e072de8b4ba
MD5 3133d924a29740ae0a09ed20888f9926
BLAKE2b-256 9969a19619a9e712c1cacdb3827325723c1a1d00bd1054f02dffe47f431a660c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: auto_i18n_lib-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 55d409137c816edf6a6474b2ce9beea1b86fd297940c81dba915a748b3f57fe0
MD5 a78646808dc07af6436b312e0985cc33
BLAKE2b-256 af42822e9534d7d96fb942326872225e8a8e033f1f2d742a5f913881868b2265

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