go-i18n/default_service.go
Virgil dd9d0832af
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
refactor(i18n): centralise namespace lookups
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-03 07:43:40 +00:00

25 lines
820 B
Go

package i18n
// withDefaultService runs fn when the default service is available.
func withDefaultService(fn func(*Service)) {
if svc := Default(); svc != nil {
fn(svc)
}
}
// defaultServiceValue returns the value produced by fn when the default
// service exists, or fallback otherwise.
func defaultServiceValue[T any](fallback T, fn func(*Service) T) T {
if svc := Default(); svc != nil {
return fn(svc)
}
return fallback
}
// defaultServiceNamespaceValue resolves a namespace key against the default
// service when available, or returns the namespace-qualified key otherwise.
func defaultServiceNamespaceValue(namespace, key string, lookup func(*Service, string) string) string {
return defaultServiceValue(namespaceLookupKey(namespace, key), func(svc *Service) string {
return lookup(svc, key)
})
}