Python examples
Examples using httpx (async-friendly) and the standard urllib for zero-dependency setups.
Install httpx
bash
pip install httpxMinimal helper
python
# i18n.py
import httpx
import os
BASE_URL = "https://sapi.i18nme.com"
HEADERS = {"X-API-Key": os.environ["I18N_API_KEY"]}
def _get(path: str) -> dict:
res = httpx.get(f"{BASE_URL}{path}", headers=HEADERS)
res.raise_for_status()
return res.json()
def manifest():
return _get("/v1/cached/manifest")
def translations(lang: str) -> dict:
return _get(f"/v1/cached/translations/{lang}")
def translation_group(lang: str, group: str) -> dict:
return _get(f"/v1/cached/translations/{lang}/{group}")
def translation_key(lang: str, group: str, key: str) -> str:
return _get(f"/v1/cached/translations/{lang}/{group}/{key}")["value"]Fetch all translations for every language
python
from i18n import manifest, translations
data = manifest()
bundles = {
lang["code"]: translations(lang["code"])
for lang in data["languages"]
}
print(bundles)Async example with httpx
python
import asyncio
import httpx
import os
BASE_URL = "https://sapi.i18nme.com"
HEADERS = {"X-API-Key": os.environ["I18N_API_KEY"]}
async def fetch_all_languages():
async with httpx.AsyncClient(headers=HEADERS) as client:
manifest_res = await client.get(f"{BASE_URL}/v1/cached/manifest")
manifest_res.raise_for_status()
languages = manifest_res.json()["languages"]
tasks = [
client.get(f"{BASE_URL}/v1/cached/translations/{l['code']}")
for l in languages
]
responses = await asyncio.gather(*tasks)
return {
lang["code"]: res.json()
for lang, res in zip(languages, responses)
}
bundles = asyncio.run(fetch_all_languages())Django integration
python
# myapp/i18n_loader.py
import httpx
import os
from django.core.cache import cache
API_KEY = os.environ["I18N_API_KEY"]
BASE_URL = "https://sapi.i18nme.com"
TTL = 120 # seconds
def get_translations(lang: str) -> dict:
cache_key = f"i18n:{lang}"
cached = cache.get(cache_key)
if cached is not None:
return cached
res = httpx.get(
f"{BASE_URL}/v1/cached/translations/{lang}",
headers={"X-API-Key": API_KEY},
timeout=5.0,
)
res.raise_for_status()
data = res.json()
cache.set(cache_key, data, TTL)
return dataZero-dependency (urllib)
python
import json
import os
import urllib.request
def get_translations(lang: str) -> dict:
url = f"https://sapi.i18nme.com/v1/cached/translations/{lang}"
req = urllib.request.Request(url, headers={"X-API-Key": os.environ["I18N_API_KEY"]})
with urllib.request.urlopen(req, timeout=5) as res:
return json.loads(res.read())