fix(i18n): release raw missing-key lock
Some checks failed
Security Scan / security (push) Successful in 12s
Test / test (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 06:13:20 +00:00
parent 3f6c06add5
commit 3c32b7a7ef
2 changed files with 43 additions and 3 deletions

View file

@ -771,16 +771,17 @@ func missingKeySubjectArgs(subj *Subject) map[string]any {
// Raw translates without i18n.* namespace magic.
func (s *Service) Raw(messageID string, args ...any) string {
s.mu.RLock()
defer s.mu.RUnlock()
var data any
if len(args) > 0 {
data = args[0]
}
text := s.resolveDirectLocked(messageID, data)
debug := s.debug
s.mu.RUnlock()
if text == "" {
return s.handleMissingKey(messageID, args)
text = s.handleMissingKey(messageID, args)
}
if s.debug {
if debug {
return debugFormat(messageID, text)
}
return text

View file

@ -275,6 +275,45 @@ func TestServiceRaw_DoesNotUseCommonFallbacks(t *testing.T) {
}
}
func TestServiceRaw_MissingKeyHandlersCanMutateService(t *testing.T) {
svc, err := New()
if err != nil {
t.Fatalf("New() failed: %v", err)
}
prev := missingKeyHandlers()
t.Cleanup(func() {
missingKeyHandler.Store(prev)
})
OnMissingKey(func(m MissingKey) {
_ = svc.SetLanguage("fr")
})
svc.SetMode(ModeCollect)
svc.SetDebug(true)
t.Cleanup(func() {
svc.SetDebug(false)
})
done := make(chan string, 1)
go func() {
done <- svc.Raw("missing.raw.key")
}()
select {
case got := <-done:
if got != "[missing.raw.key] [missing.raw.key]" {
t.Fatalf("Raw(missing.raw.key) = %q, want %q", got, "[missing.raw.key] [missing.raw.key]")
}
case <-time.After(2 * time.Second):
t.Fatal("Raw(missing.raw.key) timed out while missing-key handler mutated service state")
}
if got := svc.Language(); got != "fr" {
t.Fatalf("Language() = %q, want %q", got, "fr")
}
}
func TestServiceModes(t *testing.T) {
svc, err := New()
if err != nil {