Files
ai-agent/backend/plugins/hubi18n.py
gabrielvidalandClaude Opus 5.5 961b5929f5 feat(plugins): a plugin's own strings — the i18n contract, and the i18n.read bridge for auto-translate
A plugin's translations now live in that plugin: "i18n": "i18n" in plugin.json
→ i18n/en.json (the English source list, generated by the plugins repo's
scripts/plugin_i18n.py) + i18n/<lang>.json (English → translation).

- plugins/plugin_i18n.py: validation (the folder, en.json an array, each
  <lang>.json an object; every file's language added to `languages`),
  per-language coverage (missing / stale / placeholder mismatches), the bundle.
- GET /api/plugins/i18n/{lang}: every active plugin's dictionary in one ETag'd
  body (a guest: their installs + the managed plugins granted to them).
- Frames: init.i18n + init.locale, aiAgentPlugin.t / tn (English fallback).
- Manifest labels (nav, drawers, header, Files/Projects/Dashboards, settings
  subtitle + fields) through pluginT: plugin dictionary → core t() → English.
- Handlers get AIA_LANGS (the hub's languages); they read their own file.
- /api/plugins/{id}/info|settings carry `i18n` coverage; the plugin page shows
  "Its strings in <language>" rows.
- plugins/hubi18n.py: the owner-only i18n.read bridge (operator approval) —
  translations' and plugins' coverage, files only — for the auto-translate plugin.
- docs/plugins.md ("A plugin's own strings"), CLAUDE.md, i18n README; openapi +
  generated types, authz golden, ui key list regenerated.

Changelog
- Added: plugins can ship their own translations (i18n/<lang>.json), shown per language on their page.
- Added: the i18n.read bridge the auto-translate plugin uses to keep translations complete.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:58:08 +01:00

86 lines
3.4 KiB
Python

"""``i18n.read`` — what every translation and every plugin's own dictionaries
are missing (since 2026-11-28, the auto-translate plugin).
One owner-only bridge, operator approval:
* ``GET /proxy/tools/plugins/{pid}/hub/i18n`` → ``{languages, translations,
plugins}``:
- ``languages``: the enabled translations' languages (``code``, ``name``,
``englishName``, ``flag``, ``pluginId``) — what a plugin's own strings can
be translated into;
- ``translations``: each of the owner's ``kind: "translation"`` installs,
enabled or not, with its coverage against the core's English
(``plugins/translations.coverage``: ``ui`` / ``backend`` parts + ``missing``);
- ``plugins``: each of the owner's other installs — ``id``, ``name``,
``enabled``, ``languages``, ``repoPath`` (``plugins/<dir>`` when it comes
from an ``ai-agent-plugins`` checkout, else None) and ``i18n``: its own
coverage per language (``plugins/plugin_i18n.coverage``), None for a
plugin that doesn't declare ``i18n``.
Files only — no model call, no transcript. A guest, or a plugin without the
capability, gets a 403 ``capability``.
"""
from __future__ import annotations
import re
from typing import Any
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from starlette.concurrency import run_in_threadpool
from plugins import plugin_i18n, translations
from plugins.proxyroutes import _owner_bridge
from plugins.store import OWNER_UID
router = APIRouter(include_in_schema=False)
CAP = "i18n.read"
_REPO_RE = re.compile(r"/ai-agent-plugins[^/]*/(plugins/[A-Za-z0-9_.-]+)/?$")
def repo_path(d: str | None) -> str | None:
m = _REPO_RE.search(str(d or ""))
return m.group(1) if m else None
def build(deps) -> dict[str, Any]:
store = deps.plugin_store
cur = translations.current()
languages = [{**t.public(), "pluginId": t.plugin_id} for t in cur.by_lang.values()]
trs: list[dict[str, Any]] = []
plugins: list[dict[str, Any]] = []
for pub in store.list_for(OWNER_UID):
rec = store.get_raw(OWNER_UID, pub["id"])
if not rec:
continue
mf = rec.get("manifest") or {}
enabled = bool(rec.get("enabled")) and not rec.get("needsApproval")
row = {"id": rec["id"], "name": mf.get("name") or rec["id"], "enabled": enabled,
"version": mf.get("version") or "", "languages": mf.get("languages") or ["en"],
"repoPath": repo_path(rec.get("dir"))}
if mf.get("kind") == "translation":
t = translations._load(rec)
tv = mf.get("translation") or {}
held = cur.get(tv.get("lang"))
trs.append({**row, "lang": tv.get("lang"), "flag": tv.get("flag"),
"langName": tv.get("name"),
"active": bool(held and held.plugin_id == rec["id"]),
"coverage": translations.coverage(t) if t is not None else None})
else:
plugins.append({**row, "kind": mf.get("kind") or "app",
"i18n": plugin_i18n.coverage(rec)})
return {"languages": languages, "translations": trs, "plugins": plugins}
@router.get("/proxy/tools/plugins/{pid}/hub/i18n")
async def plugin_hub_i18n(pid: str, request: Request):
"""``i18n.read``: the translations' and the plugins' missing strings.
Owner only."""
deps, err = _owner_bridge(request, pid, CAP)
if err:
return err
return JSONResponse(await run_in_threadpool(build, deps))