-
Notifications
You must be signed in to change notification settings - Fork 19
/
localization.go
109 lines (94 loc) · 2.94 KB
/
localization.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
package definition
import (
"encoding/json"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/flows"
)
// holds all property translations for a specific item, e.g.
//
// {
// "text": "Do you like cheese?"
// "quick_replies": ["Yes", "No"]
// }
type itemTranslation map[string][]string
// holds all the item translations for a specific language, e.g.
//
// {
// "f3368070-8db8-4549-872a-e69a9d060612": {
// "text": "Do you like cheese?"
// "quick_replies": ["Yes", "No"]
// },
// "7a1aec43-f3e1-42f0-b967-0ee75e725e3a": { ... }
// }
type languageTranslation map[uuids.UUID]itemTranslation
// returns the requested item translation
func (t languageTranslation) getTextArray(uuid uuids.UUID, property string) []string {
item, found := t[uuid]
if found {
translation, found := item[property]
if found {
// TODO editor sometimes saves empty rule translations as [""] which we should fix in a flow migration
// but for now need to ignore
if len(translation) == 0 || (len(translation) == 1 && translation[0] == "") {
return nil
}
return translation
}
}
return nil
}
// creates/updates the requested item translation
func (t languageTranslation) setTextArray(uuid uuids.UUID, property string, translated []string) {
_, found := t[uuid]
if !found {
t[uuid] = make(itemTranslation)
}
t[uuid][property] = translated
}
func (t languageTranslation) Enumerate(callback func(uuids.UUID, string, []string)) {
for uuid, it := range t {
for property, texts := range it {
callback(uuid, property, texts)
}
}
}
// our top level container for all the translations for all languages
type localization map[i18n.Language]languageTranslation
// NewLocalization creates a new empty localization
func NewLocalization() flows.Localization {
return make(localization)
}
// Languages gets the list of languages included in this localization
func (l localization) Languages() []i18n.Language {
languages := make([]i18n.Language, 0, len(l))
for lang := range l {
languages = append(languages, lang)
}
return languages
}
// GetItemTranslation gets an item translation
func (l localization) GetItemTranslation(lang i18n.Language, itemUUID uuids.UUID, property string) []string {
translation, exists := l[lang]
if exists {
return translation.getTextArray(itemUUID, property)
}
return nil
}
// SetItemTranslation sets an item translation
func (l localization) SetItemTranslation(lang i18n.Language, itemUUID uuids.UUID, property string, translated []string) {
_, found := l[lang]
if !found {
l[lang] = make(languageTranslation)
}
l[lang].setTextArray(itemUUID, property, translated)
}
// ReadLocalization reads entire localization flow segment
func ReadLocalization(data json.RawMessage) (flows.Localization, error) {
translations := &localization{}
if err := jsonx.Unmarshal(data, translations); err != nil {
return nil, err
}
return translations, nil
}