-
Notifications
You must be signed in to change notification settings - Fork 335
/
resource_translation.go
176 lines (146 loc) · 3.81 KB
/
resource_translation.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package types
import (
"database/sql/driver"
"strconv"
"strings"
"time"
"github.com/cortezaproject/corteza/server/pkg/cast2"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/cortezaproject/corteza/server/pkg/locale"
"golang.org/x/text/language"
)
type (
ResourceTranslation struct {
ID uint64 `json:"translationID,string"`
Lang Lang `json:"lang"`
Resource string `json:"resource"`
K string `json:"key"`
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
DeletedAt *time.Time `json:"deletedAt,omitempty"`
OwnedBy uint64 `json:"ownedBy,string"`
CreatedBy uint64 `json:"createdBy,string" `
UpdatedBy uint64 `json:"updatedBy,string,omitempty" `
DeletedBy uint64 `json:"deletedBy,string,omitempty" `
}
ResourceTranslationFilter struct {
TranslationID []string `json:"translationID"`
Lang string `json:"lang"`
Resource string `json:"resource"`
ResourceType string `json:"resourceType"`
OwnerID uint64 `json:"ownerID,string"`
Deleted filter.State `json:"deleted"`
// Standard helpers for paging and sorting
filter.Sorting
filter.Paging
}
Lang struct {
language.Tag
}
)
// Value scan on ReportDataSourceSet gracefully handles conversion from NULL
func (l Lang) Value() (driver.Value, error) {
return l.String(), nil
}
func (l *Lang) Scan(value any) error {
v := ""
//lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8
switch aux := value.(type) {
case string:
v = aux
case []uint8:
v = string(value.([]byte))
}
l.Tag = language.Make(v)
return nil
}
func (a *ResourceTranslation) Compare(b *locale.ResourceTranslation) bool {
return strings.EqualFold(a.K, b.Key) && a.Lang.Tag.String() == b.Lang
}
func (rt *ResourceTranslation) setValue(name string, pos uint, v any) (err error) {
pp := strings.Split(name, ".")
switch pp[0] {
case "resource", "Resource", "Path", "path":
ix, err := strconv.ParseUint(pp[1], 10, 64)
if err != nil {
return err
}
res := strings.Split(rt.Resource, "/")
aux := ""
err = cast2.String(v, &aux)
// +1 bacause the first bit is the resource
res[ix+1] = aux
rt.Resource = strings.Join(res, "/")
return err
}
return
}
func (set ResourceTranslationSet) New(bb locale.ResourceTranslationSet) (out ResourceTranslationSet) {
outer:
for _, b := range bb {
for _, a := range set {
// It's not new
if a.Compare(b) {
continue outer
}
}
out = append(out, &ResourceTranslation{
Lang: Lang{Tag: language.Make(b.Lang)},
Resource: b.Resource,
K: b.Key,
Message: b.Msg,
})
}
return
}
func (set ResourceTranslationSet) Old(bb locale.ResourceTranslationSet) (out [][2]*ResourceTranslation) {
for _, b := range bb {
for _, a := range set {
// It's not new
if a.Compare(b) {
aux := *a
aux.Message = b.Msg
// old, new
out = append(out, [2]*ResourceTranslation{a, &aux})
break
}
}
}
return
}
func (set ResourceTranslationSet) FilterLanguage(tag language.Tag) (out ResourceTranslationSet) {
for _, a := range set {
if a.Lang.Tag == tag {
out = append(out, a)
}
}
return
}
func (set ResourceTranslationSet) FilterKey(key string) (out ResourceTranslationSet) {
for _, a := range set {
if a.K == key {
out = append(out, a)
}
}
return
}
func (set ResourceTranslationSet) FilterResource(res string) (out ResourceTranslationSet) {
for _, a := range set {
if a.Resource == res {
out = append(out, a)
}
}
return
}
func FromLocale(ll locale.ResourceTranslationSet) (out ResourceTranslationSet) {
for _, l := range ll {
out = append(out, &ResourceTranslation{
Lang: Lang{Tag: language.Make(l.Lang)},
Resource: l.Resource,
K: l.Key,
Message: l.Msg,
})
}
return out
}