Skip to content

Commit

Permalink
fix formatting of error for translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Nov 23, 2023
1 parent 33283b9 commit 6a32f3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion translation/file_loader.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package translation

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/bytedance/sonic"
"github.com/bytedance/sonic/decoder"

"github.com/goravel/framework/support/file"
)
Expand Down Expand Up @@ -40,9 +42,13 @@ func (f *FileLoader) Load(folder string, locale string) (map[string]map[string]s
return nil, err
}
if err := sonic.Unmarshal(data, &val); err != nil {
if _, ok := err.(decoder.SyntaxError); ok {
return nil, fmt.Errorf("translation file [%s] contains an invalid JSON structure", fullPath)
} else if _, ok := err.(*decoder.MismatchTypeError); ok {
return nil, fmt.Errorf("translation file [%s] contains mismatched types", fullPath)
}
return nil, err

Check warning on line 50 in translation/file_loader.go

View check run for this annotation

Codecov / codecov/patch

translation/file_loader.go#L48-L50

Added lines #L48 - L50 were not covered by tests
}

// Initialize the map if it's a nil
if translations[locale] == nil {
translations[locale] = make(map[string]string)
Expand Down
7 changes: 6 additions & 1 deletion translation/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Translator struct {
fallback string
loaded map[string]map[string]map[string]string
selector *MessageSelector
key string
}

// contextKey is an unexported type for keys defined in this package.
Expand Down Expand Up @@ -57,6 +58,10 @@ func (t *Translator) Choice(key string, number int, options ...translationcontra
}

func (t *Translator) Get(key string, options ...translationcontract.Option) (string, error) {
if t.key == "" {
t.key = key
}

locale := t.GetLocale()
// Check if a custom locale is provided in options.
if len(options) > 0 && options[0].Locale != "" {
Expand Down Expand Up @@ -97,7 +102,7 @@ func (t *Translator) Get(key string, options ...translationcontract.Option) (str
fallbackOptions.Locale = fallbackLocale
return t.Get(fallbackFolder+"."+keyPart, fallbackOptions)
}
return key, nil
return t.key, nil
}

// If the line doesn't contain any placeholders, we can return it right
Expand Down

0 comments on commit 6a32f3d

Please sign in to comment.