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).

Changelog

0.1.3

  • Improved filtering of non-translatable strings (GUID, numbers, logins).
  • Cache cleanup now removes junk entries automatically.
  • Added detailed debug logging for should_translate and cleanup_cache.

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.3.tar.gz (6.2 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.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: auto_i18n_lib-0.1.3.tar.gz
  • Upload date:
  • Size: 6.2 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.3.tar.gz
Algorithm Hash digest
SHA256 fbf10a2849f762a7852a4c040bb1689965258398dcf37495215ea22edcf477cf
MD5 b1e52075cf973c438a1cdb6509244413
BLAKE2b-256 8570bb5e676e090013485de7ba7a6e79796fe3169ba5e57e84e4e681a60af577

See more details on using hashes here.

File details

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

File metadata

  • Download URL: auto_i18n_lib-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5b6a448539ada468abad2b201898d735e33e072f1f08441c94a8520879bcd4c8
MD5 3f0e66579764032d214dbe3d8d5001ea
BLAKE2b-256 58e91ebfe1eea08646f7be340487f811030c2fa545b9a56121cd6894899469cb

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