| 1 |
package i18n |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
|
| 9 |
"golang.org/x/text/language" |
| 10 |
) |
| 11 |
|
| 12 |
// MessageFile represents a parsed message file. |
| 13 |
type MessageFile struct { |
| 14 |
Path string |
| 15 |
Tag language.Tag |
| 16 |
Format string |
| 17 |
Messages []*Message |
| 18 |
} |
| 19 |
|
| 20 |
// ParseMessageFileBytes returns the messages parsed from file. |
| 21 |
func ParseMessageFileBytes(buf []byte, path string, unmarshalFuncs map[string]UnmarshalFunc) (*MessageFile, error) { |
| 22 |
lang, format := parsePath(path) |
| 23 |
tag := language.Make(lang) |
| 24 |
messageFile := &MessageFile{ |
| 25 |
Path: path, |
| 26 |
Tag: tag, |
| 27 |
Format: format, |
| 28 |
} |
| 29 |
if len(buf) == 0 { |
| 30 |
return messageFile, nil |
| 31 |
} |
| 32 |
unmarshalFunc := unmarshalFuncs[messageFile.Format] |
| 33 |
if unmarshalFunc == nil { |
| 34 |
if messageFile.Format == "json" { |
| 35 |
unmarshalFunc = json.Unmarshal |
| 36 |
} else { |
| 37 |
return nil, fmt.Errorf("no unmarshaler registered for %s", messageFile.Format) |
| 38 |
} |
| 39 |
} |
| 40 |
var err error |
| 41 |
var raw interface{} |
| 42 |
if err = unmarshalFunc(buf, &raw); err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
|
| 46 |
m, err := isMessage(raw) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
|
| 51 |
if messageFile.Messages, err = recGetMessages(raw, m, true); err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
return messageFile, nil |
| 56 |
} |
| 57 |
|
| 58 |
const nestedSeparator = "." |
| 59 |
|
| 60 |
var errInvalidTranslationFile = errors.New("invalid translation file, expected key-values, got a single value") |
| 61 |
|
| 62 |
// recGetMessages looks for translation messages inside "raw" parameter, |
| 63 |
// scanning nested maps using recursion. |
| 64 |
func recGetMessages(raw interface{}, isMapMessage, isInitialCall bool) ([]*Message, error) { |
| 65 |
var messages []*Message |
| 66 |
var err error |
| 67 |
|
| 68 |
switch data := raw.(type) { |
| 69 |
case string: |
| 70 |
if isInitialCall { |
| 71 |
return nil, errInvalidTranslationFile |
| 72 |
} |
| 73 |
m, err := NewMessage(data) |
| 74 |
return []*Message{m}, err |
| 75 |
|
| 76 |
case map[string]interface{}: |
| 77 |
if isMapMessage { |
| 78 |
m, err := NewMessage(data) |
| 79 |
return []*Message{m}, err |
| 80 |
} |
| 81 |
messages = make([]*Message, 0, len(data)) |
| 82 |
for id, data := range data { |
| 83 |
// recursively scan map items |
| 84 |
messages, err = addChildMessages(id, data, messages) |
| 85 |
if err != nil { |
| 86 |
return nil, err |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
case map[interface{}]interface{}: |
| 91 |
if isMapMessage { |
| 92 |
m, err := NewMessage(data) |
| 93 |
return []*Message{m}, err |
| 94 |
} |
| 95 |
messages = make([]*Message, 0, len(data)) |
| 96 |
for id, data := range data { |
| 97 |
strid, ok := id.(string) |
| 98 |
if !ok { |
| 99 |
return nil, fmt.Errorf("expected key to be string but got %#v", id) |
| 100 |
} |
| 101 |
// recursively scan map items |
| 102 |
messages, err = addChildMessages(strid, data, messages) |
| 103 |
if err != nil { |
| 104 |
return nil, err |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
case []interface{}: |
| 109 |
// Backward compatibility for v1 file format. |
| 110 |
messages = make([]*Message, 0, len(data)) |
| 111 |
for _, data := range data { |
| 112 |
// recursively scan slice items |
| 113 |
m, err := isMessage(data) |
| 114 |
if err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
childMessages, err := recGetMessages(data, m, false) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
messages = append(messages, childMessages...) |
| 122 |
} |
| 123 |
|
| 124 |
case nil: |
| 125 |
if isInitialCall { |
| 126 |
return nil, errInvalidTranslationFile |
| 127 |
} |
| 128 |
m, err := NewMessage("") |
| 129 |
return []*Message{m}, err |
| 130 |
|
| 131 |
default: |
| 132 |
return nil, fmt.Errorf("unsupported file format %T", raw) |
| 133 |
} |
| 134 |
|
| 135 |
return messages, nil |
| 136 |
} |
| 137 |
|
| 138 |
func addChildMessages(id string, data interface{}, messages []*Message) ([]*Message, error) { |
| 139 |
isChildMessage, err := isMessage(data) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
childMessages, err := recGetMessages(data, isChildMessage, false) |
| 144 |
if err != nil { |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
for _, m := range childMessages { |
| 148 |
if isChildMessage { |
| 149 |
if m.ID == "" { |
| 150 |
m.ID = id // start with innermost key |
| 151 |
} |
| 152 |
} else { |
| 153 |
m.ID = id + nestedSeparator + m.ID // update ID with each nested key on the way |
| 154 |
} |
| 155 |
messages = append(messages, m) |
| 156 |
} |
| 157 |
return messages, nil |
| 158 |
} |
| 159 |
|
| 160 |
func parsePath(path string) (langTag, format string) { |
| 161 |
formatStartIdx := -1 |
| 162 |
for i := len(path) - 1; i >= 0; i-- { |
| 163 |
c := path[i] |
| 164 |
if os.IsPathSeparator(c) { |
| 165 |
if formatStartIdx != -1 { |
| 166 |
langTag = path[i+1 : formatStartIdx] |
| 167 |
} |
| 168 |
return |
| 169 |
} |
| 170 |
if path[i] == '.' { |
| 171 |
if formatStartIdx != -1 { |
| 172 |
langTag = path[i+1 : formatStartIdx] |
| 173 |
return |
| 174 |
} |
| 175 |
if formatStartIdx == -1 { |
| 176 |
format = path[i+1:] |
| 177 |
formatStartIdx = i |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
if formatStartIdx != -1 { |
| 182 |
langTag = path[:formatStartIdx] |
| 183 |
} |
| 184 |
return |
| 185 |
} |