/
translations_i18next.go
141 lines (114 loc) · 2.91 KB
/
translations_i18next.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
package i18n
import (
"encoding/json"
"fmt"
"strings"
"golang.org/x/text/feature/plural"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
type ContentKind int
const (
ContentKindMessage ContentKind = iota
ContentKindNested
)
type ContentNode struct {
Kind ContentKind
Message string
Nested map[string]*ContentNode
}
func (n *ContentNode) UnmarshalJSON(b []byte) error {
var value string
if err := json.Unmarshal(b, &value); err == nil {
n.Kind = ContentKindMessage
n.Message = value
return nil
}
node := make(map[string]*ContentNode)
if err := json.Unmarshal(b, &node); err == nil {
n.Kind = ContentKindNested
n.Nested = node
return nil
}
return fmt.Errorf("unable to parse content node")
}
type localizationsJSON struct {
Builder *catalog.Builder
Lang language.Tag
}
func (l *localizationsJSON) UnmarshalJSON(b []byte) error {
tree := make(map[string]*ContentNode)
if err := json.Unmarshal(b, &tree); err != nil {
return fmt.Errorf("unable to parse tree: %w", err)
}
if err := l.generateCatalog("", tree); err != nil {
return fmt.Errorf("unable to populate catalog: %w", err)
}
return nil
}
type pcases struct {
// Specific include Form such as `Zero` `One` `Two` `Few` `Many`, ``x=`, `x<`
specific []interface{}
// other is the last fallback
other []interface{}
}
// other should always be set as last item
func (p *pcases) getSelectCases() []interface{} { return append(p.specific, p.other...) }
func (l *localizationsJSON) generateCatalog(root string, tree map[string]*ContentNode) error {
selector := make(map[string]*pcases)
for k, value := range tree {
keys := strings.Split(k, "_")
if len(keys) == 0 {
return fmt.Errorf("cannot use empty key")
}
// format key
var key string
if root != "" {
key = fmt.Sprintf("%s.%s", root, keys[0])
} else {
key = keys[0]
}
// is it nested ?
if value.Kind == ContentKindNested {
if err := l.generateCatalog(key, value.Nested); err != nil {
return err
}
continue
}
if value.Message == "" {
continue // skip empty value
}
// is it regular message ?
if len(keys) == 1 {
if err := l.Builder.SetString(l.Lang, key, value.Message); err != nil {
return fmt.Errorf("unable to set string key `%s`: %w", key, err)
}
continue
}
form := keys[1]
if !supported(form, l.Lang) {
continue
}
// add it has plurals
cases, ok := selector[key]
if !ok {
cases = &pcases{
specific: []interface{}{},
other: []interface{}{},
}
}
if strings.ToLower(form) == "other" {
cases.other = []interface{}{form, value.Message}
} else {
cases.specific = []interface{}{form, value.Message}
}
selector[key] = cases
}
for key, cases := range selector {
list := cases.getSelectCases()
if err := l.Builder.Set(l.Lang, key, plural.Selectf(1, "", list...)); err != nil {
return fmt.Errorf("unable to set plural key `%s`: %w", key, err)
}
}
return nil
}