The portal in English and Turkish: a catalogue per language, a switcher at the foot of every page, landing.language, Turkish numbers and dates
Every template and handler message in internal/web goes through a JSON
catalogue under internal/web/locales/<code>/, with English as the fallback.
The language is the person's choice (a cookie set by /_lang/{code}), else
Accept-Language, else landing.language. Tests refuse a key some language
lacks, a translation with other arguments than English, and template text
left in English.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
30f7f3474a
commit
11231d8c2b
113 files changed
+8091
-1981
No files matched your search
+20
-54
@@ -213,7 +213,15 @@ func unitsOf(micros int64) string {
|
||||
return fmt.Sprintf("%.2f", float64(micros)/1e6)
|
||||
}
|
||||
|
||||
var tmpl = template.Must(template.New("").Funcs(funcs).ParseFS(files, "templates/*.html"))
|
||||
// tmpl is the English set of pages, for what is rendered with no request to
|
||||
// take a language from (the published website). A request's pages come from
|
||||
// its language's own set — see parseTemplates.
|
||||
var tmpl *template.Template
|
||||
|
||||
func init() {
|
||||
parseTemplates()
|
||||
tmpl = english.tmpl
|
||||
}
|
||||
|
||||
// Static serves the embedded assets, mounted on both listeners because both
|
||||
// pages use them.
|
||||
@@ -260,21 +268,25 @@ func (n noDirs) Open(name string) (fs.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, name string, data any) {
|
||||
renderStatus(w, http.StatusOK, name, data)
|
||||
func render(w http.ResponseWriter, r *http.Request, name string, data any) {
|
||||
renderStatus(w, r, http.StatusOK, name, data)
|
||||
}
|
||||
|
||||
// renderStatus is render with a status other than 200.
|
||||
func renderStatus(w http.ResponseWriter, code int, name string, data any) {
|
||||
func renderStatus(w http.ResponseWriter, r *http.Request, code int, name string, data any) {
|
||||
l := langOf(r)
|
||||
// Rendered whole before anything is sent: the policy names the page's
|
||||
// inline scripts by their hashes, and a template that fails halfway is
|
||||
// an error page rather than half a page.
|
||||
var page bytes.Buffer
|
||||
if err := tmpl.ExecuteTemplate(&page, name, data); err != nil {
|
||||
if err := l.tmpl.ExecuteTemplate(&page, name, data); err != nil {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Content-Language", l.Code)
|
||||
// The same address answers in more than one language, chosen by these.
|
||||
w.Header().Add("Vary", "Accept-Language, Cookie")
|
||||
securePage(w.Header(), &page)
|
||||
w.WriteHeader(code)
|
||||
_, _ = page.WriteTo(w)
|
||||
@@ -282,61 +294,15 @@ func renderStatus(w http.ResponseWriter, code int, name string, data any) {
|
||||
|
||||
// humanLatency writes a mean latency the way a person reads one: 5 ms, 8.8 s,
|
||||
// 1m 51s — not 110695 ms.
|
||||
func humanLatency(ms float64) string {
|
||||
switch {
|
||||
case ms < 1000:
|
||||
return fmt.Sprintf("%.0f ms", ms)
|
||||
case ms < 60_000:
|
||||
return fmt.Sprintf("%.1f s", ms/1000)
|
||||
default:
|
||||
d := time.Duration(ms) * time.Millisecond
|
||||
return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
}
|
||||
func humanLatency(ms float64) string { return english.Latency(ms) }
|
||||
|
||||
// ago says how long ago something was the way a person would: to the unit
|
||||
// that matters and no finer. "3h7m25s ago" is precise about seconds nobody
|
||||
// asked about and slower to read than "3 hours ago".
|
||||
func ago(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "never"
|
||||
}
|
||||
d := time.Since(t)
|
||||
unit := func(n int, one string) string {
|
||||
if n == 1 {
|
||||
return "1 " + one + " ago"
|
||||
}
|
||||
return fmt.Sprintf("%d %ss ago", n, one)
|
||||
}
|
||||
switch {
|
||||
case d < 45*time.Second:
|
||||
return "just now"
|
||||
case d < time.Hour:
|
||||
return unit(int(d.Minutes()+0.5), "minute")
|
||||
case d < 36*time.Hour:
|
||||
return unit(int(d.Hours()+0.5), "hour")
|
||||
case d < 14*24*time.Hour:
|
||||
return unit(int(d.Hours()/24+0.5), "day")
|
||||
case d < 60*24*time.Hour:
|
||||
return unit(int(d.Hours()/24/7+0.5), "week")
|
||||
default:
|
||||
return unit(int(d.Hours()/24/30+0.5), "month")
|
||||
}
|
||||
}
|
||||
func ago(t time.Time) string { return english.Ago(t) }
|
||||
|
||||
// humanBytes writes a size in binary units, the way the shell does.
|
||||
func humanBytes(n int64) string {
|
||||
const unit = 1024
|
||||
if n < unit {
|
||||
return fmt.Sprintf("%d B", n)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for v := n / unit; v >= unit; v /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
func humanBytes(n int64) string { return english.Bytes(n) }
|
||||
|
||||
// windowWord names a chart window the way a sentence would: "hour", "6
|
||||
// hours", "30 minutes", rather than Go's 1h0m0s.
|
||||
|
||||
Reference in new issue
Block a user