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

add NewBundle and remove init #163

Merged
merged 4 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v2/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var page = template.Must(template.New("").Parse(`
`))

func main() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
// No need to load active.en.toml since we are providing default translations.
// bundle.MustLoadMessageFile("active.en.toml")
Expand Down
Empty file added v2/goi18n/active.en.toml
Empty file.
4 changes: 2 additions & 2 deletions v2/goi18n/extract_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestExtract(t *testing.T) {
import "github.com/nicksnyder/go-i18n/v2/i18n"

func main() {
bundle := &i18n.Bundle{}
bundle := i18n.NewBundle(language.English)
l := i18n.NewLocalizer(bundle, "en")
l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
}
Expand All @@ -66,7 +66,7 @@ func TestExtract(t *testing.T) {
import "github.com/nicksnyder/go-i18n/v2/i18n"

func main() {
bundle := &i18n.Bundle{}
bundle := i18n.NewBundle(language.English)
l := i18n.NewLocalizer(bundle, "en")
l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
}
Expand Down
27 changes: 12 additions & 15 deletions v2/i18n/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,29 @@ type UnmarshalFunc = internal.UnmarshalFunc
// It is not goroutine safe to modify the bundle while Localizers
// are reading from it.
type Bundle struct {
// DefaultLanguage is the default language of the bundle.
DefaultLanguage language.Tag

// UnmarshalFuncs is a map of file extensions to UnmarshalFuncs.
UnmarshalFuncs map[string]UnmarshalFunc

defaultLanguage language.Tag
unmarshalFuncs map[string]UnmarshalFunc
messageTemplates map[language.Tag]map[string]*internal.MessageTemplate
pluralRules plural.Rules
tags []language.Tag
matcher language.Matcher
}

func (b *Bundle) init() {
if b.pluralRules == nil {
b.pluralRules = plural.DefaultRules()
func NewBundle(defaultLanguage language.Tag) *Bundle {
b := &Bundle{
defaultLanguage: defaultLanguage,
pluralRules: plural.DefaultRules(),
}
b.addTag(b.DefaultLanguage)
b.addTag(defaultLanguage)
return b
}

// RegisterUnmarshalFunc registers an UnmarshalFunc for format.
func (b *Bundle) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFunc) {
if b.UnmarshalFuncs == nil {
b.UnmarshalFuncs = make(map[string]UnmarshalFunc)
if b.unmarshalFuncs == nil {
b.unmarshalFuncs = make(map[string]UnmarshalFunc)
}
b.UnmarshalFuncs[format] = unmarshalFunc
b.unmarshalFuncs[format] = unmarshalFunc
}

// LoadMessageFile loads the bytes from path
Expand Down Expand Up @@ -73,7 +71,7 @@ type MessageFile = internal.MessageFile
//
// The language tag of the file is everything after the second to last "." or after the last path separator, but before the format.
func (b *Bundle) ParseMessageFileBytes(buf []byte, path string) (*MessageFile, error) {
messageFile, err := internal.ParseMessageFileBytes(buf, path, b.UnmarshalFuncs)
messageFile, err := internal.ParseMessageFileBytes(buf, path, b.unmarshalFuncs)
if err != nil {
return nil, err
}
Expand All @@ -94,7 +92,6 @@ func (b *Bundle) MustParseMessageFileBytes(buf []byte, path string) {
// AddMessages adds messages for a language.
// It is useful if your messages are in a format not supported by ParseMessageFileBytes.
func (b *Bundle) AddMessages(tag language.Tag, messages ...*Message) error {
b.init()
pluralRule := b.pluralRules.Rule(tag)
if pluralRule == nil {
return fmt.Errorf("no plural rule registered for %s", tag)
Expand Down
16 changes: 8 additions & 8 deletions v2/i18n/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var everythingMessage = internal.MustNewMessage(map[string]string{
})

func TestPseudoLanguage(t *testing.T) {
bundle := &Bundle{DefaultLanguage: language.English}
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
expected := "simple simple"
bundle.MustParseMessageFileBytes([]byte(`
Expand All @@ -51,7 +51,7 @@ simple = "simple simple"
}

func TestPseudoLanguagePlural(t *testing.T) {
bundle := &Bundle{DefaultLanguage: language.English}
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
[everything]
Expand Down Expand Up @@ -86,7 +86,7 @@ zero = "zero translation"
}

func TestJSON(t *testing.T) {
var bundle Bundle
bundle := NewBundle(language.English)
bundle.MustParseMessageFileBytes([]byte(`{
"simple": "simple translation",
"detail": {
Expand All @@ -110,7 +110,7 @@ func TestJSON(t *testing.T) {
}

func TestYAML(t *testing.T) {
var bundle Bundle
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
# Comment
Expand Down Expand Up @@ -138,7 +138,7 @@ everything:
}

func TestTOML(t *testing.T) {
var bundle Bundle
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
# Comment
Expand Down Expand Up @@ -166,7 +166,7 @@ other = "other translation"
}

func TestV1Format(t *testing.T) {
var bundle Bundle
bundle := NewBundle(language.English)
bundle.MustParseMessageFileBytes([]byte(`[
{
"id": "simple",
Expand All @@ -193,7 +193,7 @@ func TestV1Format(t *testing.T) {
}

func TestV1FlatFormat(t *testing.T) {
var bundle Bundle
bundle := NewBundle(language.English)
bundle.MustParseMessageFileBytes([]byte(`{
"simple": {
"other": "simple translation"
Expand All @@ -215,7 +215,7 @@ func TestV1FlatFormat(t *testing.T) {
expectMessage(t, bundle, language.AmericanEnglish, "everything", &e)
}

func expectMessage(t *testing.T, bundle Bundle, tag language.Tag, messageID string, message *Message) {
func expectMessage(t *testing.T, bundle *Bundle, tag language.Tag, messageID string, message *Message) {
expected := internal.NewMessageTemplate(message)
actual := bundle.messageTemplates[tag][messageID]
if !reflect.DeepEqual(actual, expected) {
Expand Down
2 changes: 1 addition & 1 deletion v2/i18n/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// according to a set of locale preferences.
//
// Create a Bundle to use for the lifetime of your application.
// bundle := &i18n.Bundle{DefaultLanguage: language.English}
// bundle := i18n.NewBundle(language.English)
//
// Load translations into your bundle during initialization.
// bundle.LoadMessageFile("en-US.yaml")
Expand Down
12 changes: 6 additions & 6 deletions v2/i18n/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func ExampleLocalizer_MustLocalize() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
localizer := i18n.NewLocalizer(bundle, "en")
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
Expand All @@ -22,7 +22,7 @@ func ExampleLocalizer_MustLocalize() {
}

func ExampleLocalizer_MustLocalize_noDefaultMessage() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
HelloWorld = "Hello World!"
Expand All @@ -45,7 +45,7 @@ HelloWorld = "Hola Mundo!"
}

func ExampleLocalizer_MustLocalize_plural() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
localizer := i18n.NewLocalizer(bundle, "en")
catsMessage := &i18n.Message{
ID: "Cats",
Expand All @@ -71,7 +71,7 @@ func ExampleLocalizer_MustLocalize_plural() {
}

func ExampleLocalizer_MustLocalize_template() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
localizer := i18n.NewLocalizer(bundle, "en")
helloPersonMessage := &i18n.Message{
ID: "HelloPerson",
Expand All @@ -86,7 +86,7 @@ func ExampleLocalizer_MustLocalize_template() {
}

func ExampleLocalizer_MustLocalize_plural_template() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
localizer := i18n.NewLocalizer(bundle, "en")
personCatsMessage := &i18n.Message{
ID: "PersonCats",
Expand Down Expand Up @@ -124,7 +124,7 @@ func ExampleLocalizer_MustLocalize_plural_template() {
}

func ExampleLocalizer_MustLocalize_customTemplateDelims() {
bundle := &i18n.Bundle{DefaultLanguage: language.English}
bundle := i18n.NewBundle(language.English)
localizer := i18n.NewLocalizer(bundle, "en")
helloPersonMessage := &i18n.Message{
ID: "HelloPerson",
Expand Down
7 changes: 3 additions & 4 deletions v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Localizer struct {
// in the bundle according to the language preferences in langs.
// It can parse Accept-Language headers as defined in http://www.ietf.org/rfc/rfc2616.txt.
func NewLocalizer(bundle *Bundle, langs ...string) *Localizer {
bundle.init()
return &Localizer{
bundle: bundle,
tags: parseTags(langs),
Expand Down Expand Up @@ -155,15 +154,15 @@ func (l *Localizer) getTemplate(id string, defaultMessage *Message) (language.Ta
}

if len(l.bundle.tags) <= 1 {
return l.bundle.DefaultLanguage, nil
return l.bundle.defaultLanguage, nil
}

// Slow path.
// We didn't find a translation for the tag suggested by the default matcher
// so we need to create a new matcher that contains only the tags in the bundle
// that have this message.
foundTags := make([]language.Tag, 0, len(l.bundle.messageTemplates)+1)
foundTags = append(foundTags, l.bundle.DefaultLanguage)
foundTags = append(foundTags, l.bundle.defaultLanguage)

for t, templates := range l.bundle.messageTemplates {
template := templates[id]
Expand All @@ -183,7 +182,7 @@ func (l *Localizer) matchTemplate(id string, defaultMessage *Message, matcher la
if templates != nil && templates[id] != nil {
return tag, templates[id]
}
if tag == l.bundle.DefaultLanguage && defaultMessage != nil {
if tag == l.bundle.defaultLanguage && defaultMessage != nil {
return tag, internal.NewMessageTemplate(defaultMessage)
}
return tag, nil
Expand Down
6 changes: 4 additions & 2 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,11 @@ func TestLocalizer_Localize(t *testing.T) {
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
bundle := &Bundle{DefaultLanguage: testCase.defaultLanguage}
bundle := NewBundle(testCase.defaultLanguage)
for tag, messages := range testCase.messages {
bundle.AddMessages(tag, messages...)
if err := bundle.AddMessages(tag, messages...); err != nil {
t.Fatal(err)
}
}
localizer := NewLocalizer(bundle, testCase.acceptLangs...)
localized, err := localizer.Localize(testCase.conf)
Expand Down