| 1 | // Package locale is how Fichas writes dates, times and numbers for the |
| 2 | // person reading (level-up spec §6.8). The interface stays English; only |
| 3 | // the formats change. |
| 4 | // |
| 5 | // Copied from Mapa (feat/locale, 3895499: internal/mapa/locale, its |
| 6 | // build note 85, Fable APPROVED) so two Oficina apps never write a |
| 7 | // reader's date two ways. Changes from Mapa's: this comment, the |
| 8 | // localizer's translator (rastrillo's English base catalog rather than |
| 9 | // keys as their own text) and "Fichas" in Check's refusal. dates.json and |
| 10 | // testdata/date_labels.json are Mapa's, byte for byte, and are never |
| 11 | // regenerated here: a change comes from Mapa's `make locale-dates`. The |
| 12 | // package should move into rastrillo (§6.8, R3), so that no third copy |
| 13 | // is made. |
| 14 | // |
| 15 | // The locale is negotiated from the request's Accept-Language among the |
| 16 | // locales offered here, the team's own (FICHAS_LOCALE, en-IE unless set) |
| 17 | // first. Numbers go through rastrillo's own number function, which reads |
| 18 | // the CLDR pack each locale links. rastrillo's packs hold no date data, |
| 19 | // so the CLDR date patterns are dates.json, which Mapa's |
| 20 | // editor/gen-dates.mjs reads out of ICU; format.go interprets them. |
| 21 | package locale |
| 22 | |
| 23 | import ( |
| 24 | _ "embed" |
| 25 | "encoding/json" |
| 26 | "fmt" |
| 27 | "slices" |
| 28 | |
| 29 | "amadan.net/rastrillo/rastrillo" |
| 30 | "amadan.net/rastrillo/rastrillo/locales" |
| 31 | "amadan.net/rastrillo/rastrillo/ui" |
| 32 | |
| 33 | // The packs the offered locales are formatted from: rastrillo |
| 34 | // refuses a locale whose pack this binary does not link. |
| 35 | _ "amadan.net/rastrillo/rastrillo/locales/de" |
| 36 | _ "amadan.net/rastrillo/rastrillo/locales/en" |
| 37 | _ "amadan.net/rastrillo/rastrillo/locales/es" |
| 38 | _ "amadan.net/rastrillo/rastrillo/locales/fr" |
| 39 | ) |
| 40 | |
| 41 | // Default is the team's locale when MAPA_LOCALE is unset. |
| 42 | const Default = "en-IE" |
| 43 | |
| 44 | //go:embed dates.json |
| 45 | var datesJSON []byte |
| 46 | |
| 47 | // field is one part of a pattern: a literal, or a date's field. |
| 48 | type field struct { |
| 49 | Type string `json:"t"` |
| 50 | Value string `json:"v,omitempty"` |
| 51 | Pad bool `json:"pad,omitempty"` |
| 52 | Cycle string `json:"cycle,omitempty"` |
| 53 | } |
| 54 | |
| 55 | // pattern is one kind of date in one locale, with the names it uses. |
| 56 | type pattern struct { |
| 57 | Parts []field `json:"parts"` |
| 58 | Weekdays []string `json:"weekdays,omitempty"` |
| 59 | Months []string `json:"months,omitempty"` |
| 60 | Periods []string `json:"periods,omitempty"` |
| 61 | } |
| 62 | |
| 63 | // data is one locale's entry in dates.json. |
| 64 | type data struct { |
| 65 | FirstDay int `json:"firstDay"` |
| 66 | Order string `json:"order"` |
| 67 | // Separator is the mark between a numeric date's numbers. |
| 68 | Separator string `json:"separator"` |
| 69 | Weekdays []string `json:"weekdays"` |
| 70 | Narrow []string `json:"narrow"` |
| 71 | Patterns map[string]*pattern `json:"patterns"` |
| 72 | } |
| 73 | |
| 74 | // Locale is one offered locale: its date patterns and the rastrillo |
| 75 | // localizer its numbers are written with. Its methods treat a nil |
| 76 | // Locale as the default's. |
| 77 | type Locale struct { |
| 78 | code string |
| 79 | // L is rastrillo's localizer, what {{number L n}} is handed. |
| 80 | L *ui.Localizer |
| 81 | data data |
| 82 | } |
| 83 | |
| 84 | var ( |
| 85 | offered []string |
| 86 | byCode = map[string]*Locale{} |
| 87 | ) |
| 88 | |
| 89 | func init() { |
| 90 | var file struct { |
| 91 | Offered []string `json:"offered"` |
| 92 | Locales map[string]data `json:"locales"` |
| 93 | } |
| 94 | if err := json.Unmarshal(datesJSON, &file); err != nil { |
| 95 | panic("locale: dates.json: " + err.Error()) |
| 96 | } |
| 97 | offered = file.Offered |
| 98 | for _, code := range offered { |
| 99 | byCode[code] = &Locale{code: code, data: file.Locales[code], |
| 100 | L: ui.NewLocalizer(code, locales.Resolve(code).ID, |
| 101 | english)} |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // english is the localizer's translator: Fichas' own text is English |
| 106 | // literals, and the framework partials' keys (rastrillo.ui.*, a money |
| 107 | // field's errors) read rastrillo's English base catalog. |
| 108 | var english = func() func(string, ...any) string { |
| 109 | l, err := rastrillo.NewLocales([]string{"en"}, "en", rastrillo.BaseCatalog(), nil) |
| 110 | if err != nil { |
| 111 | panic("locale: rastrillo's English catalog: " + err.Error()) |
| 112 | } |
| 113 | return func(key string, args ...any) string { return l.Tf("en", key, args...) } |
| 114 | }() |
| 115 | |
| 116 | // names are the offered locales in English, the interface's language. |
| 117 | var names = map[string]string{"en-IE": "English (Ireland)", |
| 118 | "en-GB": "English (UK)", "en-US": "English (US)", "de": "German", |
| 119 | "fr": "French", "es": "Spanish"} |
| 120 | |
| 121 | // Name is an offered locale's name in English; the code for one with |
| 122 | // none. |
| 123 | func Name(code string) string { |
| 124 | if name, ok := names[code]; ok { |
| 125 | return name |
| 126 | } |
| 127 | return code |
| 128 | } |
| 129 | |
| 130 | // Code is the locale's code, the default's for a nil one. |
| 131 | func (in *Locale) Code() string { return in.orDefault().code } |
| 132 | |
| 133 | // Offered is every locale Fichas can write in, in dates.json's order. |
| 134 | func Offered() []string { return slices.Clone(offered) } |
| 135 | |
| 136 | // Get is the offered locale code names, or nil. |
| 137 | func Get(code string) *Locale { return byCode[code] } |
| 138 | |
| 139 | // Localizer is rastrillo's localizer for the locale, the default's for |
| 140 | // a nil one: what {{number L n}} is handed. |
| 141 | func (in *Locale) Localizer() *ui.Localizer { return in.orDefault().L } |
| 142 | |
| 143 | // orDefault is in, or the default locale for a nil in. |
| 144 | func (in *Locale) orDefault() *Locale { |
| 145 | if in == nil { |
| 146 | return byCode[Default] |
| 147 | } |
| 148 | return in |
| 149 | } |
| 150 | |
| 151 | // Check refuses a team locale Fichas cannot write in: not CLDR's |
| 152 | // spelling, not offered, or offered without its pack linked. |
| 153 | func Check(team string) error { |
| 154 | if _, err := locales.CanonicalIdentity(team); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | if Get(team) == nil { |
| 158 | return fmt.Errorf("%q is not a locale Fichas offers; use one of %v", |
| 159 | team, offered) |
| 160 | } |
| 161 | _, err := locales.CheckConfig(team, offered) |
| 162 | return err |
| 163 | } |
| 164 | |
| 165 | // Negotiation is rastrillo's locale set for a team: the offered |
| 166 | // locales with the team's first, so a reader whose language Fichas |
| 167 | // offers only in other regions (en-AU, say) gets the team's. |
| 168 | func Negotiation(team string) (*rastrillo.Locales, error) { |
| 169 | codes := append([]string{team}, slices.DeleteFunc(Offered(), |
| 170 | func(code string) bool { return code == team })...) |
| 171 | return rastrillo.NewLocales(codes, team, nil, nil) |
| 172 | } |
| 173 | |
| 174 | // number is rastrillo's number template function, which formats from |
| 175 | // the localizer's CLDR pack. |
| 176 | var number = ui.Funcs()["number"].(func(...any) (any, error)) |
| 177 | |
| 178 | // Number is a count as the locale writes it: 1,234 or 1.234. |
| 179 | func (in *Locale) Number(count int) string { |
| 180 | written, err := number(in.Localizer(), count) |
| 181 | if err != nil { |
| 182 | return fmt.Sprint(count) |
| 183 | } |
| 184 | return fmt.Sprint(written) |
| 185 | } |
| 186 | |
| 187 | // Client is what the editor is given to write dates with (dates.mjs): |
| 188 | // the patterns its chips, the [[ list and the picker use. |
| 189 | type Client struct { |
| 190 | Code string `json:"code"` |
| 191 | FirstDay int `json:"firstDay"` |
| 192 | Order string `json:"order"` |
| 193 | Weekdays []string `json:"weekdays"` |
| 194 | Narrow []string `json:"narrow"` |
| 195 | Patterns map[string]*pattern `json:"patterns"` |
| 196 | } |
| 197 | |
| 198 | // clientKinds are the patterns the editor draws with. |
| 199 | var clientKinds = []string{"day", "dayYear", "monthYear"} |
| 200 | |
| 201 | // Client is the editor's share of the locale's data. |
| 202 | func (in *Locale) Client() Client { |
| 203 | in = in.orDefault() |
| 204 | out := Client{Code: in.code, FirstDay: in.data.FirstDay, |
| 205 | Order: in.data.Order, Weekdays: in.data.Weekdays, |
| 206 | Narrow: in.data.Narrow, Patterns: map[string]*pattern{}} |
| 207 | for _, kind := range clientKinds { |
| 208 | out.Patterns[kind] = in.data.Patterns[kind] |
| 209 | } |
| 210 | return out |
| 211 | } |
| 212 | |