oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3// Formats: the reader's locale (level-up spec §6.8). Values are stored
4// canonical and locale-free; the reader's locale applies only where a
5// value is drawn and where what a person typed is read. The interface
6// stays English: the localizer carries English words and the reader's
7// formatting locale.
8
9import (
10 "context"
11 "fmt"
12 "net/http"
13 "net/url"
14 "strconv"
15 "strings"
16
17 "amadan.net/rastrillo/rastrillo/ui"
18
19 "amadan.net/oficina/fichas/internal/fichas/locale"
20)
21
22// teamLocale is FICHAS_LOCALE, checked as Mapa checks MAPA_LOCALE (its
23// build note 85): CLDR's spelling, one of the offered locales, its pack
24// linked. en-IE unless set.
25func teamLocale(v string) (string, error) {
26 v = strings.TrimSpace(v)
27 if v == "" {
28 v = locale.Default
29 }
30 if err := locale.Check(v); err != nil {
31 return "", fmt.Errorf("fichas: FICHAS_LOCALE: %w", err)
32 }
33 return v, nil
34}
35
36// browserLocale is the offered locale the request's Accept-Language
37// asks for, the team's when it asks for none (§6.8, as Mapa's
38// browserLocale). rastrillo's negotiation is handed a request carrying
39// that header and nothing else: its middleware would also take a /de/
40// path prefix and its own cookie over the header, and Fichas writes
41// neither.
42func (a *app) browserLocale(r *http.Request) *locale.Locale {
43 asked := &http.Request{URL: &url.URL{Path: "/"}, Header: http.Header{}}
44 if languages := r.Header.Values("Accept-Language"); len(languages) > 0 {
45 asked.Header["Accept-Language"] = languages
46 }
47 if chosen := locale.Get(a.negotiation.Negotiate(asked)); chosen != nil {
48 return chosen
49 }
50 return locale.Get(a.cfg.Locale)
51}
52
53type datesKey struct{}
54
55// withFormats puts the reader's locale on every request: its localizer
56// (English words, the reader's formats) and its dates.
57func (a *app) withFormats(next http.Handler) http.Handler {
58 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59 chosen := a.browserLocale(r)
60 r = ui.WithLocalizer(r, chosen.Localizer())
61 next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), datesKey{}, chosen)))
62 })
63}
64
65// readerL is the request's localizer, or the team's when the middleware
66// did not run (a test calling a handler directly).
67func (a *app) readerL(r *http.Request) *ui.Localizer {
68 if l := ui.LocalizerFrom(r); l != nil {
69 return l
70 }
71 return locale.Get(a.cfg.Locale).Localizer()
72}
73
74// datesOf is the date formatter that goes with a localizer: the offered
75// locale it was made for, by its identity. nil-safe: the default's.
76func datesOf(L *ui.Localizer) *locale.Locale {
77 if L == nil {
78 return nil
79 }
80 return locale.Get(L.Identity())
81}
82
83// formats maps each offered locale's formatting id (what a page echoes,
84// rastrillo's Resolve(code).ID) to the locale.
85var formats = func() map[string]*locale.Locale {
86 out := map[string]*locale.Locale{}
87 for _, code := range locale.Offered() {
88 l := locale.Get(code)
89 if _, taken := out[l.Localizer().Format()]; !taken {
90 out[l.Localizer().Format()] = l
91 }
92 }
93 return out
94}()
95
96// localizerFor is the localizer for an echoed formatting id; nil when it
97// names no offered locale's.
98func localizerFor(format string) *ui.Localizer {
99 if l, ok := formats[format]; ok {
100 return l.Localizer()
101 }
102 return nil
103}
104
105// rastrilloNumber is rastrillo's {{number}}, called from Go for the
106// sentences Fichas builds in code ("7 of 10 customers").
107var rastrilloNumber = ui.Funcs()["number"].(func(...any) (any, error))
108
109// count writes n in L's format, through rastrillo.
110func count(L *ui.Localizer, n int) string {
111 v, err := rastrilloNumber(L, n)
112 if err != nil {
113 return strconv.Itoa(n)
114 }
115 return fmt.Sprint(v)
116}
117
118// showNumber is a stored number as L's reader sees it. Until rastrillo's
119// {{number}} reads the packs (§6.8, R1), a number value is drawn from
120// the pack's data alone, in the form the interim reading reads: the
121// canonical digits, the pack's decimal mark, no group marks. x/text's
122// older data disagrees with the packs on marks in dozens of locales, and
123// a value drawn by one and read by the other could come back a thousand
124// times wrong. A stored value that is not canonical shows as stored.
125func showNumber(L *ui.Localizer, v string) string { return seedNumber(L.Format(), v) }
126
127// canonicalNumber reports whether v is in §6.2's canonical form: an
128// optional minus, digits with no leading zero, and a fraction with no
129// trailing zero; never "-0".
130func canonicalNumber(v string) bool {
131 neg := strings.HasPrefix(v, "-")
132 body := strings.TrimPrefix(v, "-")
133 whole, frac, hasFrac := strings.Cut(body, ".")
134 if whole == "" || !allDigits(whole) || (len(whole) > 1 && whole[0] == '0') {
135 return false
136 }
137 if hasFrac && (frac == "" || !allDigits(frac) || strings.HasSuffix(frac, "0")) {
138 return false
139 }
140 return !(neg && whole == "0" && !hasFrac)
141}
142
143func allDigits(s string) bool {
144 for i := 0; i < len(s); i++ {
145 if s[i] < '0' || s[i] > '9' {
146 return false
147 }
148 }
149 return s != ""
150}
151