Skip to content

Commit

Permalink
Fix locale loading & defaulting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
darh committed Apr 5, 2022
1 parent 321e322 commit 964b71a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions pkg/locale/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func (svc *service) SupportedLang(tag language.Tag) bool {
// all translation files
func (svc *service) ReloadStatic() (err error) {
var (
i int
lang *Language
ll, aux []*Language

logFields = make([]zap.Field, 0)
Expand All @@ -141,6 +139,10 @@ func (svc *service) ReloadStatic() (err error) {
svc.l.Lock()
defer svc.l.Unlock()

if len(svc.tags) == 0 {
return fmt.Errorf("no supported languages (LOCALE_LANGUAGES is empty)")
}

svc.log.Info("reloading static",
zap.Strings("path", svc.src),
zap.Strings("tags", tagsToStrings(svc.tags)),
Expand Down Expand Up @@ -178,7 +180,7 @@ func (svc *service) ReloadStatic() (err error) {
ll = append(ll, aux...)
}

for i, lang = range ll {
for _, lang := range ll {
logFields = []zap.Field{
zap.Stringer("tag", lang.Tag),
}
Expand Down Expand Up @@ -206,11 +208,6 @@ func (svc *service) ReloadStatic() (err error) {
return err
}

if i == 0 && svc.def == nil {
// set first one as default
svc.def = lang
}

if svc.set[lang.Tag] != nil {
svc.log.Info(
"language overloaded",
Expand All @@ -234,7 +231,7 @@ func (svc *service) ReloadStatic() (err error) {
}

// Do another pass and link all extended languages
for _, lang = range svc.set {
for _, lang := range svc.set {
if lang.Extends.IsRoot() {
continue
}
Expand All @@ -246,6 +243,16 @@ func (svc *service) ReloadStatic() (err error) {
lang.extends = svc.set[lang.Extends]
}

if svc.def == nil {
first := svc.tags[0]

if svc.set[first] == nil {
return fmt.Errorf("could not find %s in loaded languages", first)
}

svc.def = svc.set[first]
}

return nil
}

Expand Down

0 comments on commit 964b71a

Please sign in to comment.