diff --git a/service.go b/service.go index 503215f..8aac8fd 100644 --- a/service.go +++ b/service.go @@ -40,7 +40,7 @@ type Option func(*Service) // WithFallback sets the fallback language for missing translations. func WithFallback(lang string) Option { - return func(s *Service) { s.fallbackLang = lang } + return func(s *Service) { s.fallbackLang = normalizeLanguageTag(lang) } } // WithFormality sets the default formality level. @@ -290,7 +290,7 @@ 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) SetFallback(lang string) { s.mu.Lock(); s.fallbackLang = normalizeLanguageTag(lang); s.mu.Unlock() } func (s *Service) Fallback() string { s.mu.RLock() defer s.mu.RUnlock() diff --git a/service_test.go b/service_test.go index 9d34e01..8c41676 100644 --- a/service_test.go +++ b/service_test.go @@ -370,6 +370,22 @@ func TestServiceFallback(t *testing.T) { } } +func TestServiceFallbackNormalisesLanguageTag(t *testing.T) { + svc, err := New(WithFallback("fr_CA")) + if err != nil { + t.Fatalf("New() failed: %v", err) + } + + if got, want := svc.Fallback(), "fr-CA"; got != want { + t.Fatalf("WithFallback(fr_CA) = %q, want %q", got, want) + } + + svc.SetFallback("en_US") + if got, want := svc.Fallback(), "en-US"; got != want { + t.Fatalf("SetFallback(en_US) = %q, want %q", got, want) + } +} + func TestServiceMessageFallbackUsesBaseLanguageTagBeforeConfiguredFallback(t *testing.T) { svc, err := NewWithLoader(messageBaseFallbackLoader{}) if err != nil {