feat(i18n): expose fallback language accessors
Some checks failed
Security Scan / security (push) Successful in 9s
Test / test (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 04:20:04 +00:00
parent 9a58b60f0e
commit a2e7f01cbb
4 changed files with 64 additions and 0 deletions

15
i18n.go
View file

@ -54,6 +54,13 @@ func SetMode(m Mode) {
}
}
// SetFallback sets the fallback language for the default service.
func SetFallback(lang string) {
if svc := Default(); svc != nil {
svc.SetFallback(lang)
}
}
// CurrentMode returns the current translation mode.
func CurrentMode() Mode {
if svc := Default(); svc != nil {
@ -62,6 +69,14 @@ func CurrentMode() Mode {
return ModeNormal
}
// CurrentFallback returns the current fallback language.
func CurrentFallback() string {
if svc := Default(); svc != nil {
return svc.Fallback()
}
return "en"
}
// CurrentFormality returns the current default formality.
func CurrentFormality() Formality {
if svc := Default(); svc != nil {

View file

@ -120,6 +120,20 @@ func TestSetFormality_Good(t *testing.T) {
assert.Equal(t, FormalityNeutral, svc.Formality())
}
// --- Package-level SetFallback ---
func TestSetFallback_Good(t *testing.T) {
svc, err := New()
require.NoError(t, err)
SetDefault(svc)
SetFallback("fr")
assert.Equal(t, "fr", svc.Fallback())
SetFallback("en")
assert.Equal(t, "en", svc.Fallback())
}
// --- Package-level CurrentFormality ---
func TestCurrentFormality_Good(t *testing.T) {
@ -133,6 +147,19 @@ func TestCurrentFormality_Good(t *testing.T) {
assert.Equal(t, FormalityFormal, CurrentFormality())
}
// --- Package-level CurrentFallback ---
func TestCurrentFallback_Good(t *testing.T) {
svc, err := New()
require.NoError(t, err)
SetDefault(svc)
assert.Equal(t, "en", CurrentFallback())
SetFallback("fr")
assert.Equal(t, "fr", CurrentFallback())
}
// --- Package-level SetLocation ---
func TestSetLocation_Good(t *testing.T) {

View file

@ -283,6 +283,12 @@ func (s *Service) SetMode(m Mode) { s.mu.Lock(); s.mode = m; s.mu.Unlo
func (s *Service) Mode() Mode { s.mu.RLock(); defer s.mu.RUnlock(); return s.mode }
func (s *Service) SetFormality(f Formality) { s.mu.Lock(); s.formality = f; s.mu.Unlock() }
func (s *Service) Formality() Formality { s.mu.RLock(); defer s.mu.RUnlock(); return s.formality }
func (s *Service) SetFallback(lang string) { s.mu.Lock(); s.fallbackLang = lang; s.mu.Unlock() }
func (s *Service) Fallback() string {
s.mu.RLock()
defer s.mu.RUnlock()
return s.fallbackLang
}
func (s *Service) SetLocation(location string) {
s.mu.Lock()

View file

@ -228,6 +228,22 @@ func TestServiceModes(t *testing.T) {
svc.T("nonexistent.key")
}
func TestServiceFallback(t *testing.T) {
svc, err := New()
if err != nil {
t.Fatalf("New() failed: %v", err)
}
if svc.Fallback() != "en" {
t.Errorf("default Fallback() = %q, want en", svc.Fallback())
}
svc.SetFallback("fr")
if svc.Fallback() != "fr" {
t.Errorf("Fallback() = %q, want fr", svc.Fallback())
}
}
func TestServiceDebug(t *testing.T) {
svc, err := New()
if err != nil {