1 package et
2
3 import (
4 "math"
5 "strconv"
6 "time"
7
8 "github.com/go-playground/locales"
9 "github.com/go-playground/locales/currency"
10 )
11
12 type et struct {
13 locale string
14 pluralsCardinal []locales.PluralRule
15 pluralsOrdinal []locales.PluralRule
16 pluralsRange []locales.PluralRule
17 decimal string
18 group string
19 minus string
20 percent string
21 perMille string
22 timeSeparator string
23 inifinity string
24 currencies []string
25 currencyPositiveSuffix string
26 currencyNegativePrefix string
27 currencyNegativeSuffix string
28 monthsAbbreviated []string
29 monthsNarrow []string
30 monthsWide []string
31 daysAbbreviated []string
32 daysNarrow []string
33 daysShort []string
34 daysWide []string
35 periodsAbbreviated []string
36 periodsNarrow []string
37 periodsShort []string
38 periodsWide []string
39 erasAbbreviated []string
40 erasNarrow []string
41 erasWide []string
42 timezones map[string]string
43 }
44
45
46 func New() locales.Translator {
47 return &et{
48 locale: "et",
49 pluralsCardinal: []locales.PluralRule{2, 6},
50 pluralsOrdinal: []locales.PluralRule{6},
51 pluralsRange: []locales.PluralRule{6},
52 decimal: ",",
53 group: " ",
54 minus: "−",
55 percent: "%",
56 perMille: "‰",
57 timeSeparator: ":",
58 inifinity: "∞",
59 currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AU$", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CN¥", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "kr", "EGP", "ERN", "ESA", "ESB", "ESP", "ETB", "€", "FIM", "FJD", "FKP", "FRF", "£", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HK$", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "₪", "₹", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "₩", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MRU", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MX$", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZ$", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "STN", "SUR", "SVC", "SYP", "SZL", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "USN", "USS", "UYI", "UYP", "UYU", "UYW", "UZS", "VEB", "VEF", "VES", "₫", "VNN", "VUV", "WST", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "EC$", "XDR", "XEU", "XFO", "XFU", "CFA", "XPD", "CFPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
60 currencyPositiveSuffix: " ",
61 currencyNegativePrefix: "(",
62 currencyNegativeSuffix: " )",
63 monthsAbbreviated: []string{"", "jaan", "veebr", "märts", "apr", "mai", "juuni", "juuli", "aug", "sept", "okt", "nov", "dets"},
64 monthsNarrow: []string{"", "J", "V", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
65 monthsWide: []string{"", "jaanuar", "veebruar", "märts", "aprill", "mai", "juuni", "juuli", "august", "september", "oktoober", "november", "detsember"},
66 daysAbbreviated: []string{"P", "E", "T", "K", "N", "R", "L"},
67 daysNarrow: []string{"P", "E", "T", "K", "N", "R", "L"},
68 daysShort: []string{"P", "E", "T", "K", "N", "R", "L"},
69 daysWide: []string{"pühapäev", "esmaspäev", "teisipäev", "kolmapäev", "neljapäev", "reede", "laupäev"},
70 periodsAbbreviated: []string{"AM", "PM"},
71 periodsNarrow: []string{"AM", "PM"},
72 periodsWide: []string{"AM", "PM"},
73 erasAbbreviated: []string{"eKr", "pKr"},
74 erasNarrow: []string{"eKr", "pKr"},
75 erasWide: []string{"enne Kristust", "pärast Kristust"},
76 timezones: map[string]string{"ACDT": "Kesk-Austraalia suveaeg", "ACST": "Kesk-Austraalia standardaeg", "ACWDT": "Austraalia Kesk-Lääne suveaeg", "ACWST": "Austraalia Kesk-Lääne standardaeg", "ADT": "Atlandi suveaeg", "AEDT": "Ida-Austraalia suveaeg", "AEST": "Ida-Austraalia standardaeg", "AKDT": "Alaska suveaeg", "AKST": "Alaska standardaeg", "ARST": "Argentina suveaeg", "ART": "Argentina standardaeg", "AST": "Atlandi standardaeg", "AWDT": "Lääne-Austraalia suveaeg", "AWST": "Lääne-Austraalia standardaeg", "BOT": "Boliivia aeg", "BT": "Bhutani aeg", "CAT": "Kesk-Aafrika aeg", "CDT": "Kesk-Ameerika suveaeg", "CHADT": "Chathami suveaeg", "CHAST": "Chathami standardaeg", "CLST": "Tšiili suveaeg", "CLT": "Tšiili standardaeg", "COST": "Colombia suveaeg", "COT": "Colombia standardaeg", "CST": "Kesk-Ameerika standardaeg", "ChST": "Tšamorro standardaeg", "EAT": "Ida-Aafrika aeg", "ECT": "Ecuadori aeg", "EDT": "Idaranniku suveaeg", "EST": "Idaranniku standardaeg", "GFT": "Prantsuse Guajaana aeg", "GMT": "Greenwichi aeg", "GST": "Pärsia lahe standardaeg", "GYT": "Guyana aeg", "HADT": "Hawaii-Aleuudi suveaeg", "HAST": "Hawaii-Aleuudi standardaeg", "HAT": "Newfoundlandi suveaeg", "HECU": "Kuuba suveaeg", "HEEG": "Ida-Gröönimaa suveaeg", "HENOMX": "Loode-Mehhiko suveaeg", "HEOG": "Lääne-Gröönimaa suveaeg", "HEPM": "Saint-Pierre’i ja Miqueloni suveaeg", "HEPMX": "Mehhiko Vaikse ookeani suveaeg", "HKST": "Hongkongi suveaeg", "HKT": "Hongkongi standardaeg", "HNCU": "Kuuba standardaeg", "HNEG": "Ida-Gröönimaa standardaeg", "HNNOMX": "Loode-Mehhiko standardaeg", "HNOG": "Lääne-Gröönimaa standardaeg", "HNPM": "Saint-Pierre’i ja Miqueloni standardaeg", "HNPMX": "Mehhiko Vaikse ookeani standardaeg", "HNT": "Newfoundlandi standardaeg", "IST": "India aeg", "JDT": "Jaapani suveaeg", "JST": "Jaapani standardaeg", "LHDT": "Lord Howe’i suveaeg", "LHST": "Lord Howe’i standardaeg", "MDT": "Mäestikuvööndi suveaeg", "MESZ": "Kesk-Euroopa suveaeg", "MEZ": "Kesk-Euroopa standardaeg", "MST": "Mäestikuvööndi standardaeg", "MYT": "Malaisia aeg", "NZDT": "Uus-Meremaa suveaeg", "NZST": "Uus-Meremaa standardaeg", "OESZ": "Ida-Euroopa suveaeg", "OEZ": "Ida-Euroopa standardaeg", "PDT": "Vaikse ookeani suveaeg", "PST": "Vaikse ookeani standardaeg", "SAST": "Lõuna-Aafrika standardaeg", "SGT": "Singapuri standardaeg", "SRT": "Suriname aeg", "TMST": "Türkmenistani suveaeg", "TMT": "Türkmenistani standardaeg", "UYST": "Uruguay suveaeg", "UYT": "Uruguay standardaeg", "VET": "Venezuela aeg", "WARST": "Lääne-Argentina suveaeg", "WART": "Lääne-Argentina standardaeg", "WAST": "Lääne-Aafrika suveaeg", "WAT": "Lääne-Aafrika standardaeg", "WESZ": "Lääne-Euroopa suveaeg", "WEZ": "Lääne-Euroopa standardaeg", "WIB": "Lääne-Indoneesia aeg", "WIT": "Ida-Indoneesia aeg", "WITA": "Kesk-Indoneesia aeg", "∅∅∅": "Peruu suveaeg"},
77 }
78 }
79
80
81 func (et *et) Locale() string {
82 return et.locale
83 }
84
85
86 func (et *et) PluralsCardinal() []locales.PluralRule {
87 return et.pluralsCardinal
88 }
89
90
91 func (et *et) PluralsOrdinal() []locales.PluralRule {
92 return et.pluralsOrdinal
93 }
94
95
96 func (et *et) PluralsRange() []locales.PluralRule {
97 return et.pluralsRange
98 }
99
100
101 func (et *et) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
102
103 n := math.Abs(num)
104 i := int64(n)
105
106 if i == 1 && v == 0 {
107 return locales.PluralRuleOne
108 }
109
110 return locales.PluralRuleOther
111 }
112
113
114 func (et *et) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
115 return locales.PluralRuleOther
116 }
117
118
119 func (et *et) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
120 return locales.PluralRuleOther
121 }
122
123
124 func (et *et) MonthAbbreviated(month time.Month) string {
125 return et.monthsAbbreviated[month]
126 }
127
128
129 func (et *et) MonthsAbbreviated() []string {
130 return et.monthsAbbreviated[1:]
131 }
132
133
134 func (et *et) MonthNarrow(month time.Month) string {
135 return et.monthsNarrow[month]
136 }
137
138
139 func (et *et) MonthsNarrow() []string {
140 return et.monthsNarrow[1:]
141 }
142
143
144 func (et *et) MonthWide(month time.Month) string {
145 return et.monthsWide[month]
146 }
147
148
149 func (et *et) MonthsWide() []string {
150 return et.monthsWide[1:]
151 }
152
153
154 func (et *et) WeekdayAbbreviated(weekday time.Weekday) string {
155 return et.daysAbbreviated[weekday]
156 }
157
158
159 func (et *et) WeekdaysAbbreviated() []string {
160 return et.daysAbbreviated
161 }
162
163
164 func (et *et) WeekdayNarrow(weekday time.Weekday) string {
165 return et.daysNarrow[weekday]
166 }
167
168
169 func (et *et) WeekdaysNarrow() []string {
170 return et.daysNarrow
171 }
172
173
174 func (et *et) WeekdayShort(weekday time.Weekday) string {
175 return et.daysShort[weekday]
176 }
177
178
179 func (et *et) WeekdaysShort() []string {
180 return et.daysShort
181 }
182
183
184 func (et *et) WeekdayWide(weekday time.Weekday) string {
185 return et.daysWide[weekday]
186 }
187
188
189 func (et *et) WeekdaysWide() []string {
190 return et.daysWide
191 }
192
193
194 func (et *et) Decimal() string {
195 return et.decimal
196 }
197
198
199 func (et *et) Group() string {
200 return et.group
201 }
202
203
204 func (et *et) Minus() string {
205 return et.minus
206 }
207
208
209 func (et *et) FmtNumber(num float64, v uint64) string {
210
211 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
212 l := len(s) + 4 + 2*len(s[:len(s)-int(v)-1])/3
213 count := 0
214 inWhole := v == 0
215 b := make([]byte, 0, l)
216
217 for i := len(s) - 1; i >= 0; i-- {
218
219 if s[i] == '.' {
220 b = append(b, et.decimal[0])
221 inWhole = true
222 continue
223 }
224
225 if inWhole {
226 if count == 3 {
227 for j := len(et.group) - 1; j >= 0; j-- {
228 b = append(b, et.group[j])
229 }
230 count = 1
231 } else {
232 count++
233 }
234 }
235
236 b = append(b, s[i])
237 }
238
239 if num < 0 {
240 for j := len(et.minus) - 1; j >= 0; j-- {
241 b = append(b, et.minus[j])
242 }
243 }
244
245
246 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
247 b[i], b[j] = b[j], b[i]
248 }
249
250 return string(b)
251 }
252
253
254
255 func (et *et) FmtPercent(num float64, v uint64) string {
256 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
257 l := len(s) + 5
258 b := make([]byte, 0, l)
259
260 for i := len(s) - 1; i >= 0; i-- {
261
262 if s[i] == '.' {
263 b = append(b, et.decimal[0])
264 continue
265 }
266
267 b = append(b, s[i])
268 }
269
270 if num < 0 {
271 for j := len(et.minus) - 1; j >= 0; j-- {
272 b = append(b, et.minus[j])
273 }
274 }
275
276
277 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
278 b[i], b[j] = b[j], b[i]
279 }
280
281 b = append(b, et.percent...)
282
283 return string(b)
284 }
285
286
287 func (et *et) FmtCurrency(num float64, v uint64, currency currency.Type) string {
288
289 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
290 symbol := et.currencies[currency]
291 l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
292 count := 0
293 inWhole := v == 0
294 b := make([]byte, 0, l)
295
296 for i := len(s) - 1; i >= 0; i-- {
297
298 if s[i] == '.' {
299 b = append(b, et.decimal[0])
300 inWhole = true
301 continue
302 }
303
304 if inWhole {
305 if count == 3 {
306 for j := len(et.group) - 1; j >= 0; j-- {
307 b = append(b, et.group[j])
308 }
309 count = 1
310 } else {
311 count++
312 }
313 }
314
315 b = append(b, s[i])
316 }
317
318 if num < 0 {
319 for j := len(et.minus) - 1; j >= 0; j-- {
320 b = append(b, et.minus[j])
321 }
322 }
323
324
325 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
326 b[i], b[j] = b[j], b[i]
327 }
328
329 if int(v) < 2 {
330
331 if v == 0 {
332 b = append(b, et.decimal...)
333 }
334
335 for i := 0; i < 2-int(v); i++ {
336 b = append(b, '0')
337 }
338 }
339
340 b = append(b, et.currencyPositiveSuffix...)
341
342 b = append(b, symbol...)
343
344 return string(b)
345 }
346
347
348
349 func (et *et) FmtAccounting(num float64, v uint64, currency currency.Type) string {
350
351 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
352 symbol := et.currencies[currency]
353 l := len(s) + len(symbol) + 8 + 2*len(s[:len(s)-int(v)-1])/3
354 count := 0
355 inWhole := v == 0
356 b := make([]byte, 0, l)
357
358 for i := len(s) - 1; i >= 0; i-- {
359
360 if s[i] == '.' {
361 b = append(b, et.decimal[0])
362 inWhole = true
363 continue
364 }
365
366 if inWhole {
367 if count == 3 {
368 for j := len(et.group) - 1; j >= 0; j-- {
369 b = append(b, et.group[j])
370 }
371 count = 1
372 } else {
373 count++
374 }
375 }
376
377 b = append(b, s[i])
378 }
379
380 if num < 0 {
381
382 b = append(b, et.currencyNegativePrefix[0])
383
384 }
385
386
387 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
388 b[i], b[j] = b[j], b[i]
389 }
390
391 if int(v) < 2 {
392
393 if v == 0 {
394 b = append(b, et.decimal...)
395 }
396
397 for i := 0; i < 2-int(v); i++ {
398 b = append(b, '0')
399 }
400 }
401
402 if num < 0 {
403 b = append(b, et.currencyNegativeSuffix...)
404 b = append(b, symbol...)
405 } else {
406
407 b = append(b, et.currencyPositiveSuffix...)
408 b = append(b, symbol...)
409 }
410
411 return string(b)
412 }
413
414
415 func (et *et) FmtDateShort(t time.Time) string {
416
417 b := make([]byte, 0, 32)
418
419 if t.Day() < 10 {
420 b = append(b, '0')
421 }
422
423 b = strconv.AppendInt(b, int64(t.Day()), 10)
424 b = append(b, []byte{0x2e}...)
425
426 if t.Month() < 10 {
427 b = append(b, '0')
428 }
429
430 b = strconv.AppendInt(b, int64(t.Month()), 10)
431
432 b = append(b, []byte{0x2e}...)
433
434 if t.Year() > 9 {
435 b = append(b, strconv.Itoa(t.Year())[2:]...)
436 } else {
437 b = append(b, strconv.Itoa(t.Year())[1:]...)
438 }
439
440 return string(b)
441 }
442
443
444 func (et *et) FmtDateMedium(t time.Time) string {
445
446 b := make([]byte, 0, 32)
447
448 b = strconv.AppendInt(b, int64(t.Day()), 10)
449 b = append(b, []byte{0x2e, 0x20}...)
450 b = append(b, et.monthsAbbreviated[t.Month()]...)
451 b = append(b, []byte{0x20}...)
452
453 if t.Year() > 0 {
454 b = strconv.AppendInt(b, int64(t.Year()), 10)
455 } else {
456 b = strconv.AppendInt(b, int64(-t.Year()), 10)
457 }
458
459 return string(b)
460 }
461
462
463 func (et *et) FmtDateLong(t time.Time) string {
464
465 b := make([]byte, 0, 32)
466
467 b = strconv.AppendInt(b, int64(t.Day()), 10)
468 b = append(b, []byte{0x2e, 0x20}...)
469 b = append(b, et.monthsWide[t.Month()]...)
470 b = append(b, []byte{0x20}...)
471
472 if t.Year() > 0 {
473 b = strconv.AppendInt(b, int64(t.Year()), 10)
474 } else {
475 b = strconv.AppendInt(b, int64(-t.Year()), 10)
476 }
477
478 return string(b)
479 }
480
481
482 func (et *et) FmtDateFull(t time.Time) string {
483
484 b := make([]byte, 0, 32)
485
486 b = append(b, et.daysWide[t.Weekday()]...)
487 b = append(b, []byte{0x2c, 0x20}...)
488 b = strconv.AppendInt(b, int64(t.Day()), 10)
489 b = append(b, []byte{0x2e, 0x20}...)
490 b = append(b, et.monthsWide[t.Month()]...)
491 b = append(b, []byte{0x20}...)
492
493 if t.Year() > 0 {
494 b = strconv.AppendInt(b, int64(t.Year()), 10)
495 } else {
496 b = strconv.AppendInt(b, int64(-t.Year()), 10)
497 }
498
499 return string(b)
500 }
501
502
503 func (et *et) FmtTimeShort(t time.Time) string {
504
505 b := make([]byte, 0, 32)
506
507 if t.Hour() < 10 {
508 b = append(b, '0')
509 }
510
511 b = strconv.AppendInt(b, int64(t.Hour()), 10)
512 b = append(b, et.timeSeparator...)
513
514 if t.Minute() < 10 {
515 b = append(b, '0')
516 }
517
518 b = strconv.AppendInt(b, int64(t.Minute()), 10)
519
520 return string(b)
521 }
522
523
524 func (et *et) FmtTimeMedium(t time.Time) string {
525
526 b := make([]byte, 0, 32)
527
528 if t.Hour() < 10 {
529 b = append(b, '0')
530 }
531
532 b = strconv.AppendInt(b, int64(t.Hour()), 10)
533 b = append(b, et.timeSeparator...)
534
535 if t.Minute() < 10 {
536 b = append(b, '0')
537 }
538
539 b = strconv.AppendInt(b, int64(t.Minute()), 10)
540 b = append(b, et.timeSeparator...)
541
542 if t.Second() < 10 {
543 b = append(b, '0')
544 }
545
546 b = strconv.AppendInt(b, int64(t.Second()), 10)
547
548 return string(b)
549 }
550
551
552 func (et *et) FmtTimeLong(t time.Time) string {
553
554 b := make([]byte, 0, 32)
555
556 if t.Hour() < 10 {
557 b = append(b, '0')
558 }
559
560 b = strconv.AppendInt(b, int64(t.Hour()), 10)
561 b = append(b, et.timeSeparator...)
562
563 if t.Minute() < 10 {
564 b = append(b, '0')
565 }
566
567 b = strconv.AppendInt(b, int64(t.Minute()), 10)
568 b = append(b, et.timeSeparator...)
569
570 if t.Second() < 10 {
571 b = append(b, '0')
572 }
573
574 b = strconv.AppendInt(b, int64(t.Second()), 10)
575 b = append(b, []byte{0x20}...)
576
577 tz, _ := t.Zone()
578 b = append(b, tz...)
579
580 return string(b)
581 }
582
583
584 func (et *et) FmtTimeFull(t time.Time) string {
585
586 b := make([]byte, 0, 32)
587
588 if t.Hour() < 10 {
589 b = append(b, '0')
590 }
591
592 b = strconv.AppendInt(b, int64(t.Hour()), 10)
593 b = append(b, et.timeSeparator...)
594
595 if t.Minute() < 10 {
596 b = append(b, '0')
597 }
598
599 b = strconv.AppendInt(b, int64(t.Minute()), 10)
600 b = append(b, et.timeSeparator...)
601
602 if t.Second() < 10 {
603 b = append(b, '0')
604 }
605
606 b = strconv.AppendInt(b, int64(t.Second()), 10)
607 b = append(b, []byte{0x20}...)
608
609 tz, _ := t.Zone()
610
611 if btz, ok := et.timezones[tz]; ok {
612 b = append(b, btz...)
613 } else {
614 b = append(b, tz...)
615 }
616
617 return string(b)
618 }
619
View as plain text