Raw
 1package text
 2
 3import (
 4	"fmt"
 5	"runtime"
 6	"slices"
 7	"strings"
 8)
 9
10type Key string
11
12const FlashPrefix = "flash."
13
14type entry struct {
15	text string
16	site string
17}
18
19var catalog = map[Key]entry{}
20
21func Register(entries map[Key]string) bool {
22	_, file, line, _ := runtime.Caller(1)
23	site := fmt.Sprintf("%s:%d", file, line)
24	for k, v := range entries {
25		if prev, ok := catalog[k]; ok {
26			panic(fmt.Sprintf("duplicate catalog key %q registered at %s and %s", k, prev.site, site))
27		}
28		catalog[k] = entry{text: v, site: site}
29	}
30	return true
31}
32
33func Lookup(k Key) (string, bool) {
34	e, ok := catalog[k]
35	return e.text, ok
36}
37
38func Keys() []Key {
39	keys := make([]Key, 0, len(catalog))
40	for k := range catalog {
41		keys = append(keys, k)
42	}
43	slices.Sort(keys)
44	return keys
45}
46
47func (k Key) IsFlash() bool {
48	return strings.HasPrefix(string(k), FlashPrefix)
49}