| 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. |
| 5 | package i18n |
| 6 | |
| 7 | import ( |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "strings" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // Lang is a choice stored on the user. |
| 15 | type Lang string |
| 16 | |
| 17 | const ( |
| 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. |
| 26 | func 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. |
| 30 | func (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. |
| 46 | func 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. |
| 56 | type Error struct { |
| 57 | Key string |
| 58 | Args []any |
| 59 | } |
| 60 | |
| 61 | func (e Error) Error() string { return T(EN, e.Key, e.Args...) } |
| 62 | |
| 63 | // E builds a translatable error. |
| 64 | func 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. |
| 67 | func 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. |
| 82 | func 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 | |
| 97 | func 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. |
| 110 | func 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". |
| 119 | func 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. |
| 124 | func 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 | // ShortAgo is the compact relative time used in tables, such as "3d". |
| 193 | func ShortAgo(lang Lang, unix int64) string { |
| 194 | if unix <= 0 { |
| 195 | return "" |
| 196 | } |
| 197 | d := time.Since(time.Unix(unix, 0)) |
| 198 | if d < time.Minute { |
| 199 | return T(lang, "ago.short.just") |
| 200 | } |
| 201 | if d < time.Hour { |
| 202 | n := int(d.Minutes()) |
| 203 | if n < 1 { |
| 204 | n = 1 |
| 205 | } |
| 206 | if n == 1 { |
| 207 | return T(lang, "ago.short.minute") |
| 208 | } |
| 209 | return T(lang, "ago.short.minutes", n) |
| 210 | } |
| 211 | if d < 24*time.Hour { |
| 212 | n := int(d.Hours()) |
| 213 | if n < 1 { |
| 214 | n = 1 |
| 215 | } |
| 216 | if n == 1 { |
| 217 | return T(lang, "ago.short.hour") |
| 218 | } |
| 219 | return T(lang, "ago.short.hours", n) |
| 220 | } |
| 221 | days := int(d.Hours() / 24) |
| 222 | if days < 1 { |
| 223 | days = 1 |
| 224 | } |
| 225 | if days == 1 { |
| 226 | return T(lang, "ago.short.day") |
| 227 | } |
| 228 | if days < 14 { |
| 229 | return T(lang, "ago.short.days", days) |
| 230 | } |
| 231 | if days < 60 { |
| 232 | n := days / 7 |
| 233 | if n < 1 { |
| 234 | n = 1 |
| 235 | } |
| 236 | if n == 1 { |
| 237 | return T(lang, "ago.short.week") |
| 238 | } |
| 239 | return T(lang, "ago.short.weeks", n) |
| 240 | } |
| 241 | if days < 365 { |
| 242 | n := days / 30 |
| 243 | if n < 1 { |
| 244 | n = 1 |
| 245 | } |
| 246 | if n == 1 { |
| 247 | return T(lang, "ago.short.month") |
| 248 | } |
| 249 | return T(lang, "ago.short.months", n) |
| 250 | } |
| 251 | n := days / 365 |
| 252 | if n < 1 { |
| 253 | n = 1 |
| 254 | } |
| 255 | if n == 1 { |
| 256 | return T(lang, "ago.short.year") |
| 257 | } |
| 258 | return T(lang, "ago.short.years", n) |
| 259 | } |
| 260 | |
| 261 | // Contrib is the heatmap sentence. year is the twelve-month total; |
| 262 | // otherwise date is the long date of one cell. |
| 263 | func Contrib(lang Lang, n int, date string, year bool) string { |
| 264 | switch { |
| 265 | case year && n <= 0: |
| 266 | return T(lang, "heat.none.year") |
| 267 | case year && n == 1: |
| 268 | return T(lang, "heat.one.year") |
| 269 | case year: |
| 270 | return T(lang, "heat.many.year", n) |
| 271 | case n <= 0: |
| 272 | return T(lang, "heat.none.day", date) |
| 273 | case n == 1: |
| 274 | return T(lang, "heat.one.day", date) |
| 275 | default: |
| 276 | return T(lang, "heat.many.day", n, date) |
| 277 | } |
| 278 | } |