-
Notifications
You must be signed in to change notification settings - Fork 2
/
bundle.go
143 lines (116 loc) · 2.61 KB
/
bundle.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package localize
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"strings"
)
type parsedMessage struct {
S string
// when plural
One string
Other string
// for Welsh only
Two string
Few string
Many string
}
func (m *parsedMessage) UnmarshalJSON(text []byte) error {
var s string
if err := json.Unmarshal(text, &s); err == nil {
m.S = s
return nil
}
var v map[string]string
if err := json.Unmarshal(text, &v); err == nil {
m.One = v["one"]
m.Other = v["other"]
m.Two = v["two"]
m.Few = v["few"]
m.Many = v["many"]
return nil
}
return errors.New("message malformed")
}
type Bundle struct {
messages map[string]Messages
}
func NewBundle(paths ...string) (*Bundle, error) {
bundle := &Bundle{messages: map[string]Messages{}}
for _, path := range paths {
if err := bundle.LoadMessageFile(path); err != nil {
return nil, err
}
}
return bundle, nil
}
func (b *Bundle) LoadMessageFile(p string) error {
data, err := os.ReadFile(p)
if err != nil {
return err
}
var v map[string]parsedMessage
if err := json.Unmarshal(data, &v); err != nil {
return err
}
lang, _ := strings.CutSuffix(path.Base(p), ".json")
if lang == "en" {
if err := verifyEn(v); err != nil {
return err
}
} else if lang == "cy" {
if err := verifyCy(v); err != nil {
return err
}
} else {
return errors.New("only supports en or cy")
}
messages := Messages{
Singles: map[string]singleMessage{},
Plurals: map[string]pluralMessage{},
}
for key, message := range v {
if message.S != "" {
messages.Singles[key] = newSingleMessage(message.S)
} else {
messages.Plurals[key] = pluralMessage{
One: newSingleMessage(message.One),
Two: newSingleMessage(message.Two),
Few: newSingleMessage(message.Few),
Many: newSingleMessage(message.Many),
Other: newSingleMessage(message.Other),
}
}
}
b.messages[lang] = messages
return nil
}
func verifyEn(v map[string]parsedMessage) error {
for key, message := range v {
if message.S != "" {
continue
}
if message.One != "" && message.Other != "" && message.Two == "" && message.Few == "" && message.Many == "" {
continue
}
return fmt.Errorf("problem with key: %s", key)
}
return nil
}
func verifyCy(v map[string]parsedMessage) error {
for key, message := range v {
if message.S != "" {
continue
}
if message.One != "" && message.Other != "" && message.Two != "" && message.Few != "" && message.Many != "" {
continue
}
return fmt.Errorf("problem with key: %s", key)
}
return nil
}
func (b *Bundle) For(lang Lang) *Localizer {
return &Localizer{b.messages[lang.String()], false, lang}
}