Skip to content

Commit

Permalink
fallback to empty string message "" when get nil value (#326)
Browse files Browse the repository at this point in the history
fallback to empty string message when get nil value
  • Loading branch information
LemonNekoGH committed Mar 31, 2024
1 parent 521f196 commit b42d9bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion i18n/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func stringSubmap(k string, v interface{}, strdata map[string]string) error {
func isMessage(v interface{}) bool {
reservedKeys := []string{"id", "description", "hash", "leftdelim", "rightdelim", "zero", "one", "two", "few", "many", "other"}
switch data := v.(type) {
case string:
case nil, string:
return true
case map[string]interface{}:
for _, key := range reservedKeys {
Expand Down
7 changes: 7 additions & 0 deletions i18n/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func recGetMessages(raw interface{}, isMapMessage, isInitialCall bool) ([]*Messa
messages = append(messages, childMessages...)
}

case nil:
if isInitialCall {
return nil, errInvalidTranslationFile
}
m, err := NewMessage("")
return []*Message{m}, err

default:
return nil, fmt.Errorf("unsupported file format %T", raw)
}
Expand Down
31 changes: 31 additions & 0 deletions i18n/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ outer:
}},
},
},
{
name: "YAML empty key test",
file: `
some-keys:
non-empty-key: not empty
empty-key-but-type-specified: ""
empty-key:
null-key: null`,
path: "en.yaml",
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal},
messageFile: &MessageFile{
Path: "en.yaml",
Tag: language.English,
Format: "yaml",
Messages: []*Message{
{
ID: "some-keys.non-empty-key",
Other: "not empty",
},
{
ID: "some-keys.empty-key-but-type-specified",
},
{
ID: "some-keys.empty-key",
},
{
ID: "some-keys.null-key",
},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
Expand Down

0 comments on commit b42d9bd

Please sign in to comment.