| 1 |
package i18n |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"github.com/gohugoio/go-i18n/v2/internal/plural" |
| 8 |
|
| 9 |
"golang.org/x/text/language" |
| 10 |
) |
| 11 |
|
| 12 |
// UnmarshalFunc unmarshals data into v. |
| 13 |
type UnmarshalFunc func(data []byte, v interface{}) error |
| 14 |
|
| 15 |
// Bundle stores a set of messages and pluralization rules. |
| 16 |
// Most applications only need a single bundle |
| 17 |
// that is initialized early in the application's lifecycle. |
| 18 |
// It is not goroutine safe to modify the bundle while Localizers |
| 19 |
// are reading from it. |
| 20 |
type Bundle struct { |
| 21 |
defaultLanguage language.Tag |
| 22 |
unmarshalFuncs map[string]UnmarshalFunc |
| 23 |
messageTemplates map[language.Tag]map[string]*MessageTemplate |
| 24 |
pluralRules plural.Rules |
| 25 |
tags []language.Tag |
| 26 |
matcher language.Matcher |
| 27 |
} |
| 28 |
|
| 29 |
// The matcher in x/text/language does not handle artificial languages, |
| 30 |
// see https://github.com/golang/go/issues/45749 |
| 31 |
// This is a simplified matcher that delegates to the x/text/language matcher for |
| 32 |
// the harder cases. |
| 33 |
type matcher struct { |
| 34 |
tags []language.Tag |
| 35 |
defaultMatcher language.Matcher |
| 36 |
} |
| 37 |
|
| 38 |
func newMatcher(tags []language.Tag) language.Matcher { |
| 39 |
var hasArt bool |
| 40 |
for _, tag := range tags { |
| 41 |
base, _ := tag.Base() |
| 42 |
hasArt = base == artTagBase |
| 43 |
if hasArt { |
| 44 |
break |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if !hasArt { |
| 49 |
return language.NewMatcher(tags) |
| 50 |
} |
| 51 |
|
| 52 |
return matcher{ |
| 53 |
tags: tags, |
| 54 |
defaultMatcher: language.NewMatcher(tags), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func (m matcher) Match(t ...language.Tag) (language.Tag, int, language.Confidence) { |
| 59 |
for _, candidate := range t { |
| 60 |
base, _ := candidate.Base() |
| 61 |
if base != artTagBase { |
| 62 |
continue |
| 63 |
} |
| 64 |
|
| 65 |
for i, tag := range m.tags { |
| 66 |
if tag == candidate { |
| 67 |
return candidate, i, language.Exact |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return m.defaultMatcher.Match(t...) |
| 73 |
} |
| 74 |
|
| 75 |
// artTag is the language tag used for artificial languages |
| 76 |
// https://en.wikipedia.org/wiki/Codes_for_constructed_languages |
| 77 |
var ( |
| 78 |
artTag = language.MustParse("art") |
| 79 |
artTagBase, _ = artTag.Base() |
| 80 |
) |
| 81 |
|
| 82 |
// NewBundle returns a bundle with a default language and a default set of plural rules. |
| 83 |
func NewBundle(defaultLanguage language.Tag) *Bundle { |
| 84 |
b := &Bundle{ |
| 85 |
defaultLanguage: defaultLanguage, |
| 86 |
pluralRules: plural.DefaultRules(), |
| 87 |
} |
| 88 |
b.pluralRules[artTag] = b.pluralRules.Rule(language.English) |
| 89 |
b.addTag(defaultLanguage) |
| 90 |
return b |
| 91 |
} |
| 92 |
|
| 93 |
// RegisterUnmarshalFunc registers an UnmarshalFunc for format. |
| 94 |
func (b *Bundle) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFunc) { |
| 95 |
if b.unmarshalFuncs == nil { |
| 96 |
b.unmarshalFuncs = make(map[string]UnmarshalFunc) |
| 97 |
} |
| 98 |
b.unmarshalFuncs[format] = unmarshalFunc |
| 99 |
} |
| 100 |
|
| 101 |
// LoadMessageFile loads the bytes from path |
| 102 |
// and then calls ParseMessageFileBytes. |
| 103 |
func (b *Bundle) LoadMessageFile(path string) (*MessageFile, error) { |
| 104 |
buf, err := os.ReadFile(path) |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
return b.ParseMessageFileBytes(buf, path) |
| 109 |
} |
| 110 |
|
| 111 |
// MustLoadMessageFile is similar to LoadMessageFile |
| 112 |
// except it panics if an error happens. |
| 113 |
func (b *Bundle) MustLoadMessageFile(path string) { |
| 114 |
if _, err := b.LoadMessageFile(path); err != nil { |
| 115 |
panic(err) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// ParseMessageFileBytes parses the bytes in buf to add translations to the bundle. |
| 120 |
// |
| 121 |
// The format of the file is everything after the last ".". |
| 122 |
// |
| 123 |
// The language tag of the file is everything after the second to last "." or after the last path separator, but before the format. |
| 124 |
func (b *Bundle) ParseMessageFileBytes(buf []byte, path string) (*MessageFile, error) { |
| 125 |
messageFile, err := ParseMessageFileBytes(buf, path, b.unmarshalFuncs) |
| 126 |
if err != nil { |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
if err := b.AddMessages(messageFile.Tag, messageFile.Messages...); err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
return messageFile, nil |
| 133 |
} |
| 134 |
|
| 135 |
// MustParseMessageFileBytes is similar to ParseMessageFileBytes |
| 136 |
// except it panics if an error happens. |
| 137 |
func (b *Bundle) MustParseMessageFileBytes(buf []byte, path string) { |
| 138 |
if _, err := b.ParseMessageFileBytes(buf, path); err != nil { |
| 139 |
panic(err) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// AddMessages adds messages for a language. |
| 144 |
// It is useful if your messages are in a format not supported by ParseMessageFileBytes. |
| 145 |
func (b *Bundle) AddMessages(tag language.Tag, messages ...*Message) error { |
| 146 |
pluralRule := b.pluralRules.Rule(tag) |
| 147 |
if pluralRule == nil { |
| 148 |
return fmt.Errorf("no plural rule registered for %s", tag) |
| 149 |
} |
| 150 |
if b.messageTemplates == nil { |
| 151 |
b.messageTemplates = map[language.Tag]map[string]*MessageTemplate{} |
| 152 |
} |
| 153 |
if b.messageTemplates[tag] == nil { |
| 154 |
b.messageTemplates[tag] = map[string]*MessageTemplate{} |
| 155 |
b.addTag(tag) |
| 156 |
} |
| 157 |
for _, m := range messages { |
| 158 |
b.messageTemplates[tag][m.ID] = NewMessageTemplate(m) |
| 159 |
} |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
// MustAddMessages is similar to AddMessages except it panics if an error happens. |
| 164 |
func (b *Bundle) MustAddMessages(tag language.Tag, messages ...*Message) { |
| 165 |
if err := b.AddMessages(tag, messages...); err != nil { |
| 166 |
panic(err) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func (b *Bundle) addTag(tag language.Tag) { |
| 171 |
for _, t := range b.tags { |
| 172 |
if t == tag { |
| 173 |
// Tag already exists |
| 174 |
return |
| 175 |
} |
| 176 |
} |
| 177 |
b.tags = append(b.tags, tag) |
| 178 |
b.matcher = newMatcher(b.tags) |
| 179 |
} |
| 180 |
|
| 181 |
// LanguageTags returns the list of language tags |
| 182 |
// of all the translations loaded into the bundle |
| 183 |
func (b *Bundle) LanguageTags() []language.Tag { |
| 184 |
return b.tags |
| 185 |
} |
| 186 |
|
| 187 |
func (b *Bundle) getMessageTemplate(tag language.Tag, id string) *MessageTemplate { |
| 188 |
templates := b.messageTemplates[tag] |
| 189 |
if templates == nil { |
| 190 |
return nil |
| 191 |
} |
| 192 |
return templates[id] |
| 193 |
} |