BlueGit

timfa bluegit

Files

bluegit / internal / i18n / i18n.go

i18n.go

feat: nieuw-formulier, groepsoverzicht, logo's en talen
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d1203d3b History
i18n.go 4.3 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 falls back to English,
79// then to the key itself. Cuneiform is the formatted English, written in signs.
80func T(lang Lang, key string, args ...any) string {
81 msg := lookup(lang, key)
82 if lang == Cuneiform {
83 msg = lookup(EN, key)
84 }
85 out := msg
86 if len(args) > 0 {
87 out = fmt.Sprintf(msg, args...)
88 }
89 if lang == Cuneiform {
90 return Transliterate(out)
91 }
92 return out
93}
94
95func lookup(lang Lang, key string) string {
96 if lang != EN && lang != Cuneiform {
97 if m, ok := catalogs[lang][key]; ok && m != "" {
98 return m
99 }
100 }
101 if m, ok := catalogs[EN][key]; ok {
102 return m
103 }
104 return key
105}
106
107// FormatDate is "2 January 2006" in the active language.
108func FormatDate(lang Lang, t time.Time) string {
109 if t.IsZero() {
110 return ""
111 }
112 month := T(lang, fmt.Sprintf("month.%d", int(t.Month())))
113 return fmt.Sprintf("%d %s %d", t.Day(), month, t.Year())
114}
115
116// MonthShort is the heatmap label, such as "Oct".
117func MonthShort(lang Lang, m time.Month) string {
118 return T(lang, fmt.Sprintf("month.s.%d", int(m)))
119}
120
121// Ago is a short relative time.
122func Ago(lang Lang, unix int64) string {
123 if unix <= 0 {
124 return ""
125 }
126 d := time.Since(time.Unix(unix, 0))
127 if d < time.Minute {
128 return T(lang, "ago.just")
129 }
130 if d < time.Hour {
131 n := int(d.Minutes())
132 if n < 1 {
133 n = 1
134 }
135 if n == 1 {
136 return T(lang, "ago.minute")
137 }
138 return T(lang, "ago.minutes", n)
139 }
140 if d < 24*time.Hour {
141 n := int(d.Hours())
142 if n < 1 {
143 n = 1
144 }
145 if n == 1 {
146 return T(lang, "ago.hour")
147 }
148 return T(lang, "ago.hours", n)
149 }
150 days := int(d.Hours() / 24)
151 if days < 1 {
152 days = 1
153 }
154 if days == 1 {
155 return T(lang, "ago.day")
156 }
157 if days < 14 {
158 return T(lang, "ago.days", days)
159 }
160 if days < 60 {
161 n := days / 7
162 if n < 1 {
163 n = 1
164 }
165 if n == 1 {
166 return T(lang, "ago.week")
167 }
168 return T(lang, "ago.weeks", n)
169 }
170 if days < 365 {
171 n := days / 30
172 if n < 1 {
173 n = 1
174 }
175 if n == 1 {
176 return T(lang, "ago.month")
177 }
178 return T(lang, "ago.months", n)
179 }
180 n := days / 365
181 if n < 1 {
182 n = 1
183 }
184 if n == 1 {
185 return T(lang, "ago.year")
186 }
187 return T(lang, "ago.years", n)
188}
189
190// Contrib is the heatmap sentence. year is the twelve-month total;
191// otherwise date is the long date of one cell.
192func Contrib(lang Lang, n int, date string, year bool) string {
193 switch {
194 case year && n <= 0:
195 return T(lang, "heat.none.year")
196 case year && n == 1:
197 return T(lang, "heat.one.year")
198 case year:
199 return T(lang, "heat.many.year", n)
200 case n <= 0:
201 return T(lang, "heat.none.day", date)
202 case n == 1:
203 return T(lang, "heat.one.day", date)
204 default:
205 return T(lang, "heat.many.day", n, date)
206 }
207}