From 2e4a6e5e113bc59ee7891c8a758fb576a04a4974 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 10:38:34 +0000 Subject: [PATCH] refactor(i18n): add core service current-state aliases Co-Authored-By: Virgil --- core_service.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ hooks_test.go | 9 +++++++++ 2 files changed, 54 insertions(+) diff --git a/core_service.go b/core_service.go index 890a3c9..b3fdaef 100644 --- a/core_service.go +++ b/core_service.go @@ -148,6 +148,11 @@ func (s *CoreService) Mode() Mode { return s.svc.Mode() } +// CurrentMode returns the current translation mode. +func (s *CoreService) CurrentMode() Mode { + return s.Mode() +} + // T translates a message through the wrapped i18n service. func (s *CoreService) T(messageID string, args ...any) string { return s.svc.T(messageID, args...) @@ -178,6 +183,11 @@ func (s *CoreService) Language() string { return s.svc.Language() } +// CurrentLanguage returns the wrapped service language. +func (s *CoreService) CurrentLanguage() string { + return s.Language() +} + // SetFallback changes the wrapped service fallback language. func (s *CoreService) SetFallback(lang string) { s.svc.SetFallback(lang) @@ -188,6 +198,11 @@ func (s *CoreService) Fallback() string { return s.svc.Fallback() } +// CurrentFallback returns the wrapped service fallback language. +func (s *CoreService) CurrentFallback() string { + return s.Fallback() +} + // SetFormality changes the wrapped service default formality. func (s *CoreService) SetFormality(f Formality) { s.svc.SetFormality(f) @@ -198,6 +213,11 @@ func (s *CoreService) Formality() Formality { return s.svc.Formality() } +// CurrentFormality returns the wrapped service default formality. +func (s *CoreService) CurrentFormality() Formality { + return s.Formality() +} + // SetLocation changes the wrapped service default location. func (s *CoreService) SetLocation(location string) { s.svc.SetLocation(location) @@ -208,6 +228,11 @@ func (s *CoreService) Location() string { return s.svc.Location() } +// CurrentLocation returns the wrapped service default location. +func (s *CoreService) CurrentLocation() string { + return s.Location() +} + // SetDebug changes the wrapped service debug mode. func (s *CoreService) SetDebug(enabled bool) { s.svc.SetDebug(enabled) @@ -218,6 +243,11 @@ func (s *CoreService) Debug() bool { return s.svc.Debug() } +// CurrentDebug reports whether wrapped service debug mode is enabled. +func (s *CoreService) CurrentDebug() bool { + return s.Debug() +} + // AddHandler appends handlers to the wrapped service's chain. func (s *CoreService) AddHandler(handlers ...KeyHandler) { s.svc.AddHandler(handlers...) @@ -248,6 +278,11 @@ func (s *CoreService) Handlers() []KeyHandler { return s.svc.Handlers() } +// CurrentHandlers returns a copy of the wrapped service's handler chain. +func (s *CoreService) CurrentHandlers() []KeyHandler { + return s.Handlers() +} + // AddLoader loads extra locale data into the wrapped service. func (s *CoreService) AddLoader(loader Loader) error { return s.svc.AddLoader(loader) @@ -263,11 +298,21 @@ func (s *CoreService) AvailableLanguages() []string { return s.svc.AvailableLanguages() } +// CurrentAvailableLanguages returns the wrapped service languages. +func (s *CoreService) CurrentAvailableLanguages() []string { + return s.AvailableLanguages() +} + // Direction returns the wrapped service text direction. func (s *CoreService) Direction() TextDirection { return s.svc.Direction() } +// CurrentDirection returns the wrapped service text direction. +func (s *CoreService) CurrentDirection() TextDirection { + return s.Direction() +} + // IsRTL reports whether the wrapped service language is right-to-left. func (s *CoreService) IsRTL() bool { return s.svc.IsRTL() diff --git a/hooks_test.go b/hooks_test.go index b8093cd..76d4a06 100644 --- a/hooks_test.go +++ b/hooks_test.go @@ -350,9 +350,17 @@ func TestCoreService_DelegatesToWrappedService(t *testing.T) { assert.Equal(t, svc.Raw("i18n.label.status"), coreSvc.Raw("i18n.label.status")) assert.Equal(t, svc.Translate("i18n.label.status"), coreSvc.Translate("i18n.label.status")) assert.Equal(t, svc.AvailableLanguages(), coreSvc.AvailableLanguages()) + assert.Equal(t, svc.AvailableLanguages(), coreSvc.CurrentAvailableLanguages()) assert.Equal(t, svc.Direction(), coreSvc.Direction()) + assert.Equal(t, svc.Direction(), coreSvc.CurrentDirection()) assert.Equal(t, svc.IsRTL(), coreSvc.IsRTL()) assert.Equal(t, svc.PluralCategory(2), coreSvc.PluralCategory(2)) + assert.Equal(t, svc.Mode(), coreSvc.CurrentMode()) + assert.Equal(t, svc.Language(), coreSvc.CurrentLanguage()) + assert.Equal(t, svc.Fallback(), coreSvc.CurrentFallback()) + assert.Equal(t, svc.Formality(), coreSvc.CurrentFormality()) + assert.Equal(t, svc.Location(), coreSvc.CurrentLocation()) + assert.Equal(t, svc.Debug(), coreSvc.CurrentDebug()) require.NoError(t, coreSvc.SetLanguage("en")) assert.Equal(t, "en", coreSvc.Language()) @@ -373,6 +381,7 @@ func TestCoreService_DelegatesToWrappedService(t *testing.T) { handlers := coreSvc.Handlers() assert.Equal(t, svc.Handlers(), handlers) + assert.Equal(t, svc.Handlers(), coreSvc.CurrentHandlers()) coreSvc.SetHandlers(LabelHandler{}) require.Len(t, coreSvc.Handlers(), 1) -- 2.45.3