diff --git a/localise.go b/localise.go index b4a833b..e0533e4 100644 --- a/localise.go +++ b/localise.go @@ -73,6 +73,21 @@ func SetFormality(f Formality) { } } +// SetLocation sets the default location context on the default service. +func SetLocation(location string) { + if svc := Default(); svc != nil { + svc.SetLocation(location) + } +} + +// CurrentLocation returns the current default location context. +func CurrentLocation() string { + if svc := Default(); svc != nil { + return svc.Location() + } + return "" +} + // Direction returns the text direction for the current language. func Direction() TextDirection { if svc := Default(); svc != nil { diff --git a/localise_test.go b/localise_test.go index ff5fe72..cefd15c 100644 --- a/localise_test.go +++ b/localise_test.go @@ -132,6 +132,33 @@ func TestCurrentFormality_Good(t *testing.T) { assert.Equal(t, FormalityFormal, CurrentFormality()) } +// --- Package-level SetLocation --- + +func TestSetLocation_Good(t *testing.T) { + svc, err := New() + require.NoError(t, err) + SetDefault(svc) + + SetLocation("workspace") + assert.Equal(t, "workspace", svc.Location()) + + SetLocation("") + assert.Equal(t, "", svc.Location()) +} + +// --- Package-level CurrentLocation --- + +func TestCurrentLocation_Good(t *testing.T) { + svc, err := New() + require.NoError(t, err) + SetDefault(svc) + + assert.Equal(t, "", CurrentLocation()) + + SetLocation("workspace") + assert.Equal(t, "workspace", CurrentLocation()) +} + // --- Package-level Direction --- func TestDirection_Good(t *testing.T) { diff --git a/service.go b/service.go index 54a8ba1..4618041 100644 --- a/service.go +++ b/service.go @@ -25,6 +25,7 @@ type Service struct { mode Mode debug bool formality Formality + location string handlers []KeyHandler mu sync.RWMutex } @@ -42,6 +43,11 @@ func WithFormality(f Formality) Option { return func(s *Service) { s.formality = f } } +// WithLocation sets the default location context. +func WithLocation(location string) Option { + return func(s *Service) { s.location = location } +} + // WithHandlers sets custom handlers (replaces default handlers). func WithHandlers(handlers ...KeyHandler) Option { return func(s *Service) { s.handlers = handlers } @@ -257,6 +263,18 @@ func (s *Service) Mode() Mode { s.mu.RLock(); defer s.mu.RUnlock() 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) SetLocation(location string) { + s.mu.Lock() + defer s.mu.Unlock() + s.location = location +} + +func (s *Service) Location() string { + s.mu.RLock() + defer s.mu.RUnlock() + return s.location +} + func (s *Service) Direction() TextDirection { s.mu.RLock() defer s.mu.RUnlock() @@ -439,7 +457,7 @@ func (s *Service) getEffectiveContextGenderLocationAndFormality(data any) (strin } return context, gender, location, formality } - return "", "", "", s.getEffectiveFormality(data) + return "", "", s.location, s.getEffectiveFormality(data) } func (s *Service) getEffectiveFormality(data any) Formality { diff --git a/service_test.go b/service_test.go index ec46076..3dbb536 100644 --- a/service_test.go +++ b/service_test.go @@ -216,6 +216,22 @@ func TestServiceFormality(t *testing.T) { } } +func TestServiceLocation(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + + if svc.Location() != "" { + t.Errorf("default Location() = %q, want empty", svc.Location()) + } + + svc.SetLocation("workspace") + if svc.Location() != "workspace" { + t.Errorf("Location() = %q, want workspace", svc.Location()) + } +} + func TestServiceTranslationContext(t *testing.T) { svc, err := New() if err != nil { @@ -270,6 +286,24 @@ func TestServiceTranslationContext(t *testing.T) { } } +func TestServiceDefaultLocationContext(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + SetDefault(svc) + + svc.AddMessages("en", map[string]string{ + "welcome._workspace": "welcome aboard", + }) + + svc.SetLocation("workspace") + + if got := svc.T("welcome"); got != "welcome aboard" { + t.Errorf("T(welcome) with default location = %q, want %q", got, "welcome aboard") + } +} + func TestServiceTranslationContextExtrasInTemplates(t *testing.T) { svc, err := New() if err != nil { @@ -382,6 +416,7 @@ func TestServiceWithOptions(t *testing.T) { svc, err := New( WithFallback("en"), WithFormality(FormalityFormal), + WithLocation("workspace"), WithMode(ModeCollect), WithDebug(true), ) @@ -398,6 +433,9 @@ func TestServiceWithOptions(t *testing.T) { if !svc.Debug() { t.Error("Debug should be true") } + if svc.Location() != "workspace" { + t.Errorf("Location = %q, want workspace", svc.Location()) + } } func TestNewWithFS(t *testing.T) {