| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 11 | ) |
| 12 | |
| 13 | // The reader's locale (level-up build note 85). It is the person's own |
| 14 | // choice in Settings when they made one; else what their browser's |
| 15 | // Accept-Language asks for among the locales Mapa offers, matched by |
| 16 | // rastrillo's negotiation with the team's own (MAPA_LOCALE) first; else |
| 17 | // the team's. negotiate puts the browser's on the request for the whole |
| 18 | // router, and ownLocale, behind the session, puts the person's over it. |
| 19 | // Every place that writes a date or a number for a person reads it |
| 20 | // from there: the viewer, the index's reads, the templates, the island. |
| 21 | // |
| 22 | // Only the header is read. rastrillo's Locales.Middleware would also |
| 23 | // take a /de/ path prefix and its rastrillo_locale cookie over it, and |
| 24 | // Mapa writes neither: a prefixed page would be half in German, its |
| 25 | // links and fetches answered in the header's locale (review round 8, |
| 26 | // M2). |
| 27 | |
| 28 | type localeKey struct{} |
| 29 | |
| 30 | // browserKey holds the locale the browser asked for, which Settings |
| 31 | // offers as the default. |
| 32 | type browserKey struct{} |
| 33 | |
| 34 | // negotiate is the router's locale middleware. |
| 35 | func (app *app) negotiate(next http.Handler) http.Handler { |
| 36 | return http.HandlerFunc(func(writer http.ResponseWriter, |
| 37 | request *http.Request) { |
| 38 | browser := app.browserLocale(request) |
| 39 | ctx := context.WithValue(request.Context(), browserKey{}, browser) |
| 40 | ctx = context.WithValue(ctx, localeKey{}, browser) |
| 41 | next.ServeHTTP(writer, request.WithContext(ctx)) |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | // browserLocale is the offered locale the request's Accept-Language |
| 46 | // matches, or the team's: rastrillo's Negotiate over a request that |
| 47 | // carries the header and nothing else, so no path prefix or cookie |
| 48 | // can outrank it. |
| 49 | func (app *app) browserLocale(request *http.Request) *locale.Locale { |
| 50 | asked := &http.Request{URL: &url.URL{Path: "/"}, Header: http.Header{}} |
| 51 | if languages := request.Header.Values("Accept-Language"); len( |
| 52 | languages) > 0 { |
| 53 | asked.Header["Accept-Language"] = languages |
| 54 | } |
| 55 | if chosen := locale.Get(app.locales.Negotiate(asked)); chosen != nil { |
| 56 | return chosen |
| 57 | } |
| 58 | return app.teamLocale() |
| 59 | } |
| 60 | |
| 61 | // teamLocale is MAPA_LOCALE's locale. |
| 62 | func (app *app) teamLocale() *locale.Locale { |
| 63 | return locale.Get(firstOf(app.config.Locale, locale.Default)) |
| 64 | } |
| 65 | |
| 66 | // ownLocale puts the person's own choice over the browser's, behind |
| 67 | // the session. Reading it writes nothing (gate 10). |
| 68 | func (app *app) ownLocale(next http.Handler) http.Handler { |
| 69 | return http.HandlerFunc(func(writer http.ResponseWriter, |
| 70 | request *http.Request) { |
| 71 | address, _ := app.identity(request) |
| 72 | viewer, err := app.store.Viewer(request.Context(), address, |
| 73 | app.config.TimeZone) |
| 74 | if own := locale.Get(viewer.OwnLocale); err == nil && own != nil { |
| 75 | request = request.WithContext(context.WithValue( |
| 76 | request.Context(), localeKey{}, own)) |
| 77 | } |
| 78 | next.ServeHTTP(writer, request) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | // browserIn is the locale the request's browser asked for. |
| 83 | func browserIn(request *http.Request) *locale.Locale { |
| 84 | chosen, _ := request.Context().Value(browserKey{}).(*locale.Locale) |
| 85 | return chosen |
| 86 | } |
| 87 | |
| 88 | // localeIn is the request's locale; nil outside the router, where |
| 89 | // Built.ErrorPage puts the team's on the request first. |
| 90 | func localeIn(request *http.Request) *locale.Locale { |
| 91 | chosen, _ := request.Context().Value(localeKey{}).(*locale.Locale) |
| 92 | return chosen |
| 93 | } |
| 94 | |
| 95 | // localeFuncs are the template functions that write dates and numbers, |
| 96 | // bound to one locale: each page's templates are parsed once and cloned |
| 97 | // per offered locale with these (render.go), so the function map a |
| 98 | // request executes is never rebound. {{number L n}} is rastrillo's own |
| 99 | // number function given that locale's localizer. |
| 100 | func localeFuncs(dates *locale.Locale) template.FuncMap { |
| 101 | return template.FuncMap{ |
| 102 | "L": dates.Localizer, |
| 103 | "longDate": dates.Long, |
| 104 | "shortDate": dates.Short, |
| 105 | "dayTitle": dates.DayTitle, |
| 106 | "dayYear": dates.YearPart, |
| 107 | "stamp": func(at time.Time) string { return dates.Stamp(at) }, |
| 108 | "ago": func(when time.Time) string { |
| 109 | return relative(dates, time.Now(), when) |
| 110 | }, |
| 111 | "editedWhen": func(when time.Time) string { |
| 112 | return editedWhen(dates, when) |
| 113 | }, |
| 114 | } |
| 115 | } |
| 116 | |