1 package sbp
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 sbp 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 &sbp{
45 locale: "sbp",
46 pluralsCardinal: nil,
47 pluralsOrdinal: nil,
48 pluralsRange: nil,
49 decimal: ".",
50 group: ",",
51 timeSeparator: ":",
52 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", "ETB", "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", "TSh", "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"},
53 monthsAbbreviated: []string{"", "Mup", "Mwi", "Msh", "Mun", "Mag", "Muj", "Msp", "Mpg", "Mye", "Mok", "Mus", "Muh"},
54 monthsWide: []string{"", "Mupalangulwa", "Mwitope", "Mushende", "Munyi", "Mushende Magali", "Mujimbi", "Mushipepo", "Mupuguto", "Munyense", "Mokhu", "Musongandembwe", "Muhaano"},
55 daysAbbreviated: []string{"Mul", "Jtt", "Jnn", "Jtn", "Alh", "Iju", "Jmo"},
56 daysNarrow: []string{"M", "J", "J", "J", "A", "I", "J"},
57 daysWide: []string{"Mulungu", "Jumatatu", "Jumanne", "Jumatano", "Alahamisi", "Ijumaa", "Jumamosi"},
58 periodsAbbreviated: []string{"Lwamilawu", "Pashamihe"},
59 periodsWide: []string{"Lwamilawu", "Pashamihe"},
60 erasAbbreviated: []string{"AK", "PK"},
61 erasNarrow: []string{"", ""},
62 erasWide: []string{"Ashanali uKilisito", "Pamwandi ya Kilisto"},
63 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", "∅∅∅": "∅∅∅"},
64 }
65 }
66
67
68 func (sbp *sbp) Locale() string {
69 return sbp.locale
70 }
71
72
73 func (sbp *sbp) PluralsCardinal() []locales.PluralRule {
74 return sbp.pluralsCardinal
75 }
76
77
78 func (sbp *sbp) PluralsOrdinal() []locales.PluralRule {
79 return sbp.pluralsOrdinal
80 }
81
82
83 func (sbp *sbp) PluralsRange() []locales.PluralRule {
84 return sbp.pluralsRange
85 }
86
87
88 func (sbp *sbp) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
89 return locales.PluralRuleUnknown
90 }
91
92
93 func (sbp *sbp) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
94 return locales.PluralRuleUnknown
95 }
96
97
98 func (sbp *sbp) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
99 return locales.PluralRuleUnknown
100 }
101
102
103 func (sbp *sbp) MonthAbbreviated(month time.Month) string {
104 return sbp.monthsAbbreviated[month]
105 }
106
107
108 func (sbp *sbp) MonthsAbbreviated() []string {
109 return sbp.monthsAbbreviated[1:]
110 }
111
112
113 func (sbp *sbp) MonthNarrow(month time.Month) string {
114 return sbp.monthsNarrow[month]
115 }
116
117
118 func (sbp *sbp) MonthsNarrow() []string {
119 return nil
120 }
121
122
123 func (sbp *sbp) MonthWide(month time.Month) string {
124 return sbp.monthsWide[month]
125 }
126
127
128 func (sbp *sbp) MonthsWide() []string {
129 return sbp.monthsWide[1:]
130 }
131
132
133 func (sbp *sbp) WeekdayAbbreviated(weekday time.Weekday) string {
134 return sbp.daysAbbreviated[weekday]
135 }
136
137
138 func (sbp *sbp) WeekdaysAbbreviated() []string {
139 return sbp.daysAbbreviated
140 }
141
142
143 func (sbp *sbp) WeekdayNarrow(weekday time.Weekday) string {
144 return sbp.daysNarrow[weekday]
145 }
146
147
148 func (sbp *sbp) WeekdaysNarrow() []string {
149 return sbp.daysNarrow
150 }
151
152
153 func (sbp *sbp) WeekdayShort(weekday time.Weekday) string {
154 return sbp.daysShort[weekday]
155 }
156
157
158 func (sbp *sbp) WeekdaysShort() []string {
159 return sbp.daysShort
160 }
161
162
163 func (sbp *sbp) WeekdayWide(weekday time.Weekday) string {
164 return sbp.daysWide[weekday]
165 }
166
167
168 func (sbp *sbp) WeekdaysWide() []string {
169 return sbp.daysWide
170 }
171
172
173 func (sbp *sbp) Decimal() string {
174 return sbp.decimal
175 }
176
177
178 func (sbp *sbp) Group() string {
179 return sbp.group
180 }
181
182
183 func (sbp *sbp) Minus() string {
184 return sbp.minus
185 }
186
187
188 func (sbp *sbp) FmtNumber(num float64, v uint64) string {
189
190 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
191 l := len(s) + 1 + 1*len(s[:len(s)-int(v)-1])/3
192 count := 0
193 inWhole := v == 0
194 b := make([]byte, 0, l)
195
196 for i := len(s) - 1; i >= 0; i-- {
197
198 if s[i] == '.' {
199 b = append(b, sbp.decimal[0])
200 inWhole = true
201 continue
202 }
203
204 if inWhole {
205 if count == 3 {
206 b = append(b, sbp.group[0])
207 count = 1
208 } else {
209 count++
210 }
211 }
212
213 b = append(b, s[i])
214 }
215
216 if num < 0 {
217 b = append(b, sbp.minus[0])
218 }
219
220
221 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
222 b[i], b[j] = b[j], b[i]
223 }
224
225 return string(b)
226 }
227
228
229
230 func (sbp *sbp) FmtPercent(num float64, v uint64) string {
231 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
232 l := len(s) + 1
233 b := make([]byte, 0, l)
234
235 for i := len(s) - 1; i >= 0; i-- {
236
237 if s[i] == '.' {
238 b = append(b, sbp.decimal[0])
239 continue
240 }
241
242 b = append(b, s[i])
243 }
244
245 if num < 0 {
246 b = append(b, sbp.minus[0])
247 }
248
249
250 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
251 b[i], b[j] = b[j], b[i]
252 }
253
254 b = append(b, sbp.percent...)
255
256 return string(b)
257 }
258
259
260 func (sbp *sbp) FmtCurrency(num float64, v uint64, currency currency.Type) string {
261
262 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
263 symbol := sbp.currencies[currency]
264 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
265 count := 0
266 inWhole := v == 0
267 b := make([]byte, 0, l)
268
269 for i := len(s) - 1; i >= 0; i-- {
270
271 if s[i] == '.' {
272 b = append(b, sbp.decimal[0])
273 inWhole = true
274 continue
275 }
276
277 if inWhole {
278 if count == 3 {
279 b = append(b, sbp.group[0])
280 count = 1
281 } else {
282 count++
283 }
284 }
285
286 b = append(b, s[i])
287 }
288
289 if num < 0 {
290 b = append(b, sbp.minus[0])
291 }
292
293
294 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
295 b[i], b[j] = b[j], b[i]
296 }
297
298 if int(v) < 2 {
299
300 if v == 0 {
301 b = append(b, sbp.decimal...)
302 }
303
304 for i := 0; i < 2-int(v); i++ {
305 b = append(b, '0')
306 }
307 }
308
309 b = append(b, symbol...)
310
311 return string(b)
312 }
313
314
315
316 func (sbp *sbp) FmtAccounting(num float64, v uint64, currency currency.Type) string {
317
318 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
319 symbol := sbp.currencies[currency]
320 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
321 count := 0
322 inWhole := v == 0
323 b := make([]byte, 0, l)
324
325 for i := len(s) - 1; i >= 0; i-- {
326
327 if s[i] == '.' {
328 b = append(b, sbp.decimal[0])
329 inWhole = true
330 continue
331 }
332
333 if inWhole {
334 if count == 3 {
335 b = append(b, sbp.group[0])
336 count = 1
337 } else {
338 count++
339 }
340 }
341
342 b = append(b, s[i])
343 }
344
345 if num < 0 {
346
347 b = append(b, sbp.minus[0])
348
349 }
350
351
352 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
353 b[i], b[j] = b[j], b[i]
354 }
355
356 if int(v) < 2 {
357
358 if v == 0 {
359 b = append(b, sbp.decimal...)
360 }
361
362 for i := 0; i < 2-int(v); i++ {
363 b = append(b, '0')
364 }
365 }
366
367 if num < 0 {
368 b = append(b, symbol...)
369 } else {
370
371 b = append(b, symbol...)
372 }
373
374 return string(b)
375 }
376
377
378 func (sbp *sbp) FmtDateShort(t time.Time) string {
379
380 b := make([]byte, 0, 32)
381
382 if t.Day() < 10 {
383 b = append(b, '0')
384 }
385
386 b = strconv.AppendInt(b, int64(t.Day()), 10)
387 b = append(b, []byte{0x2f}...)
388
389 if t.Month() < 10 {
390 b = append(b, '0')
391 }
392
393 b = strconv.AppendInt(b, int64(t.Month()), 10)
394
395 b = append(b, []byte{0x2f}...)
396
397 if t.Year() > 0 {
398 b = strconv.AppendInt(b, int64(t.Year()), 10)
399 } else {
400 b = strconv.AppendInt(b, int64(-t.Year()), 10)
401 }
402
403 return string(b)
404 }
405
406
407 func (sbp *sbp) FmtDateMedium(t time.Time) string {
408
409 b := make([]byte, 0, 32)
410
411 b = strconv.AppendInt(b, int64(t.Day()), 10)
412 b = append(b, []byte{0x20}...)
413 b = append(b, sbp.monthsAbbreviated[t.Month()]...)
414 b = append(b, []byte{0x20}...)
415
416 if t.Year() > 0 {
417 b = strconv.AppendInt(b, int64(t.Year()), 10)
418 } else {
419 b = strconv.AppendInt(b, int64(-t.Year()), 10)
420 }
421
422 return string(b)
423 }
424
425
426 func (sbp *sbp) FmtDateLong(t time.Time) string {
427
428 b := make([]byte, 0, 32)
429
430 b = strconv.AppendInt(b, int64(t.Day()), 10)
431 b = append(b, []byte{0x20}...)
432 b = append(b, sbp.monthsWide[t.Month()]...)
433 b = append(b, []byte{0x20}...)
434
435 if t.Year() > 0 {
436 b = strconv.AppendInt(b, int64(t.Year()), 10)
437 } else {
438 b = strconv.AppendInt(b, int64(-t.Year()), 10)
439 }
440
441 return string(b)
442 }
443
444
445 func (sbp *sbp) FmtDateFull(t time.Time) string {
446
447 b := make([]byte, 0, 32)
448
449 b = append(b, sbp.daysWide[t.Weekday()]...)
450 b = append(b, []byte{0x2c, 0x20}...)
451 b = strconv.AppendInt(b, int64(t.Day()), 10)
452 b = append(b, []byte{0x20}...)
453 b = append(b, sbp.monthsWide[t.Month()]...)
454 b = append(b, []byte{0x20}...)
455
456 if t.Year() > 0 {
457 b = strconv.AppendInt(b, int64(t.Year()), 10)
458 } else {
459 b = strconv.AppendInt(b, int64(-t.Year()), 10)
460 }
461
462 return string(b)
463 }
464
465
466 func (sbp *sbp) FmtTimeShort(t time.Time) string {
467
468 b := make([]byte, 0, 32)
469
470 if t.Hour() < 10 {
471 b = append(b, '0')
472 }
473
474 b = strconv.AppendInt(b, int64(t.Hour()), 10)
475 b = append(b, sbp.timeSeparator...)
476
477 if t.Minute() < 10 {
478 b = append(b, '0')
479 }
480
481 b = strconv.AppendInt(b, int64(t.Minute()), 10)
482
483 return string(b)
484 }
485
486
487 func (sbp *sbp) FmtTimeMedium(t time.Time) string {
488
489 b := make([]byte, 0, 32)
490
491 if t.Hour() < 10 {
492 b = append(b, '0')
493 }
494
495 b = strconv.AppendInt(b, int64(t.Hour()), 10)
496 b = append(b, sbp.timeSeparator...)
497
498 if t.Minute() < 10 {
499 b = append(b, '0')
500 }
501
502 b = strconv.AppendInt(b, int64(t.Minute()), 10)
503 b = append(b, sbp.timeSeparator...)
504
505 if t.Second() < 10 {
506 b = append(b, '0')
507 }
508
509 b = strconv.AppendInt(b, int64(t.Second()), 10)
510
511 return string(b)
512 }
513
514
515 func (sbp *sbp) FmtTimeLong(t time.Time) string {
516
517 b := make([]byte, 0, 32)
518
519 if t.Hour() < 10 {
520 b = append(b, '0')
521 }
522
523 b = strconv.AppendInt(b, int64(t.Hour()), 10)
524 b = append(b, sbp.timeSeparator...)
525
526 if t.Minute() < 10 {
527 b = append(b, '0')
528 }
529
530 b = strconv.AppendInt(b, int64(t.Minute()), 10)
531 b = append(b, sbp.timeSeparator...)
532
533 if t.Second() < 10 {
534 b = append(b, '0')
535 }
536
537 b = strconv.AppendInt(b, int64(t.Second()), 10)
538 b = append(b, []byte{0x20}...)
539
540 tz, _ := t.Zone()
541 b = append(b, tz...)
542
543 return string(b)
544 }
545
546
547 func (sbp *sbp) FmtTimeFull(t time.Time) string {
548
549 b := make([]byte, 0, 32)
550
551 if t.Hour() < 10 {
552 b = append(b, '0')
553 }
554
555 b = strconv.AppendInt(b, int64(t.Hour()), 10)
556 b = append(b, sbp.timeSeparator...)
557
558 if t.Minute() < 10 {
559 b = append(b, '0')
560 }
561
562 b = strconv.AppendInt(b, int64(t.Minute()), 10)
563 b = append(b, sbp.timeSeparator...)
564
565 if t.Second() < 10 {
566 b = append(b, '0')
567 }
568
569 b = strconv.AppendInt(b, int64(t.Second()), 10)
570 b = append(b, []byte{0x20}...)
571
572 tz, _ := t.Zone()
573
574 if btz, ok := sbp.timezones[tz]; ok {
575 b = append(b, btz...)
576 } else {
577 b = append(b, tz...)
578 }
579
580 return string(b)
581 }
582
View as plain text