refactor(i18n): simplify current-state aliases
Some checks failed
Security Scan / security (push) Successful in 15s
Test / test (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 11:30:28 +00:00
parent 7f09ee550f
commit 2abe15c2b1
2 changed files with 21 additions and 21 deletions

30
i18n.go
View file

@ -65,7 +65,9 @@ func SetLanguage(lang string) error {
//
// lang := i18n.CurrentLanguage()
func CurrentLanguage() string {
return Language()
return defaultServiceValue("en", func(svc *Service) string {
return svc.Language()
})
}
// Language returns the current language code.
@ -101,7 +103,13 @@ func AvailableLanguages() []string {
//
// langs := i18n.CurrentAvailableLanguages()
func CurrentAvailableLanguages() []string {
return AvailableLanguages()
return defaultServiceValue([]string{}, func(svc *Service) []string {
langs := svc.AvailableLanguages()
if len(langs) == 0 {
return []string{}
}
return append([]string(nil), langs...)
})
}
// SetMode sets the translation mode for the default service.
@ -139,9 +147,7 @@ func Fallback() string {
//
// mode := i18n.CurrentMode()
func CurrentMode() Mode {
return defaultServiceValue(ModeNormal, func(svc *Service) Mode {
return svc.Mode()
})
return defaultServiceValue(ModeNormal, func(svc *Service) Mode { return svc.Mode() })
}
// CurrentFallback returns the current fallback language.
@ -150,9 +156,7 @@ func CurrentMode() Mode {
//
// fallback := i18n.CurrentFallback()
func CurrentFallback() string {
return defaultServiceValue("en", func(svc *Service) string {
return svc.Fallback()
})
return defaultServiceValue("en", func(svc *Service) string { return svc.Fallback() })
}
// CurrentFormality returns the current default formality.
@ -161,9 +165,7 @@ func CurrentFallback() string {
//
// formality := i18n.CurrentFormality()
func CurrentFormality() Formality {
return defaultServiceValue(FormalityNeutral, func(svc *Service) Formality {
return svc.Formality()
})
return defaultServiceValue(FormalityNeutral, func(svc *Service) Formality { return svc.Formality() })
}
// CurrentDebug reports whether debug mode is enabled on the default service.
@ -172,7 +174,7 @@ func CurrentFormality() Formality {
//
// debug := i18n.CurrentDebug()
func CurrentDebug() bool {
return Debug()
return defaultServiceValue(false, func(svc *Service) bool { return svc.Debug() })
}
// Debug reports whether debug mode is enabled on the default service.
@ -326,9 +328,7 @@ func PrependHandler(handlers ...KeyHandler) {
//
// handlers := i18n.CurrentHandlers()
func CurrentHandlers() []KeyHandler {
return defaultServiceValue([]KeyHandler{}, func(svc *Service) []KeyHandler {
return svc.Handlers()
})
return defaultServiceValue([]KeyHandler{}, func(svc *Service) []KeyHandler { return svc.Handlers() })
}
// Handlers returns a copy of the default service's handler chain.

View file

@ -91,7 +91,7 @@ func SetLocation(location string) {
//
// location := i18n.CurrentLocation()
func CurrentLocation() string {
return Location()
return defaultServiceValue("", func(svc *Service) string { return svc.Location() })
}
// Location returns the current default location context.
@ -122,7 +122,7 @@ func Direction() TextDirection {
//
// dir := i18n.CurrentDirection()
func CurrentDirection() TextDirection {
return Direction()
return defaultServiceValue(DirLTR, func(svc *Service) TextDirection { return svc.Direction() })
}
// IsRTL returns true if the current language uses right-to-left text.
@ -138,7 +138,9 @@ func IsRTL() bool { return Direction() == DirRTL }
// Example:
//
// rtl := i18n.CurrentIsRTL()
func CurrentIsRTL() bool { return IsRTL() }
func CurrentIsRTL() bool {
return defaultServiceValue(false, func(svc *Service) bool { return svc.IsRTL() })
}
// CurrentPluralCategory returns the plural category for the current default language.
//
@ -146,9 +148,7 @@ func CurrentIsRTL() bool { return IsRTL() }
//
// cat := i18n.CurrentPluralCategory(2)
func CurrentPluralCategory(n int) PluralCategory {
return defaultServiceValue(PluralOther, func(svc *Service) PluralCategory {
return svc.PluralCategory(n)
})
return defaultServiceValue(PluralOther, func(svc *Service) PluralCategory { return svc.PluralCategory(n) })
}
func detectLanguage(supported []language.Tag) string {