| 1 |
package i18n |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
texttemplate "text/template" |
| 6 |
|
| 7 |
"github.com/gohugoio/go-i18n/v2/i18n/template" |
| 8 |
"github.com/gohugoio/go-i18n/v2/internal/plural" |
| 9 |
"golang.org/x/text/language" |
| 10 |
) |
| 11 |
|
| 12 |
// Localizer provides Localize and MustLocalize methods that return localized messages. |
| 13 |
// Localize and MustLocalize methods use a language.Tag matching algorithm based |
| 14 |
// on the best possible value. This algorithm may cause an unexpected language.Tag returned |
| 15 |
// value depending on the order of the tags stored in memory. For example, if the bundle |
| 16 |
// used to create a Localizer instance ingested locales following this order |
| 17 |
// ["en-US", "en-GB", "en-IE", "en"] and the locale "en" is asked, the underlying matching |
| 18 |
// algorithm will return "en-US" thinking it is the best match possible. More information |
| 19 |
// about the algorithm in this Github issue: https://github.com/golang/go/issues/49176. |
| 20 |
// There is additional informations inside the Go code base: |
| 21 |
// https://github.com/golang/text/blob/master/language/match.go#L142 |
| 22 |
type Localizer struct { |
| 23 |
// bundle contains the messages that can be returned by the Localizer. |
| 24 |
bundle *Bundle |
| 25 |
|
| 26 |
// tags is the list of language tags that the Localizer checks |
| 27 |
// in order when localizing a message. |
| 28 |
tags []language.Tag |
| 29 |
} |
| 30 |
|
| 31 |
// NewLocalizer returns a new Localizer that looks up messages |
| 32 |
// in the bundle according to the language preferences in langs. |
| 33 |
// It can parse Accept-Language headers as defined in http://www.ietf.org/rfc/rfc2616.txt. |
| 34 |
func NewLocalizer(bundle *Bundle, langs ...string) *Localizer { |
| 35 |
return &Localizer{ |
| 36 |
bundle: bundle, |
| 37 |
tags: parseTags(langs), |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func parseTags(langs []string) []language.Tag { |
| 42 |
tags := []language.Tag{} |
| 43 |
for _, lang := range langs { |
| 44 |
t, _, err := language.ParseAcceptLanguage(lang) |
| 45 |
if err != nil { |
| 46 |
continue |
| 47 |
} |
| 48 |
tags = append(tags, t...) |
| 49 |
} |
| 50 |
return tags |
| 51 |
} |
| 52 |
|
| 53 |
// LocalizeConfig configures a call to the Localize method on Localizer. |
| 54 |
type LocalizeConfig struct { |
| 55 |
// MessageID is the id of the message to lookup. |
| 56 |
// This field is ignored if DefaultMessage is set. |
| 57 |
MessageID string |
| 58 |
|
| 59 |
// TemplateData is the data passed when executing the message's template. |
| 60 |
// If TemplateData is nil and PluralCount is not nil, then the message template |
| 61 |
// will be executed with data that contains the plural count. |
| 62 |
TemplateData interface{} |
| 63 |
|
| 64 |
// PluralCount determines which plural form of the message is used. |
| 65 |
PluralCount interface{} |
| 66 |
|
| 67 |
// DefaultMessage is used if the message is not found in any message files. |
| 68 |
DefaultMessage *Message |
| 69 |
|
| 70 |
// Funcs is used to configure a template.TextParser if TemplateParser is not set. |
| 71 |
Funcs texttemplate.FuncMap |
| 72 |
|
| 73 |
// The TemplateParser to use for parsing templates. |
| 74 |
// If one is not set, a template.TextParser is used (configured with Funcs if it is set). |
| 75 |
TemplateParser template.Parser |
| 76 |
} |
| 77 |
|
| 78 |
var defaultTextParser = &template.TextParser{} |
| 79 |
|
| 80 |
func (lc *LocalizeConfig) getTemplateParser() template.Parser { |
| 81 |
if lc.TemplateParser != nil { |
| 82 |
return lc.TemplateParser |
| 83 |
} |
| 84 |
if lc.Funcs != nil { |
| 85 |
return &template.TextParser{ |
| 86 |
Funcs: lc.Funcs, |
| 87 |
} |
| 88 |
} |
| 89 |
return defaultTextParser |
| 90 |
} |
| 91 |
|
| 92 |
type invalidPluralCountErr struct { |
| 93 |
messageID string |
| 94 |
pluralCount interface{} |
| 95 |
err error |
| 96 |
} |
| 97 |
|
| 98 |
func (e *invalidPluralCountErr) Error() string { |
| 99 |
return fmt.Sprintf("invalid plural count %#v for message id %q: %s", e.pluralCount, e.messageID, e.err) |
| 100 |
} |
| 101 |
|
| 102 |
// MessageNotFoundErr is returned from Localize when a message could not be found. |
| 103 |
type MessageNotFoundErr struct { |
| 104 |
Tag language.Tag |
| 105 |
MessageID string |
| 106 |
} |
| 107 |
|
| 108 |
func (e *MessageNotFoundErr) Error() string { |
| 109 |
return fmt.Sprintf("message %q not found in language %q", e.MessageID, e.Tag) |
| 110 |
} |
| 111 |
|
| 112 |
type messageIDMismatchErr struct { |
| 113 |
messageID string |
| 114 |
defaultMessageID string |
| 115 |
} |
| 116 |
|
| 117 |
func (e *messageIDMismatchErr) Error() string { |
| 118 |
return fmt.Sprintf("message id %q does not match default message id %q", e.messageID, e.defaultMessageID) |
| 119 |
} |
| 120 |
|
| 121 |
// Localize returns a localized message. |
| 122 |
func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { |
| 123 |
msg, _, err := l.LocalizeWithTag(lc) |
| 124 |
return msg, err |
| 125 |
} |
| 126 |
|
| 127 |
// LocalizeMessage returns a localized message. |
| 128 |
func (l *Localizer) LocalizeMessage(msg *Message) (string, error) { |
| 129 |
return l.Localize(&LocalizeConfig{ |
| 130 |
DefaultMessage: msg, |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
// TODO: uncomment this (and the test) when extract has been updated to extract these call sites too. |
| 135 |
// Localize returns a localized message. |
| 136 |
// func (l *Localizer) LocalizeMessageID(messageID string) (string, error) { |
| 137 |
// return l.Localize(&LocalizeConfig{ |
| 138 |
// MessageID: messageID, |
| 139 |
// }) |
| 140 |
// } |
| 141 |
|
| 142 |
// LocalizeWithTag returns a localized message and the language tag. |
| 143 |
// It may return a best effort localized message even if an error happens. |
| 144 |
func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, error) { |
| 145 |
messageID := lc.MessageID |
| 146 |
if lc.DefaultMessage != nil { |
| 147 |
if messageID != "" && messageID != lc.DefaultMessage.ID { |
| 148 |
return "", language.Und, &messageIDMismatchErr{messageID: messageID, defaultMessageID: lc.DefaultMessage.ID} |
| 149 |
} |
| 150 |
messageID = lc.DefaultMessage.ID |
| 151 |
} |
| 152 |
|
| 153 |
var operands *plural.Operands |
| 154 |
templateData := lc.TemplateData |
| 155 |
if lc.PluralCount != nil { |
| 156 |
var err error |
| 157 |
operands, err = plural.NewOperands(lc.PluralCount) |
| 158 |
if err != nil { |
| 159 |
return "", language.Und, &invalidPluralCountErr{messageID: messageID, pluralCount: lc.PluralCount, err: err} |
| 160 |
} |
| 161 |
if templateData == nil { |
| 162 |
templateData = map[string]interface{}{ |
| 163 |
"PluralCount": lc.PluralCount, |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
tag, template, err := l.getMessageTemplate(messageID, lc.DefaultMessage) |
| 169 |
if template == nil { |
| 170 |
return "", language.Und, err |
| 171 |
} |
| 172 |
|
| 173 |
pluralForm := l.pluralForm(tag, operands) |
| 174 |
templateParser := lc.getTemplateParser() |
| 175 |
msg, err2 := template.execute(pluralForm, templateData, templateParser) |
| 176 |
if err2 != nil { |
| 177 |
if err == nil { |
| 178 |
err = err2 |
| 179 |
} |
| 180 |
|
| 181 |
// Attempt to fallback to "Other" pluralization in case translations are incomplete. |
| 182 |
if pluralForm != plural.Other { |
| 183 |
msg2, err3 := template.execute(plural.Other, templateData, templateParser) |
| 184 |
if err3 == nil { |
| 185 |
msg = msg2 |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
return msg, tag, err |
| 190 |
} |
| 191 |
|
| 192 |
func (l *Localizer) getMessageTemplate(id string, defaultMessage *Message) (language.Tag, *MessageTemplate, error) { |
| 193 |
_, i, _ := l.bundle.matcher.Match(l.tags...) |
| 194 |
tag := l.bundle.tags[i] |
| 195 |
mt := l.bundle.getMessageTemplate(tag, id) |
| 196 |
if mt != nil { |
| 197 |
return tag, mt, nil |
| 198 |
} |
| 199 |
|
| 200 |
if tag == l.bundle.defaultLanguage { |
| 201 |
if defaultMessage == nil { |
| 202 |
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id} |
| 203 |
} |
| 204 |
mt := NewMessageTemplate(defaultMessage) |
| 205 |
if mt == nil { |
| 206 |
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id} |
| 207 |
} |
| 208 |
return tag, mt, nil |
| 209 |
} |
| 210 |
|
| 211 |
// Fallback to default language in bundle. |
| 212 |
mt = l.bundle.getMessageTemplate(l.bundle.defaultLanguage, id) |
| 213 |
if mt != nil { |
| 214 |
return l.bundle.defaultLanguage, mt, &MessageNotFoundErr{Tag: tag, MessageID: id} |
| 215 |
} |
| 216 |
|
| 217 |
// Fallback to default message. |
| 218 |
if defaultMessage == nil { |
| 219 |
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id} |
| 220 |
} |
| 221 |
return l.bundle.defaultLanguage, NewMessageTemplate(defaultMessage), &MessageNotFoundErr{Tag: tag, MessageID: id} |
| 222 |
} |
| 223 |
|
| 224 |
func (l *Localizer) pluralForm(tag language.Tag, operands *plural.Operands) plural.Form { |
| 225 |
if operands == nil { |
| 226 |
return plural.Other |
| 227 |
} |
| 228 |
return l.bundle.pluralRules.Rule(tag).PluralFormFunc(operands) |
| 229 |
} |
| 230 |
|
| 231 |
// MustLocalize is similar to Localize, except it panics if an error happens. |
| 232 |
func (l *Localizer) MustLocalize(lc *LocalizeConfig) string { |
| 233 |
localized, err := l.Localize(lc) |
| 234 |
if err != nil { |
| 235 |
panic(err) |
| 236 |
} |
| 237 |
return localized |
| 238 |
} |
| 239 |
|
| 240 |
// MustLocalizeMessage is similar to LocalizeMessage, except it panics if an error happens. |
| 241 |
func (l *Localizer) MustLocalizeMessage(msg *Message) string { |
| 242 |
localized, err := l.LocalizeMessage(msg) |
| 243 |
if err != nil { |
| 244 |
panic(err) |
| 245 |
} |
| 246 |
return localized |
| 247 |
} |