fix: Default() no longer overwrites SetDefault via Init()
Some checks failed
Security Scan / security (push) Successful in 8s
Test / test (push) Failing after 25s

Init().defaultOnce.Do() was replacing the service set by SetDefault().
Now Default() checks the atomic pointer first, and Init() skips if
a service was already set. Fixes translations not appearing in CLI.

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-17 02:00:12 +00:00
parent 7968811d56
commit 92e2b5688b

View file

@ -130,13 +130,16 @@ func NewWithLoader(loader Loader, opts ...Option) (*Service, error) {
return s, nil
}
// Init initialises the default global service.
// Init initialises the default global service if none has been set via SetDefault.
func Init() error {
defaultOnce.Do(func() {
// If SetDefault was already called, don't overwrite
if defaultService.Load() != nil {
return
}
svc, err := New()
if err == nil {
defaultService.Store(svc)
loadRegisteredLocales(svc)
}
defaultErr = err
})
@ -146,6 +149,9 @@ func Init() error {
// Default returns the global i18n service, initialising if needed.
// Returns nil if initialisation fails (error is logged).
func Default() *Service {
if svc := defaultService.Load(); svc != nil {
return svc
}
if err := Init(); err != nil {
log.Printf("i18n: failed to initialise default service: %v", err)
}