-
Notifications
You must be signed in to change notification settings - Fork 8
/
messageformat.go
64 lines (55 loc) · 1.42 KB
/
messageformat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package messageformat implements ICU messages formatting for Go.
// see http://userguide.icu-project.org/formatparse/messages
// inspired by https://github.com/SlexAxton/messageformat.js
package messageformat
import (
"bytes"
"fmt"
"github.com/gotnospirit/makeplural/plural"
)
type MessageFormat struct {
root node
formatters map[string]formatFunc
plural pluralFunc
}
func (x *MessageFormat) SetCulture(name string) error {
fn, err := plural.GetFunc(name)
if err != nil {
return err
}
x.plural = fn
return nil
}
func (x *MessageFormat) SetPluralFunction(fn pluralFunc) error {
if fn == nil {
return fmt.Errorf("PluralFunctionRequired")
}
x.plural = fn
return nil
}
func (x *MessageFormat) Format() (string, error) {
return x.FormatMap(nil)
}
func (x *MessageFormat) FormatMap(data map[string]interface{}) (string, error) {
var buf bytes.Buffer
err := x.root.format(&buf, &data, x, "")
if err != nil {
return "", err
}
return buf.String(), nil
}
func (x *MessageFormat) getNamedKey(value interface{}, ordinal bool) (string, error) {
if x.plural == nil {
return "", fmt.Errorf("UndefinedPluralFunc")
}
return x.plural(value, ordinal), nil
}
func (x *MessageFormat) getFormatter(key string) (formatFunc, error) {
fn, ok := x.formatters[key]
if !ok {
return nil, fmt.Errorf("UnknownType: `%s`", key)
} else if fn == nil {
return nil, fmt.Errorf("UndefinedFormatFunc: `%s`", key)
}
return fn, nil
}