| 1 |
package i18n |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
// Message is a string that can be localized. |
| 10 |
type Message struct { |
| 11 |
// ID uniquely identifies the message. |
| 12 |
ID string |
| 13 |
|
| 14 |
// Hash uniquely identifies the content of the message |
| 15 |
// that this message was translated from. |
| 16 |
Hash string |
| 17 |
|
| 18 |
// Description describes the message to give additional |
| 19 |
// context to translators that may be relevant for translation. |
| 20 |
Description string |
| 21 |
|
| 22 |
// LeftDelim is the left Go template delimiter. |
| 23 |
LeftDelim string |
| 24 |
|
| 25 |
// RightDelim is the right Go template delimiter. |
| 26 |
RightDelim string |
| 27 |
|
| 28 |
// Zero is the content of the message for the CLDR plural form "zero". |
| 29 |
Zero string |
| 30 |
|
| 31 |
// One is the content of the message for the CLDR plural form "one". |
| 32 |
One string |
| 33 |
|
| 34 |
// Two is the content of the message for the CLDR plural form "two". |
| 35 |
Two string |
| 36 |
|
| 37 |
// Few is the content of the message for the CLDR plural form "few". |
| 38 |
Few string |
| 39 |
|
| 40 |
// Many is the content of the message for the CLDR plural form "many". |
| 41 |
Many string |
| 42 |
|
| 43 |
// Other is the content of the message for the CLDR plural form "other". |
| 44 |
Other string |
| 45 |
} |
| 46 |
|
| 47 |
// NewMessage parses data and returns a new message. |
| 48 |
func NewMessage(data interface{}) (*Message, error) { |
| 49 |
m := &Message{} |
| 50 |
if err := m.unmarshalInterface(data); err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return m, nil |
| 54 |
} |
| 55 |
|
| 56 |
// MustNewMessage is similar to NewMessage except it panics if an error happens. |
| 57 |
func MustNewMessage(data interface{}) *Message { |
| 58 |
m, err := NewMessage(data) |
| 59 |
if err != nil { |
| 60 |
panic(err) |
| 61 |
} |
| 62 |
return m |
| 63 |
} |
| 64 |
|
| 65 |
// unmarshalInterface unmarshals a message from data. |
| 66 |
func (m *Message) unmarshalInterface(v interface{}) error { |
| 67 |
strdata, err := stringMap(v) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
for k, v := range strdata { |
| 72 |
switch strings.ToLower(k) { |
| 73 |
case "id": |
| 74 |
m.ID = v |
| 75 |
case "description": |
| 76 |
m.Description = v |
| 77 |
case "hash": |
| 78 |
m.Hash = v |
| 79 |
case "leftdelim": |
| 80 |
m.LeftDelim = v |
| 81 |
case "rightdelim": |
| 82 |
m.RightDelim = v |
| 83 |
case "zero": |
| 84 |
m.Zero = v |
| 85 |
case "one": |
| 86 |
m.One = v |
| 87 |
case "two": |
| 88 |
m.Two = v |
| 89 |
case "few": |
| 90 |
m.Few = v |
| 91 |
case "many": |
| 92 |
m.Many = v |
| 93 |
case "other": |
| 94 |
m.Other = v |
| 95 |
} |
| 96 |
} |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
type keyTypeErr struct { |
| 101 |
key interface{} |
| 102 |
} |
| 103 |
|
| 104 |
func (err *keyTypeErr) Error() string { |
| 105 |
return fmt.Sprintf("expected key to be a string but got %#v", err.key) |
| 106 |
} |
| 107 |
|
| 108 |
type valueTypeErr struct { |
| 109 |
value interface{} |
| 110 |
} |
| 111 |
|
| 112 |
func (err *valueTypeErr) Error() string { |
| 113 |
return fmt.Sprintf("unsupported type %#v", err.value) |
| 114 |
} |
| 115 |
|
| 116 |
func stringMap(v interface{}) (map[string]string, error) { |
| 117 |
switch value := v.(type) { |
| 118 |
case string: |
| 119 |
return map[string]string{ |
| 120 |
"other": value, |
| 121 |
}, nil |
| 122 |
case map[string]string: |
| 123 |
return value, nil |
| 124 |
case map[string]interface{}: |
| 125 |
strdata := make(map[string]string, len(value)) |
| 126 |
for k, v := range value { |
| 127 |
err := stringSubmap(k, v, strdata) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
} |
| 132 |
return strdata, nil |
| 133 |
case map[interface{}]interface{}: |
| 134 |
strdata := make(map[string]string, len(value)) |
| 135 |
for k, v := range value { |
| 136 |
kstr, ok := k.(string) |
| 137 |
if !ok { |
| 138 |
return nil, &keyTypeErr{key: k} |
| 139 |
} |
| 140 |
err := stringSubmap(kstr, v, strdata) |
| 141 |
if err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
} |
| 145 |
return strdata, nil |
| 146 |
default: |
| 147 |
return nil, &valueTypeErr{value: value} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func stringSubmap(k string, v interface{}, strdata map[string]string) error { |
| 152 |
if k == "translation" { |
| 153 |
switch vt := v.(type) { |
| 154 |
case string: |
| 155 |
strdata["other"] = vt |
| 156 |
default: |
| 157 |
v1Message, err := stringMap(v) |
| 158 |
if err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
for kk, vv := range v1Message { |
| 162 |
strdata[kk] = vv |
| 163 |
} |
| 164 |
} |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
switch vt := v.(type) { |
| 169 |
case string: |
| 170 |
strdata[k] = vt |
| 171 |
return nil |
| 172 |
case nil: |
| 173 |
return nil |
| 174 |
default: |
| 175 |
return fmt.Errorf("expected value for key %q be a string but got %#v", k, v) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
var reservedKeys = map[string]struct{}{ |
| 180 |
"id": {}, |
| 181 |
"description": {}, |
| 182 |
"hash": {}, |
| 183 |
"leftdelim": {}, |
| 184 |
"rightdelim": {}, |
| 185 |
"zero": {}, |
| 186 |
"one": {}, |
| 187 |
"two": {}, |
| 188 |
"few": {}, |
| 189 |
"many": {}, |
| 190 |
"other": {}, |
| 191 |
"translation": {}, |
| 192 |
} |
| 193 |
|
| 194 |
func isReserved(key string, val any) bool { |
| 195 |
lk := strings.ToLower(key) |
| 196 |
if _, ok := reservedKeys[lk]; ok { |
| 197 |
if key == "translation" { |
| 198 |
return true |
| 199 |
} |
| 200 |
if _, ok := val.(string); ok { |
| 201 |
return true |
| 202 |
} |
| 203 |
} |
| 204 |
return false |
| 205 |
} |
| 206 |
|
| 207 |
// isMessage returns true if v contains only message keys and false if it contains no message keys. |
| 208 |
// It returns an error if v contains both message and non-message keys. |
| 209 |
// - {"message": {"description": "world"}} is a message |
| 210 |
// - {"error": {"description": "world", "foo": "bar"}} is an error |
| 211 |
// - {"notmessage": {"description": {"hello": "world"}}} is not a message |
| 212 |
// - {"notmessage": {"foo": "bar"}} is not a message |
| 213 |
func isMessage(v interface{}) (bool, error) { |
| 214 |
switch data := v.(type) { |
| 215 |
case nil, string: |
| 216 |
return true, nil |
| 217 |
case map[string]interface{}: |
| 218 |
reservedKeys := make([]string, 0, len(reservedKeys)) |
| 219 |
unreservedKeys := make([]string, 0, len(data)) |
| 220 |
for k, v := range data { |
| 221 |
if isReserved(k, v) { |
| 222 |
reservedKeys = append(reservedKeys, k) |
| 223 |
} else { |
| 224 |
unreservedKeys = append(unreservedKeys, k) |
| 225 |
} |
| 226 |
} |
| 227 |
hasReservedKeys := len(reservedKeys) > 0 |
| 228 |
if hasReservedKeys && len(unreservedKeys) > 0 { |
| 229 |
return false, &mixedKeysError{ |
| 230 |
reservedKeys: reservedKeys, |
| 231 |
unreservedKeys: unreservedKeys, |
| 232 |
} |
| 233 |
} |
| 234 |
return hasReservedKeys, nil |
| 235 |
case map[interface{}]interface{}: |
| 236 |
reservedKeys := make([]string, 0, len(reservedKeys)) |
| 237 |
unreservedKeys := make([]string, 0, len(data)) |
| 238 |
for key, v := range data { |
| 239 |
k, ok := key.(string) |
| 240 |
if !ok { |
| 241 |
unreservedKeys = append(unreservedKeys, fmt.Sprintf("%+v", key)) |
| 242 |
} else if isReserved(k, v) { |
| 243 |
reservedKeys = append(reservedKeys, k) |
| 244 |
} else { |
| 245 |
unreservedKeys = append(unreservedKeys, k) |
| 246 |
} |
| 247 |
} |
| 248 |
hasReservedKeys := len(reservedKeys) > 0 |
| 249 |
if hasReservedKeys && len(unreservedKeys) > 0 { |
| 250 |
return false, &mixedKeysError{ |
| 251 |
reservedKeys: reservedKeys, |
| 252 |
unreservedKeys: unreservedKeys, |
| 253 |
} |
| 254 |
} |
| 255 |
return hasReservedKeys, nil |
| 256 |
} |
| 257 |
return false, nil |
| 258 |
} |
| 259 |
|
| 260 |
type mixedKeysError struct { |
| 261 |
reservedKeys []string |
| 262 |
unreservedKeys []string |
| 263 |
} |
| 264 |
|
| 265 |
func (e *mixedKeysError) Error() string { |
| 266 |
sort.Strings(e.reservedKeys) |
| 267 |
sort.Strings(e.unreservedKeys) |
| 268 |
return fmt.Sprintf("reserved keys %v mixed with unreserved keys %v", e.reservedKeys, e.unreservedKeys) |
| 269 |
} |