feat(html): add context identity setter
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:38:10 +00:00
parent 831a47f1d3
commit 19ab9d246a
2 changed files with 29 additions and 0 deletions

View file

@ -102,6 +102,18 @@ func (ctx *Context) SetService(svc Translator) *Context {
return ctx
}
// SetIdentity updates the context identity and returns the same context.
// Example: ctx.SetIdentity("user-123").
func (ctx *Context) SetIdentity(identity string) *Context {
if ctx == nil {
return nil
}
ensureContextDefaults(ctx)
ctx.Identity = identity
return ctx
}
// SetLocale updates the context locale and reapplies it to the active
// translator.
// Example: ctx.SetLocale("en-US").

View file

@ -96,9 +96,26 @@ func TestContext_SetService_ReappliesCurrentLocale(t *testing.T) {
}
}
func TestContext_SetIdentity_UpdatesIdentity(t *testing.T) {
ctx := NewContext()
if got := ctx.SetIdentity("user-123"); got != ctx {
t.Fatalf("SetIdentity should return the same context")
}
if ctx.Identity != "user-123" {
t.Fatalf("SetIdentity should update context identity, got %q", ctx.Identity)
}
if ctx.Data == nil {
t.Fatal("SetIdentity should preserve initialised Data map")
}
}
func TestContext_Setters_NilReceiver(t *testing.T) {
var ctx *Context
if got := ctx.SetIdentity("user-123"); got != nil {
t.Fatalf("nil Context.SetIdentity should return nil, got %v", got)
}
if got := ctx.SetLocale("en-GB"); got != nil {
t.Fatalf("nil Context.SetLocale should return nil, got %v", got)
}