refactor(i18n): delegate prompt and lang wrappers
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 07:12:34 +00:00
parent ec9dc0666b
commit afe7b6e9de
2 changed files with 15 additions and 23 deletions

26
i18n.go
View file

@ -239,11 +239,9 @@ func N(format string, value any, args ...any) string {
// Prompt("yes") // "y"
// Prompt("confirm") // "Are you sure?"
func Prompt(key string) string {
key = normalizeLookupKey(key)
if key == "" {
return ""
}
return T(namespaceLookupKey("prompt", key))
return defaultServiceValue("", func(svc *Service) string {
return svc.Prompt(key)
})
}
// CurrentPrompt is a short alias for Prompt.
@ -263,21 +261,9 @@ func CurrentPrompt(key string) string {
//
// Lang("de") // "German"
func Lang(key string) string {
key = normalizeLookupKey(key)
if key == "" {
return ""
}
if got := T(namespaceLookupKey("lang", key)); got != namespaceLookupKey("lang", key) {
return got
}
if idx := indexAny(key, "-_"); idx > 0 {
if base := key[:idx]; base != "" {
if got := T(namespaceLookupKey("lang", base)); got != namespaceLookupKey("lang", base) {
return got
}
}
}
return namespaceLookupKey("lang", key)
return defaultServiceValue("", func(svc *Service) string {
return svc.Lang(key)
})
}
func normalizeLookupKey(key string) string {

View file

@ -403,18 +403,24 @@ func (s *Service) Lang(key string) string {
return ""
}
lookupKey := namespaceLookupKey("lang", key)
if text, ok := s.translateWithStatus(lookupKey); ok {
s.mu.RLock()
text := s.resolveDirectLocked(lookupKey, nil)
s.mu.RUnlock()
if text != "" {
return text
}
if idx := indexAny(key, "-_"); idx > 0 {
if base := key[:idx]; base != "" {
baseLookupKey := namespaceLookupKey("lang", base)
if text, ok := s.translateWithStatus(baseLookupKey); ok {
s.mu.RLock()
text = s.resolveDirectLocked(baseLookupKey, nil)
s.mu.RUnlock()
if text != "" {
return text
}
}
}
return lookupKey
return s.handleMissingKey(lookupKey, nil)
}
func (s *Service) AvailableLanguages() []string {