forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.go
60 lines (51 loc) · 1.45 KB
/
lang.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
package lang
import (
"embed"
"encoding/json"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/world"
"golang.org/x/text/language"
)
// itemHash is a combination of an item's name and metadata. It is used as a key in hash maps.
type itemHash struct {
name string
meta int16
}
var (
//go:embed names/*
namesFS embed.FS
// names is a mapping from language.Tag to an item->display name mapping.
names = make(map[language.Tag]map[itemHash]string)
)
// DisplayName returns the display name of the item as shown in game in the language passed.
func DisplayName(item world.Item, locale language.Tag) (string, bool) {
id, meta := item.EncodeItem()
h := itemHash{name: id, meta: meta}
if _, ok := names[locale]; !ok && !load(locale) {
// Language not supported, default to American English.
return names[language.AmericanEnglish][h], false
}
name, ok := names[locale][h]
return name, ok
}
// load loads the locale for the item display names.
func load(locale language.Tag) bool {
b, err := namesFS.ReadFile("names/" + locale.String() + ".json")
if err != nil {
return false
}
var entries []struct {
ID string `json:"id"`
Meta int16 `json:"meta,omitempty"`
Name string `json:"name"`
}
err = json.Unmarshal(b, &entries)
if err != nil {
panic(err)
}
names[locale] = make(map[itemHash]string, len(entries))
for _, entry := range entries {
h := itemHash{name: entry.ID, meta: entry.Meta}
names[locale][h] = entry.Name
}
return true
}