The portal in English and Turkish: a catalogue per language, a switcher at the foot of every page, landing.language, Turkish numbers and dates

Every template and handler message in internal/web goes through a JSON
catalogue under internal/web/locales/<code>/, with English as the fallback.
The language is the person's choice (a cookie set by /_lang/{code}), else
Accept-Language, else landing.language. Tests refuse a key some language
lacks, a translation with other arguments than English, and template text
left in English.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-05 00:12:53 +02:00
1 parent 30f7f3474a
commit 11231d8c2b
113 files changed
+8091 -1981

No files matched your search

+159 -89
View File
@@ -3,7 +3,6 @@ package web
import (
"context"
"errors"
"fmt"
"net/http"
"sort"
"strings"
@@ -124,144 +123,146 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
}))
mux.HandleFunc("POST /admin/users/{id}/approve", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
func(ctx context.Context, admin store.User, id string, r *http.Request) (notice, error) {
project := r.FormValue("project")
if err := s.ApproveUser(ctx, id, project, admin.Email, time.Now()); err != nil {
return "", err
return notice{}, err
}
if project == "" {
return "approved, on their own for now", nil
return say("users.msg.approved_alone"), nil
}
return "approved into " + projectPath(ctx, s, project), nil
return say("users.msg.approved_into", projectPath(ctx, s, project)), nil
}))
mux.HandleFunc("POST /admin/users/{id}/suspend", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, _ *http.Request) (string, error) {
func(ctx context.Context, admin store.User, id string, _ *http.Request) (notice, error) {
if id == admin.ID {
// Locking yourself out is not a thing an interface should let
// you do by misclicking.
return "", errors.New("you cannot suspend your own account")
return notice{}, refuse("users.err.self_suspend")
}
if err := s.SetUserState(ctx, id, store.UserSuspended); err != nil {
return "", err
return notice{}, err
}
// Otherwise they stay signed in and suspension is advisory until
// their session happens to expire.
if err := s.DeleteUserSessions(ctx, id); err != nil {
return "", err
return notice{}, err
}
return "suspended, and signed out everywhere", nil
return say("users.msg.suspended"), nil
}))
mux.HandleFunc("POST /admin/users/{id}/reinstate", p.adminAction(s,
func(ctx context.Context, _ store.User, id string, _ *http.Request) (string, error) {
func(ctx context.Context, _ store.User, id string, _ *http.Request) (notice, error) {
if err := s.SetUserState(ctx, id, store.UserActive); err != nil {
return "", err
return notice{}, err
}
return "reinstated", nil
return say("users.msg.reinstated"), nil
}))
mux.HandleFunc("POST /admin/users/{id}/join", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
func(ctx context.Context, admin store.User, id string, r *http.Request) (notice, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
if !ok || scopeID == "" {
return "", errors.New("choose where they go")
return notice{}, refuse("users.err.choose_where")
}
if err := s.AddMembership(ctx, id, store.Scope(scope), scopeID, store.RoleMember,
admin.Email, time.Now()); err != nil {
return "", err
return notice{}, err
}
return "added to " + scopeLabel(ctx, s, store.Scope(scope), scopeID), nil
return say("users.msg.added_to", scopeLabel(ctx, s, store.Scope(scope), scopeID)), nil
}))
// Managing is a role on an organization membership: it lets the person see
// that organization's page, and nothing else changes.
mux.HandleFunc("POST /admin/users/{id}/role", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
func(ctx context.Context, admin store.User, id string, r *http.Request) (notice, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
role := r.FormValue("role")
if !ok || store.Scope(scope) != store.ScopeOrg || (role != store.RoleManager && role != store.RoleMember) {
return "", errors.New("a manager is a role in an organization")
return notice{}, refuse("users.err.manager_role")
}
if err := s.AddMembership(ctx, id, store.ScopeOrg, scopeID, role, admin.Email, time.Now()); err != nil {
return "", err
return notice{}, err
}
label := scopeLabel(ctx, s, store.ScopeOrg, scopeID)
if role == store.RoleManager {
return "now manages " + label, nil
return say("users.msg.now_manages", label), nil
}
return "no longer manages " + label, nil
return say("users.msg.no_longer_manages", label), nil
}))
mux.HandleFunc("POST /admin/users/{id}/leave", p.adminAction(s,
func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) {
func(ctx context.Context, _ store.User, id string, r *http.Request) (notice, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
if !ok {
return "", errors.New("nothing to leave")
return notice{}, refuse("users.err.nothing_to_leave")
}
label := scopeLabel(ctx, s, store.Scope(scope), scopeID)
revoked, err := s.RemoveMembership(ctx, id, store.Scope(scope), scopeID, time.Now())
if err != nil {
return "", err
return notice{}, err
}
msg := "removed from " + label
if revoked > 0 {
msg += "; " + plural(revoked, "key", "keys") + " they made there revoked"
switch {
case revoked == 1:
return say("users.msg.removed_from_one_key", label), nil
case revoked > 1:
return say("users.msg.removed_from_keys", label, revoked), nil
}
return msg, nil
return say("users.msg.removed_from", label), nil
}))
mux.HandleFunc("POST /admin/users/{id}/delete", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, _ *http.Request) (string, error) {
func(ctx context.Context, admin store.User, id string, _ *http.Request) (notice, error) {
if id == admin.ID {
return "", errors.New("you cannot remove your own account")
return notice{}, refuse("users.err.self_remove")
}
if err := s.DeleteUser(ctx, id, time.Now()); err != nil {
return "", err
return notice{}, err
}
return "removed; their keys are revoked", nil
return say("users.msg.removed_keys_revoked"), nil
}))
// The tenancy tree. Creating a container is not granting access: nobody can
// use one until an account is approved into it.
mux.HandleFunc("POST /admin/tenancy/org", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
_, err := s.CreateOrg(ctx, r.FormValue("name"))
if err != nil {
return "", err
return notice{}, err
}
return "organization " + r.FormValue("name") + " created", nil
return say("users.msg.org_created", r.FormValue("name")), nil
}))
mux.HandleFunc("POST /admin/tenancy/team", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
org := r.FormValue("org")
if org == "" {
return "", errors.New("choose the organization this team belongs to")
return notice{}, refuse("users.err.choose_org")
}
_, err := s.CreateTeam(ctx, org, r.FormValue("name"))
if err != nil {
return "", err
return notice{}, err
}
return "team " + r.FormValue("name") + " created", nil
return say("users.msg.team_created", r.FormValue("name")), nil
}))
mux.HandleFunc("POST /admin/tenancy/project", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
team := r.FormValue("team")
if team == "" {
return "", errors.New("choose the team this project belongs to")
return notice{}, refuse("users.err.choose_team")
}
id, err := s.CreateProject(ctx, team, r.FormValue("name"))
if err != nil {
return "", err
return notice{}, err
}
r.Form.Set("open", id)
return "project " + r.FormValue("name") + " created", nil
return say("users.msg.project_created", r.FormValue("name")), nil
}))
mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/rename", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
id, name := r.PathValue("id"), strings.TrimSpace(r.FormValue("name"))
var err error
switch r.PathValue("kind") {
@@ -272,37 +273,37 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
case "project":
err = s.RenameProject(ctx, id, name)
default:
return "", errors.New("nothing of that kind")
return notice{}, refuse("users.err.no_such_kind")
}
if err != nil {
return "", err
return notice{}, err
}
return "renamed to " + name, nil
return say("users.msg.renamed", name), nil
}))
mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/move", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
id, to := r.PathValue("id"), r.FormValue("to")
if to == "" {
return "", errors.New("choose where it goes")
return notice{}, refuse("users.err.choose_destination")
}
switch r.PathValue("kind") {
case "team":
if err := s.MoveTeam(ctx, id, to); err != nil {
return "", err
return notice{}, err
}
return "team moved", nil
return say("users.msg.team_moved"), nil
case "project":
if err := s.MoveProject(ctx, id, to); err != nil {
return "", err
return notice{}, err
}
return "project moved to " + projectPath(ctx, s, id), nil
return say("users.msg.project_moved", projectPath(ctx, s, id)), nil
}
return "", errors.New("only a team or a project moves")
return notice{}, refuse("users.err.only_team_project_moves")
}))
mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/delete", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
id := r.PathValue("id")
var err error
switch r.PathValue("kind") {
@@ -313,43 +314,103 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
case "project":
err = s.DeleteProject(ctx, id)
default:
return "", errors.New("nothing of that kind")
return notice{}, refuse("users.err.no_such_kind")
}
if err != nil {
return "", err
return notice{}, err
}
r.Form.Del("open")
return "removed", nil
return say("users.msg.removed"), nil
}))
// The same as joining, from the scope's side: a form on a project, team or
// organization that names the person.
mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/add", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
user := r.FormValue("user")
if user == "" {
return "", errors.New("choose who to add")
return notice{}, refuse("users.err.choose_who")
}
scope, id := store.Scope(r.PathValue("kind")), r.PathValue("id")
if err := s.AddMembership(ctx, user, scope, id, store.RoleMember, "", time.Now()); err != nil {
return "", err
return notice{}, err
}
who := user
if u, err := s.UserByID(ctx, user); err == nil {
who = u.Email
}
return who + " added to " + scopeLabel(ctx, s, scope, id), nil
return say("users.msg.member_added", who, scopeLabel(ctx, s, scope, id)), nil
}))
mux.HandleFunc("POST /admin/keys/{id}/revoke", p.tenancyAction(s,
func(ctx context.Context, r *http.Request) (string, error) {
func(ctx context.Context, r *http.Request) (notice, error) {
if err := s.RevokeKey(ctx, r.PathValue("id"), time.Now()); err != nil {
return "", err
return notice{}, err
}
return "key revoked; a running server stops accepting it when its cache expires", nil
return say("users.msg.key_revoked"), nil
}))
}
// notice is what the page says about a change, kept as a catalogue key and
// its arguments rather than as text: the log and the audit record take it in
// English, which is what they are searched in, and the page in the language
// of the person who made the change.
type notice struct {
key string
args []any
}
func say(key string, args ...any) notice { return notice{key: key, args: args} }
// in is the notice in one language.
func (n notice) in(l *Lang) string {
if n.key == "" {
return ""
}
return l.T(n.key, n.args...)
}
// refusal is a change the page refused, said as a notice so the person who
// asked reads why in their own language. As an error it is English, for the
// log and the audit record.
type refusal struct{ notice }
func (e refusal) Error() string { return e.in(english) }
func refuse(key string, args ...any) error { return refusal{say(key, args...)} }
// errorIn is an error from a change as the page shows it.
func errorIn(l *Lang, err error) string {
var r refusal
if errors.As(err, &r) {
return r.in(l)
}
return l.Err(err)
}
// The notices are built by key in the handlers above; the catalogue test
// cannot see a key passed to say, so they are listed here.
func init() {
registerKeys(
"users.msg.approved_alone", "users.msg.approved_into", "users.msg.suspended",
"users.msg.reinstated", "users.msg.added_to", "users.msg.now_manages",
"users.msg.no_longer_manages", "users.msg.removed_from", "users.msg.removed_from_one_key",
"users.msg.removed_from_keys", "users.msg.removed_keys_revoked", "users.msg.org_created",
"users.msg.team_created", "users.msg.project_created", "users.msg.renamed",
"users.msg.team_moved", "users.msg.project_moved", "users.msg.removed",
"users.msg.member_added", "users.msg.key_revoked",
"users.err.self_suspend", "users.err.choose_where", "users.err.manager_role",
"users.err.nothing_to_leave", "users.err.self_remove", "users.err.choose_org",
"users.err.choose_team", "users.err.no_such_kind", "users.err.choose_destination",
"users.err.only_team_project_moves", "users.err.choose_who", "users.err.duplicate",
"users.err.not_empty", "users.err.gone",
// The words for an account's and a key's state, looked up by the
// state, which is also the row's class.
"users.state.active", "users.state.pending", "users.state.suspended",
"users.key_state.active", "users.key_state.revoked", "users.key_state.expired",
)
}
func projectPath(ctx context.Context, s AccountAdmin, id string) string {
all, err := s.AllProjects(ctx)
if err == nil {
@@ -388,14 +449,17 @@ func scopeLabel(ctx context.Context, s AccountAdmin, scope store.Scope, id strin
}
// plainError says what a store error means to the person who caused it.
//
// What a scope still holds when it is not empty comes from the store in
// English ("it still has 1 member and 2 keys"), and is shown as it is.
func plainError(err error) error {
switch {
case errors.Is(err, store.ErrDuplicateName):
return errors.New("there is already one of those here")
return refuse("users.err.duplicate")
case errors.Is(err, store.ErrNotEmpty):
return fmt.Errorf("not removed: %s", strings.TrimPrefix(err.Error(), store.ErrNotEmpty.Error()+": "))
return refuse("users.err.not_empty", strings.TrimPrefix(err.Error(), store.ErrNotEmpty.Error()+": "))
case errors.Is(err, store.ErrNotFound):
return errors.New("that is no longer there; the page was out of date")
return refuse("users.err.gone")
}
return err
}
@@ -407,7 +471,7 @@ func plainError(err error) error {
// here, and passing an empty id through it would write audit lines about
// nobody.
func (p *AccountPages) tenancyAction(s AccountAdmin,
do func(ctx context.Context, r *http.Request) (string, error),
do func(ctx context.Context, r *http.Request) (notice, error),
) http.HandlerFunc {
return p.adminOnly(func(w http.ResponseWriter, r *http.Request, admin store.User) {
sess, _, _ := p.session(r)
@@ -415,19 +479,20 @@ func (p *AccountPages) tenancyAction(s AccountAdmin,
http.Error(w, "bad request", http.StatusBadRequest)
return
}
l := langOf(r)
msg, err := do(r.Context(), r)
if err != nil {
err = plainError(err)
p.log.Warn("a tenancy change was refused", "admin", admin.Email,
"path", r.URL.Path, "err", err)
noteAudit(r, "", "", err.Error())
p.renderUsers(w, r, s, admin, "", err.Error(), r.FormValue("open"))
p.renderUsers(w, r, s, admin, "", errorIn(l, err), r.FormValue("open"))
return
}
p.log.Info("the tenancy tree was changed", "admin", admin.Email,
"path", r.URL.Path, "what", msg)
noteAudit(r, "", msg, "")
p.renderUsers(w, r, s, admin, msg, "", r.FormValue("open"))
"path", r.URL.Path, "what", msg.in(english))
noteAudit(r, "", msg.in(english), "")
p.renderUsers(w, r, s, admin, msg.in(l), "", r.FormValue("open"))
})
}
@@ -454,7 +519,7 @@ func (p *AccountPages) adminOnly(
// adminAction wraps a change to one account: admin session, CSRF, audit line,
// render.
func (p *AccountPages) adminAction(s AccountAdmin,
do func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error),
do func(ctx context.Context, admin store.User, id string, r *http.Request) (notice, error),
) http.HandlerFunc {
return p.adminOnly(func(w http.ResponseWriter, r *http.Request, admin store.User) {
sess, _, _ := p.session(r)
@@ -463,6 +528,7 @@ func (p *AccountPages) adminAction(s AccountAdmin,
return
}
id := r.PathValue("id")
l := langOf(r)
subject := id
if u, err := s.UserByID(r.Context(), id); err == nil {
@@ -477,13 +543,13 @@ func (p *AccountPages) adminAction(s AccountAdmin,
p.log.Warn("an administrative action was refused",
"admin", admin.Email, "subject", subject, "err", err)
noteAudit(r, subject, "", err.Error())
p.renderUsers(w, r, s, admin, "", subject+": "+err.Error(), r.FormValue("open"))
p.renderUsers(w, r, s, admin, "", l.T("users.err.about", subject, errorIn(l, err)), r.FormValue("open"))
return
}
p.log.Info("an account was changed",
"admin", admin.Email, "subject", subject, "what", msg)
noteAudit(r, subject, msg, "")
p.renderUsers(w, r, s, admin, subject+" "+msg, "", r.FormValue("open"))
"admin", admin.Email, "subject", subject, "what", msg.in(english))
noteAudit(r, subject, msg.in(english), "")
p.renderUsers(w, r, s, admin, l.T("users.msg.about", subject, msg.in(l)), "", r.FormValue("open"))
})
}
@@ -495,7 +561,7 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request,
// buildUsers is the tree and its people, as the first tab shows them.
func (p *AccountPages) buildUsers(r *http.Request, s AccountAdmin, admin store.User, msg, errMsg, open string) adminUsersData {
ctx := r.Context()
ctx, l := r.Context(), langOf(r)
d := adminUsersData{
Nav: navFrom(r, "people"),
Message: msg, Error: errMsg, Open: open, AdminID: admin.ID, Sel: r.FormValue("sel"),
@@ -508,26 +574,28 @@ func (p *AccountPages) buildUsers(r *http.Request, s AccountAdmin, admin store.U
Keys: map[string][]keyRow{},
}
var err error
logErr := func(what string, err error) {
// logErr logs what could not be read in English and says so on the page,
// once, in the reader's language.
logErr := func(what, said string, err error) {
if err != nil {
p.log.Error("could not read "+what, "err", err)
if d.Error == "" {
d.Error = "could not read " + what
d.Error = said
}
}
}
d.Projects, err = s.AllProjects(ctx)
logErr("the projects", err)
logErr("the projects", l.T("users.err.read_projects"), err)
paths := make(map[string]string, len(d.Projects))
for _, pp := range d.Projects {
paths[pp.ID] = pp.Path
}
all, err := s.Users(ctx, "")
logErr("the accounts", err)
logErr("the accounts", l.T("users.err.read_accounts"), err)
memberships, err := s.AllMemberships(ctx)
logErr("the memberships", err)
logErr("the memberships", l.T("users.err.read_memberships"), err)
byUser := map[string][]store.Membership{}
for _, m := range memberships {
byUser[m.UserID] = append(byUser[m.UserID], m)
@@ -549,18 +617,18 @@ func (p *AccountPages) buildUsers(r *http.Request, s AccountAdmin, admin store.U
}
keys, err := s.AllKeys(ctx)
logErr("the keys", err)
logErr("the keys", l.T("users.err.read_keys"), err)
now := time.Now()
for _, k := range keys {
d.Keys[k.ProjectID] = append(d.Keys[k.ProjectID], keyRow{KeyRow: k, State: keyState(k.Key, now)})
}
d.Tree, err = s.Tree(ctx)
logErr("the tree", err)
logErr("the tree", l.T("users.err.read_tree"), err)
d.Orgs, err = s.AllOrgs(ctx)
logErr("the organizations", err)
logErr("the organizations", l.T("users.err.read_orgs"), err)
d.Teams, err = s.AllTeams(ctx)
logErr("the teams", err)
logErr("the teams", l.T("users.err.read_teams"), err)
for _, o := range d.Orgs {
d.Scopes = append(d.Scopes, scopeOption{"org:" + o.ID, o.Name})
}
@@ -574,6 +642,8 @@ func (p *AccountPages) buildUsers(r *http.Request, s AccountAdmin, admin store.U
return d
}
// keyState is a key's state as a value: the row's class, and the key its word
// on the page is looked up by.
func keyState(k store.Key, now time.Time) string {
switch {
case !k.RevokedAt.IsZero():