The core speaks English only; every other language is a kind: "translation" plugin
(translation-<lang>; the French one is translation-fr in ai-agent-plugins, seeded once at start).
- plugins: kind translation (typed_manifest.parse_translation: lang, name, englishName, flag,
locale, promptLine, ui/backend JSON dictionaries — strict, operator-only, in the fingerprint);
a top-level "languages" field on any plugin (default ["en"]) shown on its page; seeding takes
translation-*; plugins/translations.py reads the enabled ones (first by id wins a code).
- core/i18n: tr / langs / localized / clean_pref / effective_lang read the installed
translations; a pick whose translation is gone reads English and is kept; browserLang records
any base code. core/i18n_fr.py and FRENCH_LINE are gone (language_line = the plugin's promptLine).
- The owner picks a language too (users.json ownerLang, PUT /api/me {lang}; English by default).
- Public GET /api/i18n/languages and /api/i18n/{lang} (ETag/304), guest host and sign-in included.
- Coverage: backend/core/i18n_keys/{ui,backend}.json (kept by the two i18n tests,
UPDATE_I18N_KEYS=1); a translation's page shows translated / missing / stale / placeholder
mismatches.
- Frontend: frontend/src/i18n/fr removed; the dictionary comes from the hub, cached in
localStorage and revalidated in the background (first boot in a language waits <= 4 s, any
failure is English); Lang is a string; number/currency style from the translation's locale;
LanguageSelect lists English + installed translations (hidden when none); Settings → Plugins
has a Translations kind (tab, catalog filter, group) and a Translation section with the
missing-strings count; mock is English only.
- Tests: test_i18n.py rewritten on a made-up translation; i18n_testkit.speaking() for the
tests that need a second language. authz golden, openapi.json + src/generated regenerated;
docs/plugins.md (Translations), CLAUDE.md, i18n README, docs/settings.md.
Changelog
- Added: languages are plugins — Settings → Plugins → Translations; the owner can pick a language.
- Changed: French moved to the translation-fr plugin (seeded at start: nothing changes for guests).
- Removed: the core's built-in French dictionaries.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
"""``/api/i18n/*`` — the installed translations, for every browser.
|
|
|
|
The PWA speaks English; another language is a ``translation-<lang>`` plugin
|
|
(``plugins/translations.py``). The sign-in page is translated before anyone
|
|
signs in, so both routes are **public** (``authz/routes.py``), on the guest
|
|
host too, and carry nothing but what the plugins ship:
|
|
|
|
* ``GET /api/i18n/languages`` — the enabled translations: code, own name,
|
|
English name, flag, BCP 47 locale and the ETag of its dictionary. English
|
|
is implied (the core's own).
|
|
* ``GET /api/i18n/{lang}`` — one language's UI dictionary (English text →
|
|
translation), with an ``ETag``; ``If-None-Match`` answers 304. The browser
|
|
keeps a copy (``frontend/src/i18n``) and revalidates it in the background.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
|
|
from fastapi import APIRouter, HTTPException, Request, Response
|
|
|
|
from plugins import translations
|
|
|
|
router = APIRouter()
|
|
|
|
# A dictionary changes only with its plugin: let a browser keep it, but
|
|
# always ask before using it again (the ETag makes that a 304).
|
|
_CACHE = "no-cache"
|
|
_body_cache: dict[tuple[str, str], bytes] = {}
|
|
|
|
|
|
@router.get("/api/i18n/languages")
|
|
def languages(request: Request):
|
|
items = [t.public() for t in translations.current().by_lang.values()]
|
|
body = json.dumps({"default": "en", "languages": items}, ensure_ascii=False).encode()
|
|
etag = '"' + hashlib.sha256(body).hexdigest()[:20] + '"'
|
|
if request.headers.get("if-none-match") == etag:
|
|
return Response(status_code=304, headers={"ETag": etag, "Cache-Control": _CACHE})
|
|
return Response(content=body, media_type="application/json",
|
|
headers={"ETag": etag, "Cache-Control": _CACHE})
|
|
|
|
|
|
@router.get("/api/i18n/{lang}")
|
|
def dictionary(lang: str, request: Request):
|
|
t = translations.current().get(lang)
|
|
if t is None:
|
|
raise HTTPException(404, "no such language")
|
|
etag = f'"{t.etag}"'
|
|
headers = {"ETag": etag, "Cache-Control": _CACHE, "Content-Language": t.locale}
|
|
if request.headers.get("if-none-match") == etag:
|
|
return Response(status_code=304, headers=headers)
|
|
key = (t.lang, t.etag)
|
|
body = _body_cache.get(key)
|
|
if body is None:
|
|
for k in [k for k in _body_cache if k[0] == t.lang]:
|
|
del _body_cache[k] # one copy per language
|
|
body = json.dumps(t.ui, ensure_ascii=False, separators=(",", ":")).encode()
|
|
_body_cache[key] = body
|
|
return Response(content=body, media_type="application/json", headers=headers)
|