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:
Ilker ManapandClaude Opus 5.5 committed 2026-10-05 00:12:53 +02:00
1 parent 30f7f3474a
commit 11231d8c2b
113 files changed
+8091 -1981

No files matched your search

+65 -50
View File
@@ -2,7 +2,6 @@ package web
import (
"context"
"fmt"
"net/http"
"sort"
"time"
@@ -26,7 +25,10 @@ type StatsReader interface {
// pctile is a percentile from the usage histograms as a person reads it:
// "180 ms", "4.2 s", "> 120 s" past the last bucket, "—" with nothing to read.
func pctile(q store.Quantiles, which int) string {
func pctile(q store.Quantiles, which int) string { return english.pctile(q, which) }
// pctile is the package's pctile in this language's numbers.
func (l *Lang) pctile(q store.Quantiles, which int) string {
if q.N == 0 {
return "—"
}
@@ -37,13 +39,13 @@ func pctile(q store.Quantiles, which int) string {
case 99:
v = q.P99
if q.Over {
return fmt.Sprintf("> %d s", v/1000)
return "> " + l.T("unit.s", l.Int(v/1000))
}
}
if v < 1000 {
return fmt.Sprintf("%d ms", v)
return l.T("unit.ms", l.Int(v))
}
return fmt.Sprintf("%.1f s", float64(v)/1000)
return l.T("unit.s", l.Num(float64(v)/1000, 1))
}
// usageWindow is one length of history the page offers, and the slice it is cut
@@ -53,17 +55,38 @@ func pctile(q store.Quantiles, which int) string {
// five-minute slices is 288 points across 720 pixels, which is a texture; a
// month in five-minute slices is 8,640, which is a smear. Roughly fifty to a
// hundred points is what a line of this size can actually show.
//
// Its name on the page is the catalogue's usage.window.KEY.
type usageWindow struct {
Key string
Label string
Span time.Duration
Width time.Duration
}
var usageWindows = []usageWindow{
{"24h", "last 24 hours", 24 * time.Hour, 20 * time.Minute},
{"7d", "last 7 days", 7 * 24 * time.Hour, 2 * time.Hour},
{"30d", "last 30 days", 30 * 24 * time.Hour, 12 * time.Hour},
{"24h", 24 * time.Hour, 20 * time.Minute},
{"7d", 7 * 24 * time.Hour, 2 * time.Hour},
{"30d", 30 * 24 * time.Hour, 12 * time.Hour},
}
func init() {
registerKeys("usage.window.24h", "usage.window.7d", "usage.window.30d",
"usage.group.org", "usage.group.team", "usage.group.project", "usage.group.user", "usage.group.model")
}
// windowChoices are the windows as the chooser offers them, in l.
func windowChoices(l *Lang, selected string) []windowChoice {
out := make([]windowChoice, 0, len(usageWindows))
for _, uw := range usageWindows {
out = append(out, windowChoice{uw.Key, l.T("usage.window." + uw.Key), uw.Key == selected})
}
return out
}
// windowChoice is one window in the chooser.
type windowChoice struct {
Key, Label string
Selected bool
}
func windowFor(key string) usageWindow {
@@ -99,12 +122,9 @@ type usageRow struct {
type adminUsageData struct {
Nav Nav
Groups []usageGroup
Windows []struct {
Key, Label string
Selected bool
}
By string
Window string
Windows []windowChoice
By string
Window string
Requests TimeSeries
Tokens TimeSeries
@@ -150,6 +170,7 @@ func (p *AccountPages) buildUsage(r *http.Request, s UsageAdmin, priced Priced)
by = store.GroupByTeam
}
win := windowFor(r.URL.Query().Get("window"))
l := langOf(r)
d := adminUsageData{
Nav: navFrom(r, "usage"),
@@ -160,24 +181,19 @@ func (p *AccountPages) buildUsage(r *http.Request, s UsageAdmin, priced Priced)
}
for _, g := range store.Groupings() {
d.Groups = append(d.Groups, usageGroup{
Key: string(g), Label: groupLabel(g), Selected: g == by,
Key: string(g), Label: groupLabel(l, g), Selected: g == by,
})
}
for _, uw := range usageWindows {
d.Windows = append(d.Windows, struct {
Key, Label string
Selected bool
}{uw.Key, uw.Label, uw.Key == win.Key})
}
d.Windows = windowChoices(l, win.Key)
rows, err := s.UsageBucketsBy(r.Context(), by, time.Now().Add(-win.Span), win.Width)
if err != nil {
p.log.Error("could not read usage over time", "by", by, "err", err)
d.Error = "could not read usage"
d.Error = l.T("usage.err.read")
return d
}
d.Requests, d.Tokens, d.Spend, d.Rows = usageCharts(rows)
d.Requests, d.Tokens, d.Spend, d.Rows = usageChartsIn(l, rows)
if sr, ok := s.(StatsReader); ok {
stats, err := sr.StatsBy(r.Context(), by, time.Now().Add(-win.Span))
if err != nil {
@@ -188,7 +204,7 @@ func (p *AccountPages) buildUsage(r *http.Request, s UsageAdmin, priced Priced)
}
d.HasStats = stats != nil
}
d.GPUHeld, d.GPUHours = p.gpuHours(r.Context(), win)
d.GPUHeld, d.GPUHours = p.gpuHours(r.Context(), l, win)
return d
}
@@ -205,7 +221,7 @@ type gpuHoursRow struct {
// holds is placement's, not the request log's: a model nobody asks still
// holds its cards. As far back as the store keeps history, which for a
// default Prometheus is fifteen days.
func (p *AccountPages) gpuHours(ctx context.Context, win usageWindow) (TimeSeries, []gpuHoursRow) {
func (p *AccountPages) gpuHours(ctx context.Context, l *Lang, win usageWindow) (TimeSeries, []gpuHoursRow) {
if p.history == nil {
return TimeSeries{}, nil
}
@@ -216,7 +232,7 @@ func (p *AccountPages) gpuHours(ctx context.Context, win usageWindow) (TimeSerie
var rows []gpuHoursRow
for i := range series {
if series[i].Name == "" {
series[i].Name = "no owner"
series[i].Name = l.T("usage.no_owner")
}
row := gpuHoursRow{Owner: series[i].Name, Now: series[i].Last()}
// Each point is the cards held at that instant, standing for the
@@ -225,7 +241,7 @@ func (p *AccountPages) gpuHours(ctx context.Context, win usageWindow) (TimeSerie
rows = append(rows, row)
}
sort.Slice(rows, func(i, j int) bool { return rows[i].Hours > rows[j].Hours })
return LineChart(series, "", 6), rows
return l.LineChart(series, "", 6), rows
}
// seriesOf turns each group's buckets into a line of one measure.
@@ -233,10 +249,10 @@ func (p *AccountPages) gpuHours(ctx context.Context, win usageWindow) (TimeSerie
// One pass per measure rather than one query per measure: the buckets are
// already in memory and already share a clock, and three queries that could
// disagree about the window is a worse answer than three loops.
func seriesOf(rows []store.UsageSeries, value func(store.Bucket) float64) []promq.Series {
func seriesOf(l *Lang, rows []store.UsageSeries, value func(store.Bucket) float64) []promq.Series {
out := make([]promq.Series, 0, len(rows))
for _, u := range rows {
s := promq.Series{Name: displayName(u)}
s := promq.Series{Name: displayName(l, u)}
for _, b := range u.Buckets {
s.Points = append(s.Points, promq.Point{At: b.Start, Value: value(b)})
}
@@ -250,45 +266,44 @@ func seriesOf(rows []store.UsageSeries, value func(store.Bucket) float64) []prom
// A person with no account is named rather than left blank: keys issued before
// accounts existed carry real traffic, and an unlabelled line in a legend is
// the one nobody can act on.
func displayName(u store.UsageSeries) string {
func displayName(l *Lang, u store.UsageSeries) string {
if u.Name != "" {
return u.Name
}
if u.Grouping == store.GroupByUser {
return "keys with no owner"
return l.T("usage.no_owner_keys")
}
if u.ID != "" {
return u.ID
}
return "unknown"
return l.T("usage.unknown")
}
func groupLabel(g store.Grouping) string {
// groupLabel names a grouping in l.
func groupLabel(l *Lang, g store.Grouping) string {
switch g {
case store.GroupByOrg:
return "organization"
case store.GroupByTeam:
return "team"
case store.GroupByProject:
return "project"
case store.GroupByUser:
return "person"
case store.GroupByModel:
return "model"
case store.GroupByOrg, store.GroupByTeam, store.GroupByProject, store.GroupByUser, store.GroupByModel:
return l.T("usage.group." + string(g))
default:
return string(g)
}
}
// usageCharts draws usage over time three ways, with the table beneath them.
// usageCharts is usageChartsIn in English.
func usageCharts(rows []store.UsageSeries) (req, tok, spend TimeSeries, table []usageRow) {
req = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
return usageChartsIn(english, rows)
}
// usageChartsIn draws usage over time three ways, with the table beneath
// them, the names and figures in l.
func usageChartsIn(l *Lang, rows []store.UsageSeries) (req, tok, spend TimeSeries, table []usageRow) {
req = l.LineChartTotals(seriesOf(l, rows, func(b store.Bucket) float64 {
return float64(b.Requests)
}), "", 4)
tok = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
tok = l.LineChartTotals(seriesOf(l, rows, func(b store.Bucket) float64 {
return float64(b.Prompt + b.Completion)
}), "", 4)
spend = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
spend = l.LineChartTotals(seriesOf(l, rows, func(b store.Bucket) float64 {
// Shown in whole units, as every other spend figure is: a budget is
// written in units and a chart in micros would not compare with it.
return float64(b.CostMicros) / 1e6
@@ -296,10 +311,10 @@ func usageCharts(rows []store.UsageSeries) (req, tok, spend TimeSeries, table []
for _, u := range rows {
table = append(table, usageRow{
ID: u.ID,
Name: displayName(u),
Name: displayName(l, u),
Requests: u.Requests,
Tokens: u.Tokens(),
Spend: unitsOf(u.CostMicros),
Spend: l.Units(u.CostMicros),
Errors: u.Errors,
})
}