-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_language.go
57 lines (49 loc) · 1.27 KB
/
form_language.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
package main
import (
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/form"
"golang.org/x/text/language/display"
"golang.org/x/text/message"
)
func newLanguageForm(p *message.Printer, prof profile) form.Form {
languages := message.DefaultCatalog.Languages()
dropdown := form.Dropdown{
Text: p.Sprintf("menu.language.dropdown.text"),
Options: make([]string, len(languages)),
}
for i, tag := range languages {
if tag == prof.Language {
dropdown.DefaultIndex = i
}
dropdown.Options[i] = p.Sprintf("menu.language.dropdown.option",
display.Self.Name(tag),
display.English.Languages().Name(tag),
)
}
return form.New(languageForm{
Dropdown: dropdown,
}, p.Sprintf("menu.language.title"))
}
type languageForm struct {
Dropdown form.Dropdown
}
func (f languageForm) Submit(s form.Submitter) {
if p, ok := s.(*player.Player); ok {
v, ok := handlers.Load(p.UUID())
if !ok {
return
}
h := v.(*handler)
prof := h.prof.Load()
tag := message.DefaultCatalog.Languages()[f.Dropdown.Value()]
if prof.Language == tag {
return
}
m := message.NewPrinter(tag)
h.m.Store(m)
prof.Language = tag
h.prof.Store(prof)
h.setInventory(p, m)
p.Message(m.Sprintf("language.changed", display.Self.Name(tag)))
}
}