cli/pkg/i18n/hooks.go
Snider 285a4582b0 fix(i18n): address thread-safety issues from code review
- hooks.go: Use atomic.Value for missingKeyHandler global
- loader.go: Use sync.Once for FSLoader.Languages() caching
- service.go: Use atomic.Pointer[Service] for defaultService

All globals that could race between goroutines now use proper
synchronization primitives.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 19:02:30 +00:00

47 lines
1.1 KiB
Go

// Package i18n provides internationalization for the CLI.
package i18n
import (
"runtime"
"sync/atomic"
)
var missingKeyHandler atomic.Value // stores MissingKeyHandler
// OnMissingKey registers a handler for missing translation keys.
// Called when T() can't find a key in ModeCollect.
// Thread-safe: can be called concurrently with translations.
//
// i18n.SetMode(i18n.ModeCollect)
// i18n.OnMissingKey(func(m i18n.MissingKey) {
// log.Printf("MISSING: %s at %s:%d", m.Key, m.CallerFile, m.CallerLine)
// })
func OnMissingKey(h MissingKeyHandler) {
missingKeyHandler.Store(h)
}
// dispatchMissingKey creates and dispatches a MissingKey event.
// Called internally when a key is missing in ModeCollect.
func dispatchMissingKey(key string, args map[string]any) {
v := missingKeyHandler.Load()
if v == nil {
return
}
h, ok := v.(MissingKeyHandler)
if !ok || h == nil {
return
}
_, file, line, ok := runtime.Caller(2) // Skip dispatchMissingKey and handleMissingKey
if !ok {
file = "unknown"
line = 0
}
h(MissingKey{
Key: key,
Args: args,
CallerFile: file,
CallerLine: line,
})
}