-
Notifications
You must be signed in to change notification settings - Fork 19
/
localization.go
87 lines (74 loc) · 2.31 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
package definition
import (
"encoding/json"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils/uuids"
)
// the translations for a specific item, e.g.
// {
// "text": "Do you like cheese?"
// "quick_replies": ["Yes", "No"]
// }
type itemTranslations map[string][]string
// the 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 languageTranslations map[uuids.UUID]itemTranslations
// GetTextArray returns the requested item translation
func (t languageTranslations) GetTextArray(uuid uuids.UUID, property string) []string {
item, found := t[uuid]
if found {
translation, found := item[property]
if found {
return translation
}
}
return nil
}
// SetTextArray updates the requested item translation
func (t languageTranslations) SetTextArray(uuid uuids.UUID, property string, translated []string) {
_, found := t[uuid]
if !found {
t[uuid] = make(itemTranslations)
}
t[uuid][property] = translated
}
// our top level container for all the translations for all languages
type localization map[envs.Language]languageTranslations
func NewLocalization() flows.Localization {
return make(localization)
}
// Languages gets the list of languages included in this localization
func (l localization) Languages() []envs.Language {
languages := make([]envs.Language, 0, len(l))
for lang := range l {
languages = append(languages, lang)
}
return languages
}
// AddItemTranslation adds a new item translation
func (l localization) AddItemTranslation(lang envs.Language, itemUUID uuids.UUID, property string, translated []string) {
_, found := l[lang]
if !found {
l[lang] = make(languageTranslations)
}
l[lang].SetTextArray(itemUUID, property, translated)
}
// GetTranslations returns the translations for the given language
func (l localization) GetTranslations(lang envs.Language) flows.Translations {
return l[lang]
}
// ReadLocalization reads entire localization flow segment
func ReadLocalization(data json.RawMessage) (flows.Localization, error) {
translations := &localization{}
if err := json.Unmarshal(data, translations); err != nil {
return nil, err
}
return translations, nil
}