fix(i18n): normalise fallback language tags
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
3c32b7a7ef
commit
7e0f6cb13a
2 changed files with 18 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue