fix(i18n): guard against nil loaders
This commit is contained in:
parent
7b36000b7d
commit
d95218917e
2 changed files with 23 additions and 0 deletions
|
|
@ -103,6 +103,9 @@ func NewWithFS(fsys fs.FS, dir string, opts ...Option) (*Service, error) {
|
|||
|
||||
// NewWithLoader creates a new i18n service with a custom loader.
|
||||
func NewWithLoader(loader Loader, opts ...Option) (*Service, error) {
|
||||
if loader == nil {
|
||||
return nil, log.E("NewWithLoader", "nil loader", nil)
|
||||
}
|
||||
s := &Service{
|
||||
loader: loader,
|
||||
messages: make(map[string]map[string]Message),
|
||||
|
|
@ -836,6 +839,9 @@ func (s *Service) AddMessages(lang string, messages map[string]string) {
|
|||
// and grammar data into the existing service. This is the correct way to
|
||||
// add package-specific translations at runtime.
|
||||
func (s *Service) AddLoader(loader Loader) error {
|
||||
if loader == nil {
|
||||
return log.E("Service.AddLoader", "nil loader", nil)
|
||||
}
|
||||
langs := loader.Languages()
|
||||
for _, lang := range langs {
|
||||
messages, grammar, err := loader.Load(lang)
|
||||
|
|
|
|||
|
|
@ -986,6 +986,17 @@ func TestServiceAddLoader_Bad(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestServiceAddLoader_Nil(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New() failed: %v", err)
|
||||
}
|
||||
|
||||
if err := svc.AddLoader(nil); err == nil {
|
||||
t.Error("AddLoader() should fail with nil loader")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelAddLoader(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
|
|
@ -1047,6 +1058,12 @@ func TestNewWithLoaderNoLanguages(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNewWithLoaderNil(t *testing.T) {
|
||||
if _, err := NewWithLoader(nil); err == nil {
|
||||
t.Error("NewWithLoader(nil) should fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceIsRTL(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue