BlueGit

timfa bluegit

Files

bluegit / internal / i18n / i18n.go

i18n.go

fix: schrijf de tagpagina volledig in spijkerschrift
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed eb7dced7 History
i18n.go 4.4 KiB Download Raw
1// Package i18n is the language catalog for the web interface.
2// English is the source text. Nederlands, Deutsch and Français are
3// translations. Cuneiform is the English text written phonetically
4// with Sumero-Akkadian signs.
5package i18n
6
7import (
8 "errors"
9 "fmt"
10 "strings"
11 "time"
12)
13
14// Lang is a choice stored on the user.
15type Lang string
16
17const (
18 EN Lang = "en"
19 NL Lang = "nl"
20 DE Lang = "de"
21 FR Lang = "fr"
22 Cuneiform Lang = "cuneiform"
23)
24
25// All is the order shown in the account settings.
26func All() []Lang { return []Lang{EN, NL, DE, FR, Cuneiform} }
27
28// Name is the label of the language in the picker.
29// These stay in their own script so a cuneiform page can still be left.
30func (l Lang) Name() string {
31 switch l {
32 case NL:
33 return "Nederlands"
34 case DE:
35 return "Deutsch"
36 case FR:
37 return "Français"
38 case Cuneiform:
39 return "Cuneiform"
40 default:
41 return "English"
42 }
43}
44
45// Parse accepts the five languages. Anything else is English.
46func Parse(s string) Lang {
47 switch Lang(strings.ToLower(strings.TrimSpace(s))) {
48 case NL, DE, FR, Cuneiform:
49 return Lang(strings.ToLower(strings.TrimSpace(s)))
50 default:
51 return EN
52 }
53}
54
55// Error is a user-visible failure identified by a catalog key.
56type Error struct {
57 Key string
58 Args []any
59}
60
61func (e Error) Error() string { return T(EN, e.Key, e.Args...) }
62
63// E builds a translatable error.
64func E(key string, args ...any) error { return Error{Key: key, Args: args} }
65
66// Text renders err in lang. Unknown errors stay as they are.
67func Text(lang Lang, err error) string {
68 if err == nil {
69 return ""
70 }
71 var e Error
72 if errors.As(err, &e) {
73 return T(lang, e.Key, e.Args...)
74 }
75 return err.Error()
76}
77
78// T looks up key and formats it. A missing translation in Nederlands,
79// Deutsch or Français falls back to English, then to the key itself.
80// Cuneiform never shows that English: it is the formatted sentence
81// written in signs, with printf verbs left intact.
82func T(lang Lang, key string, args ...any) string {
83 msg := lookup(lang, key)
84 if lang == Cuneiform {
85 msg = lookup(EN, key)
86 }
87 out := msg
88 if len(args) > 0 {
89 out = fmt.Sprintf(msg, args...)
90 }
91 if lang == Cuneiform {
92 return Transliterate(out)
93 }
94 return out
95}
96
97func lookup(lang Lang, key string) string {
98 if lang != EN && lang != Cuneiform {
99 if m, ok := catalogs[lang][key]; ok && m != "" {
100 return m
101 }
102 }
103 if m, ok := catalogs[EN][key]; ok {
104 return m
105 }
106 return key
107}
108
109// FormatDate is "2 January 2006" in the active language.
110func FormatDate(lang Lang, t time.Time) string {
111 if t.IsZero() {
112 return ""
113 }
114 month := T(lang, fmt.Sprintf("month.%d", int(t.Month())))
115 return fmt.Sprintf("%d %s %d", t.Day(), month, t.Year())
116}
117
118// MonthShort is the heatmap label, such as "Oct".
119func MonthShort(lang Lang, m time.Month) string {
120 return T(lang, fmt.Sprintf("month.s.%d", int(m)))
121}
122
123// Ago is a short relative time.
124func Ago(lang Lang, unix int64) string {
125 if unix <= 0 {
126 return ""
127 }
128 d := time.Since(time.Unix(unix, 0))
129 if d < time.Minute {
130 return T(lang, "ago.just")
131 }
132 if d < time.Hour {
133 n := int(d.Minutes())
134 if n < 1 {
135 n = 1
136 }
137 if n == 1 {
138 return T(lang, "ago.minute")
139 }
140 return T(lang, "ago.minutes", n)
141 }
142 if d < 24*time.Hour {
143 n := int(d.Hours())
144 if n < 1 {
145 n = 1
146 }
147 if n == 1 {
148 return T(lang, "ago.hour")
149 }
150 return T(lang, "ago.hours", n)
151 }
152 days := int(d.Hours() / 24)
153 if days < 1 {
154 days = 1
155 }
156 if days == 1 {
157 return T(lang, "ago.day")
158 }
159 if days < 14 {
160 return T(lang, "ago.days", days)
161 }
162 if days < 60 {
163 n := days / 7
164 if n < 1 {
165 n = 1
166 }
167 if n == 1 {
168 return T(lang, "ago.week")
169 }
170 return T(lang, "ago.weeks", n)
171 }
172 if days < 365 {
173 n := days / 30
174 if n < 1 {
175 n = 1
176 }
177 if n == 1 {
178 return T(lang, "ago.month")
179 }
180 return T(lang, "ago.months", n)
181 }
182 n := days / 365
183 if n < 1 {
184 n = 1
185 }
186 if n == 1 {
187 return T(lang, "ago.year")
188 }
189 return T(lang, "ago.years", n)
190}
191
192// Contrib is the heatmap sentence. year is the twelve-month total;
193// otherwise date is the long date of one cell.
194func Contrib(lang Lang, n int, date string, year bool) string {
195 switch {
196 case year && n <= 0:
197 return T(lang, "heat.none.year")
198 case year && n == 1:
199 return T(lang, "heat.one.year")
200 case year:
201 return T(lang, "heat.many.year", n)
202 case n <= 0:
203 return T(lang, "heat.none.day", date)
204 case n == 1:
205 return T(lang, "heat.one.day", date)
206 default:
207 return T(lang, "heat.many.day", n, date)
208 }
209}