refactor(i18n): delegate prompt and lang wrappers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
ec9dc0666b
commit
afe7b6e9de
2 changed files with 15 additions and 23 deletions
26
i18n.go
26
i18n.go
|
|
@ -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 {
|
||||
|
|
|
|||
12
service.go
12
service.go
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue