Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide nice output of plural form not found error #147

Merged

Conversation

d2r2
Copy link
Contributor

@d2r2 d2r2 commented Dec 5, 2018

Rational:

If you run code example:

package main

import (
	"fmt"

	"github.com/BurntSushi/toml"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

func main() {
	bundle := &i18n.Bundle{DefaultLanguage: language.English}
	bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
	// No need to load active.en.toml since we are providing default translations.

	bundle.ParseMessageFileBytes([]byte(`

[Test1]
one = "One"
other = "Other"

		`),
		"active.en.toml")

	bundle.ParseMessageFileBytes([]byte(`

[Test1]
one = "Один"
other = "Другой"
#few = "Несколько"
#many = "Много"

		`), "active.ru.toml")

	localizer := i18n.NewLocalizer(bundle, "en")
	fmt.Println(fmt.Sprintf("en: %s", localizer.MustLocalize(
		&i18n.LocalizeConfig{MessageID: "Test1", PluralCount: 100})))

	localizer = i18n.NewLocalizer(bundle, "ru")
	fmt.Println(fmt.Sprintf("ru: %s", localizer.MustLocalize(
		&i18n.LocalizeConfig{MessageID: "Test1", PluralCount: 100})))
}

You will get output, which looks like a bug:

en: Other
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x539ba6]

goroutine 1 [running]:
github.com/nicksnyder/go-i18n/v2/internal.(*Template).parse(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5936f4, 0x4)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/internal/template.go:16 +0x26
github.com/nicksnyder/go-i18n/v2/internal.(*MessageTemplate).Execute(0xc00007e7d0, 0x5936f4, 0x4, 0x566ac0, 0xc0000f6840, 0x0, 0x4, 0xc00007e7d0, 0x20, 0x571420)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/internal/message_template.go:44 +0xa2
github.com/nicksnyder/go-i18n/v2/i18n.(*Localizer).LocalizeWithTag(0xc0000894a0, 0xc0000cbf08, 0x20, 0x571420, 0xc000089401, 0xc0000894a0, 0xc0000cbe08, 0x53b30f, 0x571420)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/i18n/localizer.go:142 +0x3c2
github.com/nicksnyder/go-i18n/v2/i18n.(*Localizer).Localize(0xc0000894a0, 0xc0000cbf08, 0x1, 0x1, 0x1, 0xc000089480)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/i18n/localizer.go:106 +0x35
github.com/nicksnyder/go-i18n/v2/i18n.(*Localizer).MustLocalize(0xc0000894a0, 0xc0000cbf08, 0x1, 0x1)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/i18n/localizer.go:205 +0x35
main.main()
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/example2/main.go:40 +0x456
exit status 2

Fix proposed by me resolve this and gives nice output (easy to understand what plural form is missed):

en: Other
panic: message "Test1" has no plural form "many" defined

goroutine 1 [running]:
github.com/nicksnyder/go-i18n/v2/i18n.(*Localizer).MustLocalize(0xc00000b500, 0xc0000b5f08, 0x1, 0x1)
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/i18n/localizer.go:207 +0x79
main.main()
	/home/ddyakov/Documents/gocode/src/github.com/nicksnyder/go-i18n/v2/example2/main.go:40 +0x456
exit status 2

@codecov
Copy link

codecov bot commented Dec 5, 2018

Codecov Report

Merging #147 into master will increase coverage by 0.08%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #147      +/-   ##
==========================================
+ Coverage   67.58%   67.66%   +0.08%     
==========================================
  Files          28       28              
  Lines        2298     2304       +6     
==========================================
+ Hits         1553     1559       +6     
  Misses        663      663              
  Partials       82       82
Impacted Files Coverage Δ
v2/internal/message_template.go 68.75% <66.66%> (+7.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc0ca3b...9c77f59. Read the comment docs.

@nicksnyder
Copy link
Owner

Thanks for finding and fixing this! Can you add a test for this?

@d2r2
Copy link
Contributor Author

d2r2 commented Dec 7, 2018

Hi @nicksnyder ! Sure, I'll try to figure out how to make the appropriate test and get back with update to commit.

@d2r2
Copy link
Contributor Author

d2r2 commented Dec 8, 2018

@nicksnyder, test added to the message_template_test.go.

@nicksnyder
Copy link
Owner

Thanks! I pushed a commit to use an error struct which makes testing easier. I will merge once CI passes.

@nicksnyder nicksnyder merged commit d698e2f into nicksnyder:master Dec 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants