[agent/codex:gpt-5.4-mini] Read ~/spec/code/core/go/i18n/RFC.md fully. Find features de... #188
4 changed files with 60 additions and 0 deletions
15
i18n.go
15
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 {
|
||||
|
|
|
|||
19
i18n_test.go
19
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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue