Every `中文 · English` string moves out of ~29 components into web/src/i18n: one Pack type, a verbatim zh pack, and two ways to read it — usePack() for components, pack() for the modules that build a line when something happens rather than when something renders. Anything with a value in it is a function on the pack rather than a template at the call site, English pluralisation included: word order isn't universal, and a pack author has to be able to move the number. The roster constants (tones, rewrite styles, export formats, companions) keep only value + emoji, so a label can't drift from its key. On the server, internal/llm/lang.go replaces "Simplified Chinese" in the three prompts that actually name her language. pt-PT is spelled "European Portuguese (pt-PT, never Brazilian Portuguese)" in the prompt itself, and each Lang carries her word for "why" so the tutor prompt still recognises the question when she asks it her way. pair_lang reaches the model through the row-scoped query each handler already ran — the one that proves she owns the document — rather than a second lookup that could disagree with it. Also records Phase 18's deploy: migration 0011 rehearsed against a copy of the live VPS database, then applied for real.
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package suggestions
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"gitea.parodia.dev/drwily/petal/internal/auth"
|
|
"gitea.parodia.dev/drwily/petal/internal/httputil"
|
|
"gitea.parodia.dev/drwily/petal/internal/llm"
|
|
)
|
|
|
|
type translateResponse struct {
|
|
Translation string `json:"translation"`
|
|
}
|
|
|
|
// translate renders a suggestion's English explanation into Simplified Chinese
|
|
// for the Ask Petal bubble, so the ESL reader sees the "why" in her first
|
|
// language instead of a second copy of the same English text. The explanation is
|
|
// loaded server-side from the suggestion id (scoped to the local user) and never
|
|
// trusted from the client, mirroring chat (spec Note #10).
|
|
func (h *Handler) translate(w http.ResponseWriter, r *http.Request) {
|
|
sugID := chi.URLParam(r, "id")
|
|
|
|
var explanation, pairLang string
|
|
err := h.DB.QueryRow(
|
|
`SELECT s.explanation, COALESCE(u.pair_lang, '')
|
|
FROM suggestions s
|
|
JOIN documents d ON d.id = s.doc_id
|
|
JOIN users u ON u.id = d.user_id
|
|
WHERE s.id = ? AND d.user_id = ?`,
|
|
sugID, auth.UserID(r.Context()),
|
|
).Scan(&explanation, &pairLang)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httputil.ErrorJSON(w, http.StatusNotFound, "suggestion not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
httputil.ServerError(w, err)
|
|
return
|
|
}
|
|
|
|
explanation = strings.TrimSpace(explanation)
|
|
if explanation == "" {
|
|
httputil.WriteJSON(w, http.StatusOK, translateResponse{Translation: ""})
|
|
return
|
|
}
|
|
|
|
out, err := llm.RunTranslate(r.Context(), h.Client, explanation, llm.LangFor(pairLang))
|
|
if err != nil {
|
|
httputil.ErrorJSON(w, http.StatusBadGateway, "translate failed: "+err.Error())
|
|
return
|
|
}
|
|
|
|
httputil.WriteJSON(w, http.StatusOK, translateResponse{Translation: out})
|
|
}
|