diff options
author | Corey Hulen <corey@hulen.com> | 2016-01-19 13:33:57 -0600 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2016-01-19 13:33:57 -0600 |
commit | 36c5c46e24f745ee80b49f47363217fcb740ce53 (patch) | |
tree | d66189c2ba3c76b76488a3d0dd6ae210ddfc0096 /Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go | |
parent | 80bfdf71f2ab7271dd199c61229fa2b8a7e0213c (diff) | |
parent | 4389571dedb9a68d801427c37ad971c8c488991f (diff) | |
download | chat-36c5c46e24f745ee80b49f47363217fcb740ce53.tar.gz chat-36c5c46e24f745ee80b49f47363217fcb740ce53.tar.bz2 chat-36c5c46e24f745ee80b49f47363217fcb740ce53.zip |
Merge pull request #1921 from mattermost/PLT-7-server-libs
PLT-7 adding server side libraries
Diffstat (limited to 'Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go new file mode 100644 index 000000000..fa93180b8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/translation.go @@ -0,0 +1,83 @@ +// Package translation defines the interface for a translation. +package translation + +import ( + "fmt" + + "github.com/nicksnyder/go-i18n/i18n/language" +) + +// Translation is the interface that represents a translated string. +type Translation interface { + // MarshalInterface returns the object that should be used + // to serialize the translation. + MarshalInterface() interface{} + ID() string + Template(language.Plural) *template + UntranslatedCopy() Translation + Normalize(language *language.Language) Translation + Backfill(src Translation) Translation + Merge(Translation) Translation + Incomplete(l *language.Language) bool +} + +// SortableByID implements sort.Interface for a slice of translations. +type SortableByID []Translation + +func (a SortableByID) Len() int { return len(a) } +func (a SortableByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a SortableByID) Less(i, j int) bool { return a[i].ID() < a[j].ID() } + +// NewTranslation reflects on data to create a new Translation. +// +// data["id"] must be a string and data["translation"] must be either a string +// for a non-plural translation or a map[string]interface{} for a plural translation. +func NewTranslation(data map[string]interface{}) (Translation, error) { + id, ok := data["id"].(string) + if !ok { + return nil, fmt.Errorf(`missing "id" key`) + } + var pluralObject map[string]interface{} + switch translation := data["translation"].(type) { + case string: + tmpl, err := newTemplate(translation) + if err != nil { + return nil, err + } + return &singleTranslation{id, tmpl}, nil + case map[interface{}]interface{}: + // The YAML parser uses interface{} keys so we first convert them to string keys. + pluralObject = make(map[string]interface{}) + for k, v := range translation { + kStr, ok := k.(string) + if !ok { + return nil, fmt.Errorf(`invalid plural category type %T; expected string`, k) + } + pluralObject[kStr] = v + } + case map[string]interface{}: + pluralObject = translation + case nil: + return nil, fmt.Errorf(`missing "translation" key`) + default: + return nil, fmt.Errorf(`unsupported type for "translation" key %T`, translation) + } + + templates := make(map[language.Plural]*template, len(pluralObject)) + for k, v := range pluralObject { + pc, err := language.NewPlural(k) + if err != nil { + return nil, err + } + str, ok := v.(string) + if !ok { + return nil, fmt.Errorf(`plural category "%s" has value of type %T; expected string`, pc, v) + } + tmpl, err := newTemplate(str) + if err != nil { + return nil, err + } + templates[pc] = tmpl + } + return &pluralTranslation{id, templates}, nil +} |