From 02339893ee80a448844bef89f03db6e16c852653 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 09:40:20 +0000 Subject: [PATCH] feat(i18n): add handler chain replacement helpers Co-Authored-By: Virgil --- i18n.go | 15 +++++++++++++++ i18n_test.go | 19 +++++++++++++++++++ service.go | 7 +++++++ service_test.go | 19 +++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/i18n.go b/i18n.go index 4173ca6..576827a 100644 --- a/i18n.go +++ b/i18n.go @@ -172,6 +172,13 @@ func AddHandler(handlers ...KeyHandler) { } } +// SetHandlers replaces the default service's handler chain. +func SetHandlers(handlers ...KeyHandler) { + if svc := Default(); svc != nil { + svc.SetHandlers(handlers...) + } +} + // LoadFS loads additional translations from an fs.FS into the default service. // // Call this from init() in packages that ship their own locale files: @@ -195,6 +202,14 @@ func PrependHandler(handlers ...KeyHandler) { } } +// CurrentHandlers returns a copy of the default service's handler chain. +func CurrentHandlers() []KeyHandler { + if svc := Default(); svc != nil { + return svc.Handlers() + } + return nil +} + // ClearHandlers removes all handlers from the default service. func ClearHandlers() { if svc := Default(); svc != nil { diff --git a/i18n_test.go b/i18n_test.go index c3c91bf..b12bd8d 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -401,6 +401,25 @@ func TestClearHandlers_Good(t *testing.T) { assert.Empty(t, svc.Handlers()) } +func TestSetHandlers_Good(t *testing.T) { + svc, err := New() + require.NoError(t, err) + _ = Init() + prev := Default() + SetDefault(svc) + t.Cleanup(func() { + SetDefault(prev) + }) + + SetHandlers(serviceStubHandler{}) + + handlers := CurrentHandlers() + require.Len(t, handlers, 1) + assert.IsType(t, serviceStubHandler{}, handlers[0]) + assert.Equal(t, "stub", T("custom.stub")) + assert.Equal(t, "i18n.label.status", T("i18n.label.status")) +} + func TestNewWithHandlers_SkipsNil(t *testing.T) { svc, err := New(WithHandlers(nil, LabelHandler{})) require.NoError(t, err) diff --git a/service.go b/service.go index 1109dba..0568635 100644 --- a/service.go +++ b/service.go @@ -406,6 +406,13 @@ func (s *Service) AddHandler(handlers ...KeyHandler) { s.handlers = append(s.handlers, filterNilHandlers(handlers)...) } +// SetHandlers replaces the current handler chain. +func (s *Service) SetHandlers(handlers ...KeyHandler) { + s.mu.Lock() + defer s.mu.Unlock() + s.handlers = filterNilHandlers(handlers) +} + func (s *Service) PrependHandler(handlers ...KeyHandler) { s.mu.Lock() defer s.mu.Unlock() diff --git a/service_test.go b/service_test.go index d697123..44bae7a 100644 --- a/service_test.go +++ b/service_test.go @@ -1034,6 +1034,25 @@ func TestServiceHandlers(t *testing.T) { } } +func TestServiceSetHandlers(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + + svc.SetHandlers(serviceStubHandler{}, nil, LabelHandler{}) + handlers := svc.Handlers() + if got, want := len(handlers), 2; got != want { + t.Fatalf("len(Handlers()) = %d, want %d", got, want) + } + if _, ok := handlers[0].(serviceStubHandler); !ok { + t.Fatalf("Handlers()[0] = %T, want serviceStubHandler", handlers[0]) + } + if _, ok := handlers[1].(LabelHandler); !ok { + t.Fatalf("Handlers()[1] = %T, want LabelHandler", handlers[1]) + } +} + func TestWithDefaultHandlers_Idempotent(t *testing.T) { svc, err := New(WithDefaultHandlers()) if err != nil { -- 2.45.3