| 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" |
| 9 |
"github.com/gohugoio/go-i18n/v2/internal/plural" |
| 10 |
) |
| 11 |
|
| 12 |
// MessageTemplate is an executable template for a message. |
| 13 |
type MessageTemplate struct { |
| 14 |
*Message |
| 15 |
PluralTemplates map[plural.Form]*internal.Template |
| 16 |
} |
| 17 |
|
| 18 |
// NewMessageTemplate returns a new message template. |
| 19 |
func NewMessageTemplate(m *Message) *MessageTemplate { |
| 20 |
pluralTemplates := map[plural.Form]*internal.Template{} |
| 21 |
setPluralTemplate(pluralTemplates, plural.Zero, m.Zero, m.LeftDelim, m.RightDelim) |
| 22 |
setPluralTemplate(pluralTemplates, plural.One, m.One, m.LeftDelim, m.RightDelim) |
| 23 |
setPluralTemplate(pluralTemplates, plural.Two, m.Two, m.LeftDelim, m.RightDelim) |
| 24 |
setPluralTemplate(pluralTemplates, plural.Few, m.Few, m.LeftDelim, m.RightDelim) |
| 25 |
setPluralTemplate(pluralTemplates, plural.Many, m.Many, m.LeftDelim, m.RightDelim) |
| 26 |
setPluralTemplate(pluralTemplates, plural.Other, m.Other, m.LeftDelim, m.RightDelim) |
| 27 |
if len(pluralTemplates) == 0 { |
| 28 |
return nil |
| 29 |
} |
| 30 |
return &MessageTemplate{ |
| 31 |
Message: m, |
| 32 |
PluralTemplates: pluralTemplates, |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func setPluralTemplate(pluralTemplates map[plural.Form]*internal.Template, pluralForm plural.Form, src, leftDelim, rightDelim string) { |
| 37 |
if src != "" { |
| 38 |
pluralTemplates[pluralForm] = &internal.Template{ |
| 39 |
Src: src, |
| 40 |
LeftDelim: leftDelim, |
| 41 |
RightDelim: rightDelim, |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
type pluralFormNotFoundError struct { |
| 47 |
pluralForm plural.Form |
| 48 |
messageID string |
| 49 |
} |
| 50 |
|
| 51 |
func (e pluralFormNotFoundError) Error() string { |
| 52 |
return fmt.Sprintf("message %q has no plural form %q", e.messageID, e.pluralForm) |
| 53 |
} |
| 54 |
|
| 55 |
// Execute executes the template for the plural form and template data. |
| 56 |
// Deprecated: This method is no longer used internally by go-i18n and it probably should not have been exported to |
| 57 |
// begin with. Its replacement is not exported. If you depend on this method for some reason and/or have |
| 58 |
// a use case for exporting execute, please file an issue. |
| 59 |
func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}, funcs texttemplate.FuncMap) (string, error) { |
| 60 |
t := mt.PluralTemplates[pluralForm] |
| 61 |
if t == nil { |
| 62 |
return "", pluralFormNotFoundError{ |
| 63 |
pluralForm: pluralForm, |
| 64 |
messageID: mt.ID, |
| 65 |
} |
| 66 |
} |
| 67 |
parser := &template.TextParser{ |
| 68 |
Funcs: funcs, |
| 69 |
} |
| 70 |
return t.Execute(parser, data) |
| 71 |
} |
| 72 |
|
| 73 |
func (mt *MessageTemplate) execute(pluralForm plural.Form, data interface{}, parser template.Parser) (string, error) { |
| 74 |
t := mt.PluralTemplates[pluralForm] |
| 75 |
if t == nil { |
| 76 |
return "", pluralFormNotFoundError{ |
| 77 |
pluralForm: pluralForm, |
| 78 |
messageID: mt.ID, |
| 79 |
} |
| 80 |
} |
| 81 |
return t.Execute(parser, data) |
| 82 |
} |