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