This repository has been archived by the owner on Feb 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.go
187 lines (165 loc) · 5.69 KB
/
translate.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
177
178
179
180
181
182
183
184
185
186
187
package dhelpers
import (
"strings"
"text/template"
"time"
"github.com/dustin/go-humanize"
"github.com/mongodb/mongo-go-driver/bson/objectid"
"github.com/nicksnyder/go-i18n/v2/i18n"
"gitlab.com/Cacophony/dhelpers/cache"
"gitlab.com/Cacophony/dhelpers/emoji"
)
var (
// the additional functions to use in the template engine
translationFuncs = template.FuncMap{
// ObjectID returns a humanised version of an object ID
// example: {{ObjectID id}} => 5ae3a59624f8753dba273792
"ObjectID": func(id objectid.ObjectID) string {
return id.String()
},
// MarkdownLinkEscape returns a given link escaped to be used in Markdown
// example: {{MarkdownLinkEscape "https://example.org/A+(B)"}} => https://example.org/A+%28B%29
"MarkdownLinkEscape": func(text string) string {
return EscapeLinkForMarkdown(text)
},
// NumberCommas adds commas to large numbers
// example: {{HumanizeComma 1000}} => 1,000
"NumberCommas": func(number int) string {
return humanize.Comma(int64(number))
},
// HumanizeTime formats time human readable
// example: {{HumanizeTime time.Sub(10*time.Minute)}} => 10 minutes ago
"HumanizeTime": func(theTime time.Time) string {
return humanize.Time(theTime)
},
// Prefix returns the prefix for a GuildID
// example: {{Prefix 339227598544568340}} => /
"Prefix": func(guildID string) string {
return GetPrefix(guildID)
},
// PrefixE returns the prefix for an EventContainer
// currently only works with MessageCreate, MessageUpdate, and MessageDelete events
// example: {{PrefixE event}} => /
"PrefixE": func(event EventContainer) string {
switch event.Type {
case MessageCreateEventType:
return GetPrefix(event.MessageCreate.GuildID)
case MessageUpdateEventType:
return GetPrefix(event.MessageUpdate.GuildID)
case MessageDeleteEventType:
return GetPrefix(event.MessageDelete.GuildID)
}
return defaultPrefix
},
}
)
// T returns the translation for the given message ID
// Example: T("HelloWorld")
func T(messageID string) (result string) {
if cache.GetLocalizationBundle() == nil {
return messageID
}
// on panic return message ID
defer func() {
err := recover()
if err != nil {
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
result = messageID
}
}()
translation, err := i18n.NewLocalizer(cache.GetLocalizationBundle(), "en").Localize(&i18n.LocalizeConfig{
MessageID: messageID,
Funcs: translationFuncs,
})
if err != nil {
if !strings.Contains(err.Error(), "not found") { // ignore message not found errors
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
}
return messageID
}
return emoji.Replace(translation)
}
// Tf returns the translation for the given message ID applying the fields
// Example: Tf("HelloWorld", "key", "value")
func Tf(messageID string, fields ...interface{}) (result string) {
if cache.GetLocalizationBundle() == nil {
return messageID
}
// on panic return message ID
defer func() {
err := recover()
if err != nil {
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
result = messageID
}
}()
// create map out of fields
data := make(map[interface{}]interface{})
for i := range fields {
if i%2 == 0 && len(fields) > i+1 {
data[fields[i]] = fields[i+1]
}
}
translation, err := i18n.NewLocalizer(cache.GetLocalizationBundle(), "en").Localize(&i18n.LocalizeConfig{
MessageID: messageID,
TemplateData: data,
Funcs: translationFuncs,
})
if err != nil {
if !strings.Contains(err.Error(), "not found") { // ignore message not found errors
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
}
return messageID
}
return emoji.Replace(translation)
}
// Tfc returns the translation for the given message ID applying the fields and pluralization count
// Example: Tfc("HelloWorld", 3, "key", "value")
func Tfc(messageID string, count int, fields ...interface{}) (result string) {
if cache.GetLocalizationBundle() == nil {
return messageID
}
// on panic return message ID
defer func() {
err := recover()
if err != nil {
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
result = messageID
}
}()
// create map out of fields
data := make(map[interface{}]interface{})
for i := range fields {
if i%2 == 0 && len(fields) > i+1 {
data[fields[i]] = fields[i+1]
}
}
translation, err := i18n.NewLocalizer(cache.GetLocalizationBundle(), "en").Localize(&i18n.LocalizeConfig{
MessageID: messageID,
TemplateData: data,
PluralCount: count,
Funcs: translationFuncs,
})
if err != nil {
if !strings.Contains(err.Error(), "not found") { // ignore message not found errors
cache.GetLogger().WithField("module", "translate").Errorln(err.(error).Error())
}
return messageID
}
return emoji.Replace(translation)
}
// T returns the translation for the given message ID, the event variable is being set
// Example: T("HelloWorld")
func (event EventContainer) T(messageID string) (result string) {
return Tf(messageID, "event", event)
}
// Tf returns the translation for the given message ID applying the fields, the event variable is being set
// Example: Tf("HelloWorld", "key", "value")
func (event EventContainer) Tf(messageID string, fields ...interface{}) (result string) {
return Tf(messageID, append(fields, "event", event)...)
}
// Tfc returns the translation for the given message ID applying the fields and pluralization count, the event variable is being set
// Example: Tfc("HelloWorld", 3, "key", "value")
func (event EventContainer) Tfc(messageID string, count int, fields ...interface{}) (result string) {
return Tfc(messageID, count, append(fields, "event", event)...)
}