Skip to content

Commit

Permalink
fixes nil reference exception if localization text is empty, fixes #207
Browse files Browse the repository at this point in the history
… (#208)
  • Loading branch information
MJacred authored Sep 28, 2020
1 parent 354b2fc commit 8f09be8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions v2/goi18n/merge_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,16 @@ func merge(messageFiles map[string][]byte, sourceLanguageTag language.Tag, outdi
}
templates := map[string]*i18n.MessageTemplate{}
for _, m := range mf.Messages {
templates[m.ID] = i18n.NewMessageTemplate(m)
template := i18n.NewMessageTemplate(m)
if template == nil {
continue
}
templates[m.ID] = template
}
if mf.Tag == sourceLanguageTag {
for _, template := range templates {
if sourceMessageTemplates[template.ID] != nil {
return nil, fmt.Errorf("multiple source translations for id %s", template.ID)
return nil, fmt.Errorf("multiple source translations for id %q", template.ID)
}
template.Hash = hash(template)
sourceMessageTemplates[template.ID] = template
Expand Down
32 changes: 32 additions & 0 deletions v2/goi18n/merge_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ func TestMerge(t *testing.T) {
"active.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"),
},
},
{
name: "single identity, one localization text missing",
sourceLanguage: language.AmericanEnglish,
inFiles: map[string][]byte{
"one.en-US.toml": []byte(`
1HelloMessage = ""
Body = "Finally some text!"
`),
},
outFiles: map[string][]byte{
"active.en-US.toml": []byte(`Body = "Finally some text!"
`),
},
},
{
name: "plural identity",
sourceLanguage: language.AmericanEnglish,
Expand All @@ -52,6 +66,24 @@ other = "{{.Count}} unread emails"
description = "Message that tells the user how many unread emails they have"
one = "{{.Count}} unread email"
other = "{{.Count}} unread emails"
`),
},
},
{
name: "plural identity, missing localization text",
sourceLanguage: language.AmericanEnglish,
inFiles: map[string][]byte{
"active.en-US.toml": []byte(`
Body = "Some text!"
[MissingTranslation]
description = "I wonder what it was?!"
one = ""
other = ""
`),
},
outFiles: map[string][]byte{
"active.en-US.toml": expectFile(`Body = "Some text!"
`),
},
},
Expand Down

0 comments on commit 8f09be8

Please sign in to comment.