diff options
Diffstat (limited to 'vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go')
-rw-r--r-- | vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go b/vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go index 2a8faaf23..62bcd4355 100644 --- a/vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go +++ b/vendor/github.com/nicksnyder/go-i18n/v2/i18n/bundle.go @@ -4,44 +4,49 @@ import ( "fmt" "io/ioutil" - "github.com/nicksnyder/go-i18n/v2/internal" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" ) // UnmarshalFunc unmarshals data into v. -type UnmarshalFunc = internal.UnmarshalFunc +type UnmarshalFunc func(data []byte, v interface{}) error // Bundle stores a set of messages and pluralization rules. // Most applications only need a single bundle // that is initialized early in the application's lifecycle. +// 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 - - messageTemplates map[language.Tag]map[string]*internal.MessageTemplate + defaultLanguage language.Tag + unmarshalFuncs map[string]UnmarshalFunc + messageTemplates map[language.Tag]map[string]*MessageTemplate pluralRules plural.Rules tags []language.Tag matcher language.Matcher } -func (b *Bundle) init() { - if b.pluralRules == nil { - b.pluralRules = plural.DefaultRules() +// artTag is the language tag used for artifical languages +// https://en.wikipedia.org/wiki/Codes_for_constructed_languages +var artTag = language.MustParse("art") + +// NewBundle returns a bundle with a default language and a default set of plural rules. +func NewBundle(defaultLanguage language.Tag) *Bundle { + b := &Bundle{ + defaultLanguage: defaultLanguage, + pluralRules: plural.DefaultRules(), } - b.addTag(b.DefaultLanguage) + b.pluralRules[artTag] = b.pluralRules.Rule(language.English) + 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 @@ -62,16 +67,13 @@ func (b *Bundle) MustLoadMessageFile(path string) { } } -// MessageFile represents a parsed message file. -type MessageFile = internal.MessageFile - // ParseMessageFileBytes parses the bytes in buf to add translations to the bundle. // // The format of the file is everything after the last ".". // // 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 := ParseMessageFileBytes(buf, path, b.unmarshalFuncs) if err != nil { return nil, err } @@ -92,20 +94,19 @@ 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) } if b.messageTemplates == nil { - b.messageTemplates = map[language.Tag]map[string]*internal.MessageTemplate{} + b.messageTemplates = map[language.Tag]map[string]*MessageTemplate{} } if b.messageTemplates[tag] == nil { - b.messageTemplates[tag] = map[string]*internal.MessageTemplate{} + b.messageTemplates[tag] = map[string]*MessageTemplate{} b.addTag(tag) } for _, m := range messages { - b.messageTemplates[tag][m.ID] = internal.NewMessageTemplate(m) + b.messageTemplates[tag][m.ID] = NewMessageTemplate(m) } return nil } @@ -127,3 +128,9 @@ func (b *Bundle) addTag(tag language.Tag) { b.tags = append(b.tags, tag) b.matcher = language.NewMatcher(b.tags) } + +// LanguageTags returns the list of language tags +// of all the translations loaded into the bundle +func (b *Bundle) LanguageTags() []language.Tag { + return b.tags +} |