-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.go
152 lines (128 loc) · 4.04 KB
/
translator.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
package discordgoi18n
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"os"
"strings"
"text/template"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
)
const (
defaultLocale = discordgo.EnglishUS
leftDelim = "{{"
rightDelim = "}}"
keyDelim = "."
executionPolicy = "missingkey=error"
)
func newTranslator() *translatorImpl {
return &translatorImpl{
defaultLocale: defaultLocale,
translations: make(map[discordgo.Locale]bundle),
loadedBundles: make(map[string]bundle),
}
}
func (translator *translatorImpl) SetDefault(language discordgo.Locale) {
translator.defaultLocale = language
}
func (translator *translatorImpl) LoadBundle(locale discordgo.Locale, path string) error {
loadedBundle, found := translator.loadedBundles[path]
if !found {
buf, err := os.ReadFile(path)
if err != nil {
return err
}
var jsonContent map[string]interface{}
err = json.Unmarshal(buf, &jsonContent)
if err != nil {
return err
}
newBundle := translator.mapBundleStructure(jsonContent)
log.Debug().Msgf("Bundle '%s' loaded with '%s' content", locale, path)
translator.loadedBundles[path] = newBundle
translator.translations[locale] = newBundle
} else {
log.Debug().
Msgf("Bundle '%s' loaded with '%s' content (already loaded for other locales)", locale, path)
translator.translations[locale] = loadedBundle
}
return nil
}
func (translator *translatorImpl) Get(locale discordgo.Locale, key string, variables Vars) string {
bundles, found := translator.translations[locale]
if !found {
if locale != translator.defaultLocale {
log.Warn().Msgf("Bundle '%s' is not loaded, trying to translate key '%s' in '%s'",
locale, key, translator.defaultLocale)
return translator.GetDefault(key, variables)
}
log.Warn().
Msgf("Bundle '%s' is not loaded, cannot translate '%s', key returned", locale, key)
return key
}
raws, found := bundles[key]
if !found || len(raws) == 0 {
if locale != translator.defaultLocale {
log.Warn().
Msgf("No label found for key '%s' in '%s', trying to translate it in %s",
key, locale, translator.defaultLocale)
return translator.GetDefault(key, variables)
}
log.Warn().Msgf("No label found for key '%s' in '%s', key returned", locale, key)
return key
}
//nolint:gosec // No need to have a strong random number generator here.
raw := raws[rand.Intn(len(raws))]
if variables != nil && strings.Contains(raw, leftDelim) {
t, err := template.New("").Delims(leftDelim, rightDelim).Option(executionPolicy).Parse(raw)
if err != nil {
log.Error().Err(err).
Msgf("Cannot parse raw corresponding to key '%s' in '%s', raw returned", locale, key)
return raw
}
var buf bytes.Buffer
err = t.Execute(&buf, variables)
if err != nil {
log.Error().Err(err).
Msgf("Cannot inject variables in raw corresponding to key '%s' in '%s', raw returned", locale, key)
return raw
}
return buf.String()
}
return raw
}
func (translator *translatorImpl) GetDefault(key string, variables Vars) string {
return translator.Get(translator.defaultLocale, key, variables)
}
func (translator *translatorImpl) GetLocalizations(key string, variables Vars) *map[discordgo.Locale]string {
localizations := make(map[discordgo.Locale]string)
for locale := range translator.translations {
localizations[locale] = translator.Get(locale, key, variables)
}
return &localizations
}
func (translator *translatorImpl) mapBundleStructure(jsonContent map[string]interface{}) bundle {
bundle := make(map[string][]string)
for key, content := range jsonContent {
switch v := content.(type) {
case string:
bundle[key] = []string{v}
case []interface{}:
values := make([]string, 0)
for _, value := range v {
values = append(values, fmt.Sprintf("%v", value))
}
bundle[key] = values
case map[string]interface{}:
subValues := translator.mapBundleStructure(v)
for subKey, subValue := range subValues {
bundle[fmt.Sprintf("%s%s%s", key, keyDelim, subKey)] = subValue
}
default:
bundle[key] = []string{fmt.Sprintf("%v", v)}
}
}
return bundle
}