-
Notifications
You must be signed in to change notification settings - Fork 19
/
gettext.go
48 lines (41 loc) · 1.21 KB
/
gettext.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
package inspect
import (
"reflect"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/flows"
)
// LocalizableText extracts localizable text by reading engine tags on a struct
func LocalizableText(s any, include func(uuids.UUID, string, []string, func([]string))) {
localizableText(reflect.ValueOf(s), include)
}
func localizableText(v reflect.Value, include func(uuids.UUID, string, []string, func([]string))) {
walk(v, nil, func(sv reflect.Value, fv reflect.Value, ef *EngineField) {
if ef.Localized {
localizable := sv.Interface().(flows.Localizable)
r, w := extractLocalizableText(fv)
include(localizable.LocalizationUUID(), ef.JSONName, r(), w)
}
})
}
// returns read and write functions for the given flow text value
func extractLocalizableText(v reflect.Value) (func() []string, func([]string)) {
switch typed := v.Interface().(type) {
case []string:
r := func() []string {
return typed
}
w := func(n []string) {
*v.Addr().Interface().(*[]string) = n
}
return r, w
case string:
r := func() []string {
return []string{typed}
}
w := func(n []string) {
v.SetString(n[0])
}
return r, w
}
panic("localized tags can only be applied to strings and slices of strings")
}