fix(i18n): append missing-key handlers
This commit is contained in:
parent
0cea872363
commit
6ca01b37a5
2 changed files with 32 additions and 1 deletions
6
hooks.go
6
hooks.go
|
|
@ -138,7 +138,11 @@ func markLocalesLoaded() {
|
||||||
|
|
||||||
// OnMissingKey registers a handler for missing translation keys.
|
// OnMissingKey registers a handler for missing translation keys.
|
||||||
func OnMissingKey(h MissingKeyHandler) {
|
func OnMissingKey(h MissingKeyHandler) {
|
||||||
SetMissingKeyHandlers(h)
|
if h == nil {
|
||||||
|
ClearMissingKeyHandlers()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
AddMissingKeyHandler(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMissingKeyHandlers replaces the full missing-key handler chain.
|
// SetMissingKeyHandlers replaces the full missing-key handler chain.
|
||||||
|
|
|
||||||
|
|
@ -543,6 +543,33 @@ func TestOnMissingKey_Good(t *testing.T) {
|
||||||
assert.Equal(t, "hooks_test.go", filepath.Base(captured.CallerFile))
|
assert.Equal(t, "hooks_test.go", filepath.Base(captured.CallerFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOnMissingKey_Good_AppendsHandlers(t *testing.T) {
|
||||||
|
svc, err := New()
|
||||||
|
require.NoError(t, err)
|
||||||
|
prev := Default()
|
||||||
|
SetDefault(svc)
|
||||||
|
prevHandlers := missingKeyHandlers()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
missingKeyHandler.Store(prevHandlers)
|
||||||
|
SetDefault(prev)
|
||||||
|
})
|
||||||
|
|
||||||
|
svc.SetMode(ModeCollect)
|
||||||
|
ClearMissingKeyHandlers()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
ClearMissingKeyHandlers()
|
||||||
|
})
|
||||||
|
|
||||||
|
var first, second int
|
||||||
|
OnMissingKey(func(MissingKey) { first++ })
|
||||||
|
OnMissingKey(func(MissingKey) { second++ })
|
||||||
|
|
||||||
|
_ = T("missing.on.handler.append")
|
||||||
|
|
||||||
|
assert.Equal(t, 1, first)
|
||||||
|
assert.Equal(t, 1, second)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddMissingKeyHandler_Good(t *testing.T) {
|
func TestAddMissingKeyHandler_Good(t *testing.T) {
|
||||||
svc, err := New()
|
svc, err := New()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue