1 package om
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 om 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 monthsAbbreviated []string
26 monthsNarrow []string
27 monthsWide []string
28 daysAbbreviated []string
29 daysNarrow []string
30 daysShort []string
31 daysWide []string
32 periodsAbbreviated []string
33 periodsNarrow []string
34 periodsShort []string
35 periodsWide []string
36 erasAbbreviated []string
37 erasNarrow []string
38 erasWide []string
39 timezones map[string]string
40 }
41
42
43 func New() locales.Translator {
44 return &om{
45 locale: "om",
46 pluralsCardinal: []locales.PluralRule{2, 6},
47 pluralsOrdinal: nil,
48 pluralsRange: nil,
49 decimal: ".",
50 group: ",",
51 minus: "-",
52 percent: "%",
53 perMille: "‰",
54 timeSeparator: ":",
55 inifinity: "∞",
56 currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AUD", "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", "BRL", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CNY", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "ERN", "ESA", "ESB", "ESP", "Br", "EUR", "FIM", "FJD", "FKP", "FRF", "GBP", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "INR", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "KRW", "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", "MXN", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZD", "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", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "USD", "USN", "USS", "UYI", "UYP", "UYU", "UYW", "UZS", "VEB", "VEF", "VES", "VND", "VNN", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "XOF", "XPD", "XPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
57 monthsAbbreviated: []string{"", "Ama", "Gur", "Bit", "Elb", "Cam", "Wax", "Ado", "Hag", "Ful", "Onk", "Sad", "Mud"},
58 monthsNarrow: []string{"", "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
59 monthsWide: []string{"", "Amajjii", "Guraandhala", "Bitooteessa", "Elba", "Caamsa", "Waxabajjii", "Adooleessa", "Hagayya", "Fuulbana", "Onkololeessa", "Sadaasa", "Muddee"},
60 daysAbbreviated: []string{"Dil", "Wix", "Qib", "Rob", "Kam", "Jim", "San"},
61 daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
62 daysShort: []string{"Dil", "Wix", "Qib", "Rob", "Kam", "Jim", "San"},
63 daysWide: []string{"Dilbata", "Wiixata", "Qibxata", "Roobii", "Kamiisa", "Jimaata", "Sanbata"},
64 periodsAbbreviated: []string{"WD", "WB"},
65 periodsNarrow: []string{"WD", "WB"},
66 periodsWide: []string{"WD", "WB"},
67 erasAbbreviated: []string{"BCE", "CE"},
68 erasNarrow: []string{"", ""},
69 erasWide: []string{"", ""},
70 timezones: map[string]string{"ACDT": "ACDT", "ACST": "ACST", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "ADT", "AEDT": "AEDT", "AEST": "AEST", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "AST", "AWDT": "AWDT", "AWST": "AWST", "BOT": "BOT", "BT": "BT", "CAT": "CAT", "CDT": "CDT", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "CST": "CST", "ChST": "ChST", "EAT": "EAT", "ECT": "ECT", "EDT": "EDT", "EST": "EST", "GFT": "GFT", "GMT": "GMT", "GST": "GST", "GYT": "GYT", "HADT": "HADT", "HAST": "HAST", "HAT": "HAT", "HECU": "HECU", "HEEG": "HEEG", "HENOMX": "HENOMX", "HEOG": "HEOG", "HEPM": "HEPM", "HEPMX": "HEPMX", "HKST": "HKST", "HKT": "HKT", "HNCU": "HNCU", "HNEG": "HNEG", "HNNOMX": "HNNOMX", "HNOG": "HNOG", "HNPM": "HNPM", "HNPMX": "HNPMX", "HNT": "HNT", "IST": "IST", "JDT": "JDT", "JST": "JST", "LHDT": "LHDT", "LHST": "LHST", "MDT": "MDT", "MESZ": "MESZ", "MEZ": "MEZ", "MST": "MST", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "OESZ", "OEZ": "OEZ", "PDT": "PDT", "PST": "PST", "SAST": "SAST", "SGT": "SGT", "SRT": "SRT", "TMST": "TMST", "TMT": "TMT", "UYST": "UYST", "UYT": "UYT", "VET": "VET", "WARST": "WARST", "WART": "WART", "WAST": "WAST", "WAT": "WAT", "WESZ": "WESZ", "WEZ": "WEZ", "WIB": "WIB", "WIT": "WIT", "WITA": "WITA", "∅∅∅": "∅∅∅"},
71 }
72 }
73
74
75 func (om *om) Locale() string {
76 return om.locale
77 }
78
79
80 func (om *om) PluralsCardinal() []locales.PluralRule {
81 return om.pluralsCardinal
82 }
83
84
85 func (om *om) PluralsOrdinal() []locales.PluralRule {
86 return om.pluralsOrdinal
87 }
88
89
90 func (om *om) PluralsRange() []locales.PluralRule {
91 return om.pluralsRange
92 }
93
94
95 func (om *om) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
96
97 n := math.Abs(num)
98
99 if n == 1 {
100 return locales.PluralRuleOne
101 }
102
103 return locales.PluralRuleOther
104 }
105
106
107 func (om *om) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
108 return locales.PluralRuleUnknown
109 }
110
111
112 func (om *om) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
113 return locales.PluralRuleUnknown
114 }
115
116
117 func (om *om) MonthAbbreviated(month time.Month) string {
118 return om.monthsAbbreviated[month]
119 }
120
121
122 func (om *om) MonthsAbbreviated() []string {
123 return om.monthsAbbreviated[1:]
124 }
125
126
127 func (om *om) MonthNarrow(month time.Month) string {
128 return om.monthsNarrow[month]
129 }
130
131
132 func (om *om) MonthsNarrow() []string {
133 return om.monthsNarrow[1:]
134 }
135
136
137 func (om *om) MonthWide(month time.Month) string {
138 return om.monthsWide[month]
139 }
140
141
142 func (om *om) MonthsWide() []string {
143 return om.monthsWide[1:]
144 }
145
146
147 func (om *om) WeekdayAbbreviated(weekday time.Weekday) string {
148 return om.daysAbbreviated[weekday]
149 }
150
151
152 func (om *om) WeekdaysAbbreviated() []string {
153 return om.daysAbbreviated
154 }
155
156
157 func (om *om) WeekdayNarrow(weekday time.Weekday) string {
158 return om.daysNarrow[weekday]
159 }
160
161
162 func (om *om) WeekdaysNarrow() []string {
163 return om.daysNarrow
164 }
165
166
167 func (om *om) WeekdayShort(weekday time.Weekday) string {
168 return om.daysShort[weekday]
169 }
170
171
172 func (om *om) WeekdaysShort() []string {
173 return om.daysShort
174 }
175
176
177 func (om *om) WeekdayWide(weekday time.Weekday) string {
178 return om.daysWide[weekday]
179 }
180
181
182 func (om *om) WeekdaysWide() []string {
183 return om.daysWide
184 }
185
186
187 func (om *om) Decimal() string {
188 return om.decimal
189 }
190
191
192 func (om *om) Group() string {
193 return om.group
194 }
195
196
197 func (om *om) Minus() string {
198 return om.minus
199 }
200
201
202 func (om *om) FmtNumber(num float64, v uint64) string {
203
204 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
205 l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
206 count := 0
207 inWhole := v == 0
208 b := make([]byte, 0, l)
209
210 for i := len(s) - 1; i >= 0; i-- {
211
212 if s[i] == '.' {
213 b = append(b, om.decimal[0])
214 inWhole = true
215 continue
216 }
217
218 if inWhole {
219 if count == 3 {
220 b = append(b, om.group[0])
221 count = 1
222 } else {
223 count++
224 }
225 }
226
227 b = append(b, s[i])
228 }
229
230 if num < 0 {
231 b = append(b, om.minus[0])
232 }
233
234
235 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
236 b[i], b[j] = b[j], b[i]
237 }
238
239 return string(b)
240 }
241
242
243
244 func (om *om) FmtPercent(num float64, v uint64) string {
245 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
246 l := len(s) + 3
247 b := make([]byte, 0, l)
248
249 for i := len(s) - 1; i >= 0; i-- {
250
251 if s[i] == '.' {
252 b = append(b, om.decimal[0])
253 continue
254 }
255
256 b = append(b, s[i])
257 }
258
259 if num < 0 {
260 b = append(b, om.minus[0])
261 }
262
263
264 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
265 b[i], b[j] = b[j], b[i]
266 }
267
268 b = append(b, om.percent...)
269
270 return string(b)
271 }
272
273
274 func (om *om) FmtCurrency(num float64, v uint64, currency currency.Type) string {
275
276 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
277 symbol := om.currencies[currency]
278 l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
279 count := 0
280 inWhole := v == 0
281 b := make([]byte, 0, l)
282
283 for i := len(s) - 1; i >= 0; i-- {
284
285 if s[i] == '.' {
286 b = append(b, om.decimal[0])
287 inWhole = true
288 continue
289 }
290
291 if inWhole {
292 if count == 3 {
293 b = append(b, om.group[0])
294 count = 1
295 } else {
296 count++
297 }
298 }
299
300 b = append(b, s[i])
301 }
302
303 for j := len(symbol) - 1; j >= 0; j-- {
304 b = append(b, symbol[j])
305 }
306
307 if num < 0 {
308 b = append(b, om.minus[0])
309 }
310
311
312 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
313 b[i], b[j] = b[j], b[i]
314 }
315
316 if int(v) < 2 {
317
318 if v == 0 {
319 b = append(b, om.decimal...)
320 }
321
322 for i := 0; i < 2-int(v); i++ {
323 b = append(b, '0')
324 }
325 }
326
327 return string(b)
328 }
329
330
331
332 func (om *om) FmtAccounting(num float64, v uint64, currency currency.Type) string {
333
334 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
335 symbol := om.currencies[currency]
336 l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
337 count := 0
338 inWhole := v == 0
339 b := make([]byte, 0, l)
340
341 for i := len(s) - 1; i >= 0; i-- {
342
343 if s[i] == '.' {
344 b = append(b, om.decimal[0])
345 inWhole = true
346 continue
347 }
348
349 if inWhole {
350 if count == 3 {
351 b = append(b, om.group[0])
352 count = 1
353 } else {
354 count++
355 }
356 }
357
358 b = append(b, s[i])
359 }
360
361 if num < 0 {
362
363 for j := len(symbol) - 1; j >= 0; j-- {
364 b = append(b, symbol[j])
365 }
366
367 b = append(b, om.minus[0])
368
369 } else {
370
371 for j := len(symbol) - 1; j >= 0; j-- {
372 b = append(b, symbol[j])
373 }
374
375 }
376
377
378 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
379 b[i], b[j] = b[j], b[i]
380 }
381
382 if int(v) < 2 {
383
384 if v == 0 {
385 b = append(b, om.decimal...)
386 }
387
388 for i := 0; i < 2-int(v); i++ {
389 b = append(b, '0')
390 }
391 }
392
393 return string(b)
394 }
395
396
397 func (om *om) FmtDateShort(t time.Time) string {
398
399 b := make([]byte, 0, 32)
400
401 if t.Day() < 10 {
402 b = append(b, '0')
403 }
404
405 b = strconv.AppendInt(b, int64(t.Day()), 10)
406 b = append(b, []byte{0x2f}...)
407
408 if t.Month() < 10 {
409 b = append(b, '0')
410 }
411
412 b = strconv.AppendInt(b, int64(t.Month()), 10)
413
414 b = append(b, []byte{0x2f}...)
415
416 if t.Year() > 9 {
417 b = append(b, strconv.Itoa(t.Year())[2:]...)
418 } else {
419 b = append(b, strconv.Itoa(t.Year())[1:]...)
420 }
421
422 return string(b)
423 }
424
425
426 func (om *om) FmtDateMedium(t time.Time) string {
427
428 b := make([]byte, 0, 32)
429
430 if t.Day() < 10 {
431 b = append(b, '0')
432 }
433
434 b = strconv.AppendInt(b, int64(t.Day()), 10)
435 b = append(b, []byte{0x2d}...)
436 b = append(b, om.monthsAbbreviated[t.Month()]...)
437 b = append(b, []byte{0x2d}...)
438
439 if t.Year() > 0 {
440 b = strconv.AppendInt(b, int64(t.Year()), 10)
441 } else {
442 b = strconv.AppendInt(b, int64(-t.Year()), 10)
443 }
444
445 return string(b)
446 }
447
448
449 func (om *om) FmtDateLong(t time.Time) string {
450
451 b := make([]byte, 0, 32)
452
453 if t.Day() < 10 {
454 b = append(b, '0')
455 }
456
457 b = strconv.AppendInt(b, int64(t.Day()), 10)
458 b = append(b, []byte{0x20}...)
459 b = append(b, om.monthsWide[t.Month()]...)
460 b = append(b, []byte{0x20}...)
461
462 if t.Year() > 0 {
463 b = strconv.AppendInt(b, int64(t.Year()), 10)
464 } else {
465 b = strconv.AppendInt(b, int64(-t.Year()), 10)
466 }
467
468 return string(b)
469 }
470
471
472 func (om *om) FmtDateFull(t time.Time) string {
473
474 b := make([]byte, 0, 32)
475
476 b = append(b, om.daysWide[t.Weekday()]...)
477 b = append(b, []byte{0x2c, 0x20}...)
478 b = append(b, om.monthsWide[t.Month()]...)
479 b = append(b, []byte{0x20}...)
480 b = strconv.AppendInt(b, int64(t.Day()), 10)
481 b = append(b, []byte{0x2c, 0x20}...)
482
483 if t.Year() > 0 {
484 b = strconv.AppendInt(b, int64(t.Year()), 10)
485 } else {
486 b = strconv.AppendInt(b, int64(-t.Year()), 10)
487 }
488
489 return string(b)
490 }
491
492
493 func (om *om) FmtTimeShort(t time.Time) string {
494
495 b := make([]byte, 0, 32)
496
497 h := t.Hour()
498
499 if h > 12 {
500 h -= 12
501 }
502
503 b = strconv.AppendInt(b, int64(h), 10)
504 b = append(b, om.timeSeparator...)
505
506 if t.Minute() < 10 {
507 b = append(b, '0')
508 }
509
510 b = strconv.AppendInt(b, int64(t.Minute()), 10)
511 b = append(b, []byte{0x20}...)
512
513 if t.Hour() < 12 {
514 b = append(b, om.periodsAbbreviated[0]...)
515 } else {
516 b = append(b, om.periodsAbbreviated[1]...)
517 }
518
519 return string(b)
520 }
521
522
523 func (om *om) FmtTimeMedium(t time.Time) string {
524
525 b := make([]byte, 0, 32)
526
527 h := t.Hour()
528
529 if h > 12 {
530 h -= 12
531 }
532
533 b = strconv.AppendInt(b, int64(h), 10)
534 b = append(b, om.timeSeparator...)
535
536 if t.Minute() < 10 {
537 b = append(b, '0')
538 }
539
540 b = strconv.AppendInt(b, int64(t.Minute()), 10)
541 b = append(b, om.timeSeparator...)
542
543 if t.Second() < 10 {
544 b = append(b, '0')
545 }
546
547 b = strconv.AppendInt(b, int64(t.Second()), 10)
548 b = append(b, []byte{0x20}...)
549
550 if t.Hour() < 12 {
551 b = append(b, om.periodsAbbreviated[0]...)
552 } else {
553 b = append(b, om.periodsAbbreviated[1]...)
554 }
555
556 return string(b)
557 }
558
559
560 func (om *om) FmtTimeLong(t time.Time) string {
561
562 b := make([]byte, 0, 32)
563
564 h := t.Hour()
565
566 if h > 12 {
567 h -= 12
568 }
569
570 b = strconv.AppendInt(b, int64(h), 10)
571 b = append(b, om.timeSeparator...)
572
573 if t.Minute() < 10 {
574 b = append(b, '0')
575 }
576
577 b = strconv.AppendInt(b, int64(t.Minute()), 10)
578 b = append(b, om.timeSeparator...)
579
580 if t.Second() < 10 {
581 b = append(b, '0')
582 }
583
584 b = strconv.AppendInt(b, int64(t.Second()), 10)
585 b = append(b, []byte{0x20}...)
586
587 if t.Hour() < 12 {
588 b = append(b, om.periodsAbbreviated[0]...)
589 } else {
590 b = append(b, om.periodsAbbreviated[1]...)
591 }
592
593 b = append(b, []byte{0x20}...)
594
595 tz, _ := t.Zone()
596 b = append(b, tz...)
597
598 return string(b)
599 }
600
601
602 func (om *om) FmtTimeFull(t time.Time) string {
603
604 b := make([]byte, 0, 32)
605
606 h := t.Hour()
607
608 if h > 12 {
609 h -= 12
610 }
611
612 b = strconv.AppendInt(b, int64(h), 10)
613 b = append(b, om.timeSeparator...)
614
615 if t.Minute() < 10 {
616 b = append(b, '0')
617 }
618
619 b = strconv.AppendInt(b, int64(t.Minute()), 10)
620 b = append(b, om.timeSeparator...)
621
622 if t.Second() < 10 {
623 b = append(b, '0')
624 }
625
626 b = strconv.AppendInt(b, int64(t.Second()), 10)
627 b = append(b, []byte{0x20}...)
628
629 if t.Hour() < 12 {
630 b = append(b, om.periodsAbbreviated[0]...)
631 } else {
632 b = append(b, om.periodsAbbreviated[1]...)
633 }
634
635 b = append(b, []byte{0x20}...)
636
637 tz, _ := t.Zone()
638
639 if btz, ok := om.timezones[tz]; ok {
640 b = append(b, btz...)
641 } else {
642 b = append(b, tz...)
643 }
644
645 return string(b)
646 }
647
View as plain text