fix(cabana): translate hook ValidationError phrase keys in the request locale

- One shared localizer for ValidationError and ForbiddenError, tested in
  writeCRUDError's order; replaces the ForbiddenError-only one.
- Applied on the CRUD and relation service transactions, the detached bulk
  branch, runAction, the record read (ShowRecord), and the reorder,
  singleton and relation-options routes, which localized neither type.
- Only strings containing "::" are looked up, so translated text passes
  through unchanged and logs no missing-key warning.
- Copy-on-write: the plugin's error value is never modified.
- README and backend docs describe the localized ValidationError.
This commit is contained in:
2026-10-19 12:53:17 +02:00
parent d785958df2
commit ce8d7efb67
15 changed files with 376 additions and 67 deletions

View File

@@ -9,6 +9,7 @@ import (
"strings"
"sync"
"testing"
"testing/fstest"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
@@ -35,6 +36,12 @@ func (hookShelfPlugin) Requires() []string { return nil }
func (hookShelfPlugin) Register(*backpack.App) error { return nil }
func (hookShelfPlugin) Boot(*backpack.App) error { return nil }
func (hookShelfPlugin) AdminFS() fs.FS { return shelfFS }
// LangFS serves the acme.shelf refusal messages of
// TestReorderHookRefusalLocalized.
func (hookShelfPlugin) LangFS() fs.FS {
return fstest.MapFS{"lang/en/lang.yaml": {Data: []byte("errors:\n order: \"This order is not allowed.\"\n locked: \"This shelf is locked.\"\n")}}
}
func (p hookShelfPlugin) AdminControllers() []pact.AdminController {
return []pact.AdminController{hookedPins{hook: p.hook}}
}
@@ -217,6 +224,35 @@ func TestReorderHookErrorRollsBack(t *testing.T) {
}
}
// TestReorderHookRefusalLocalized: a ValidationError or ForbiddenError from
// ReorderAfterSave whose messages are phrase keys is answered with the
// sentences in the request locale, never the keys, and the reorder is rolled
// back.
func TestReorderHookRefusalLocalized(t *testing.T) {
for _, tc := range []struct {
name string
err error
status int
want string
key string
}{
{"validation error", &cabana.ValidationError{Details: map[string]any{"nodes": []string{"acme.shelf::lang.errors.order"}}}, http.StatusUnprocessableEntity, `"nodes":["This order is not allowed."]`, "acme.shelf::lang.errors.order"},
{"forbidden", &cabana.ForbiddenError{Message: "acme.shelf::lang.errors.locked"}, http.StatusForbidden, `"message":"This shelf is locked."`, "acme.shelf::lang.errors.locked"},
} {
t.Run(tc.name, func(t *testing.T) {
env := newShelfHookEnv(t, hookShelfPlugin{hook: func(context.Context, *uint64) error { return tc.err }})
before := sorts(t, env.db)
code, body := env.swap(t)
if code != tc.status || !strings.Contains(body, tc.want) || strings.Contains(body, tc.key) {
t.Fatalf("status=%d want %d body=%s", code, tc.status, body)
}
if after := sorts(t, env.db); fmt.Sprint(after) != fmt.Sprint(before) {
t.Fatalf("sort values changed: before %v after %v", before, after)
}
})
}
}
func TestReorderWithoutHookUnchanged(t *testing.T) {
plain := newShelfHookEnv(t, shelfPlugin{})
code, plainBody := plain.swap(t)