-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscordgoi18n.go
75 lines (61 loc) · 2.37 KB
/
discordgoi18n.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
package discordgoi18n
import (
"github.com/bwmarrin/discordgo"
)
//nolint:gochecknoglobals // False positive, cannot be overridden.
var instance translator
func init() {
instance = newTranslator()
}
// SetDefaults sets the locale used as a fallback.
// Not thread-safe; designed to be called during initialization.
func SetDefault(language discordgo.Locale) {
instance.SetDefault(language)
}
// LoadBundle loads a translation file corresponding to a specified locale.
// Not thread-safe; designed to be called during initialization.
func LoadBundle(language discordgo.Locale, file string) error {
return instance.LoadBundle(language, file)
}
// Get gets a translation corresponding to a locale and a key.
// Optional Vars parameter is used to inject variables in the translation.
// When a key does not match any translations in the desired locale,
// the default locale is used instead. If the situation persists with the fallback,
// key is returned. If more than one translation is available for dedicated key,
// it is picked randomly. Thread-safe.
func Get(language discordgo.Locale, key string, values ...Vars) string {
args := make(Vars)
for _, variables := range values {
for variable, value := range variables {
args[variable] = value
}
}
return instance.Get(language, key, args)
}
// GetDefault gets a translation corresponding to default locale and a key.
// Optional Vars parameter is used to inject variables in the translation.
// When a key does not match any translations in the default locale,
// key is returned. If more than one translation is available for dedicated key,
// it is picked randomly. Thread-safe.
func GetDefault(key string, values ...Vars) string {
args := make(Vars)
for _, variables := range values {
for variable, value := range variables {
args[variable] = value
}
}
return instance.GetDefault(key, args)
}
// GetLocalizations retrieves translations from every loaded bundles.
// Aims to simplify discordgo.ApplicationCommand instanciations by providing
// localizations structures that can be used for any localizable field (example:
// command name, description, etc). Thread-safe.
func GetLocalizations(key string, values ...Vars) *map[discordgo.Locale]string {
args := make(Vars)
for _, variables := range values {
for variable, value := range variables {
args[variable] = value
}
}
return instance.GetLocalizations(key, args)
}