/
database.go
151 lines (119 loc) · 3.25 KB
/
database.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
package localedata
import (
"embed"
"fmt"
"sort"
"strconv"
"github.com/nyaruka/go-locales/fdcc"
"github.com/pkg/errors"
)
//go:embed locales/*
var static embed.FS
// TODO
var defaults = map[string][]string{
"LC_TIME.week": {"7", "19971130", "7"},
}
type LC string
type Database struct {
locales map[string]*Locale
codes []string
}
type Locale struct {
categories map[LC]*Category
}
func newLocale(s *fdcc.Set) *Locale {
categories := make(map[LC]*Category, len(s.Categories))
for _, c := range s.Categories {
categories[LC(c.Name)] = newCategory(c)
}
return &Locale{categories: categories}
}
type Category struct {
copiesFrom string
values map[string][]string
}
func newCategory(c *fdcc.Category) *Category {
copiesFrom := c.CopiesFrom()
if copiesFrom != "" {
return &Category{copiesFrom: copiesFrom}
}
values := make(map[string][]string, len(c.Body))
for _, l := range c.Body {
values[l.Keyword] = l.Operands
}
return &Category{values: values}
}
func LoadDatabase() (*Database, error) {
files, err := static.ReadDir("locales")
if err != nil {
return nil, err
}
locales := make(map[string]*Locale, len(files))
codes := make([]string, 0, len(files))
for _, f := range files {
code := f.Name()
file, err := static.Open(fmt.Sprintf("locales/%s", code))
if err != nil {
return nil, errors.Wrapf(err, "unable to open file %s", code)
}
defer file.Close()
p := fdcc.NewParser(file)
set, err := p.Parse()
if err != nil {
return nil, errors.Wrapf(err, "unable to parse file %s", code)
}
locales[code] = newLocale(set)
codes = append(codes, code)
}
sort.Strings(codes)
return &Database{locales, codes}, nil
}
// Query returns the operands of the given locale + category + key
func (d *Database) Query(code string, lc LC, keyword string) ([]string, error) {
locale := d.locales[code]
if locale == nil {
return nil, fmt.Errorf("no such locale %s", code)
}
category := locale.categories[lc]
if category == nil {
return nil, fmt.Errorf("no such category %s in locale %s", lc, code)
}
if category.copiesFrom != "" {
return d.Query(category.copiesFrom, lc, keyword)
}
operands, exists := category.values[keyword]
if !exists {
operands, exists = defaults[fmt.Sprintf("%s.%s", lc, keyword)]
if !exists {
return nil, fmt.Errorf("no such keyword %s in category %s in locale %s", keyword, lc, code)
}
}
return operands, nil
}
// QueryString is a helper for keys which are a single string
func (d *Database) QueryString(code string, lc LC, keyword string) (string, error) {
ops, err := d.Query(code, lc, keyword)
if err != nil {
return "", err
}
if len(ops) < 1 {
return "", fmt.Errorf("keyword %s has no operands", keyword)
}
return ops[0], nil
}
// QueryInteger is a helper for keys which are a single integer
func (d *Database) QueryInteger(code string, lc LC, keyword string) (int, error) {
op, err := d.QueryString(code, lc, keyword)
if err != nil {
return 0, err
}
val, err := strconv.Atoi(op)
if err != nil {
return 0, fmt.Errorf("keyword %s is not an integer", keyword)
}
return val, nil
}
// Codes returns the list of all locale codes (mostly BCP47 tho includes other special values such as POSIX, i18n etc), sorted alphabetically
func (d *Database) Codes() []string {
return d.codes
}