diff --git a/service.go b/service.go index c228757..d2d899f 100644 --- a/service.go +++ b/service.go @@ -475,14 +475,22 @@ func (s *Service) getEffectiveContextGenderLocationAndFormality(data any) (strin if formality == FormalityNeutral { formality = s.formality } - return ctx.ContextString(), ctx.GenderString(), ctx.LocationString(), formality + location := ctx.LocationString() + if location == "" { + location = s.location + } + return ctx.ContextString(), ctx.GenderString(), location, formality } if subj, ok := data.(*Subject); ok && subj != nil { formality := subj.formality if formality == FormalityNeutral { formality = s.formality } - return "", subj.gender, subj.location, formality + location := subj.location + if location == "" { + location = s.location + } + return "", subj.gender, location, formality } if m, ok := data.(map[string]any); ok { var context string diff --git a/service_test.go b/service_test.go index 685065e..8b7179d 100644 --- a/service_test.go +++ b/service_test.go @@ -626,6 +626,38 @@ func TestServiceDefaultLocationContext(t *testing.T) { } } +func TestServiceDefaultLocationAppliesToTranslationContext(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + + svc.AddMessages("en", map[string]string{ + "welcome._greeting._workspace": "hello from workspace", + }) + svc.SetLocation("workspace") + + if got := svc.T("welcome", C("greeting")); got != "hello from workspace" { + t.Errorf("T(welcome, C(greeting)) with default location = %q, want %q", got, "hello from workspace") + } +} + +func TestServiceDefaultLocationAppliesToSubject(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + + svc.AddMessages("en", map[string]string{ + "welcome._workspace": "welcome aboard", + }) + svc.SetLocation("workspace") + + if got := svc.T("welcome", S("user", "Alice")); got != "welcome aboard" { + t.Errorf("T(welcome, Subject) with default location = %q, want %q", got, "welcome aboard") + } +} + func TestServiceTranslationContextExtrasInTemplates(t *testing.T) { svc, err := New() if err != nil {