1 package bm
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 bm 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 currencyNegativePrefix string
26 currencyNegativeSuffix string
27 monthsAbbreviated []string
28 monthsNarrow []string
29 monthsWide []string
30 daysAbbreviated []string
31 daysNarrow []string
32 daysShort []string
33 daysWide []string
34 periodsAbbreviated []string
35 periodsNarrow []string
36 periodsShort []string
37 periodsWide []string
38 erasAbbreviated []string
39 erasNarrow []string
40 erasWide []string
41 timezones map[string]string
42 }
43
44
45 func New() locales.Translator {
46 return &bm{
47 locale: "bm",
48 pluralsCardinal: []locales.PluralRule{6},
49 pluralsOrdinal: nil,
50 pluralsRange: nil,
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", "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"},
53 currencyNegativePrefix: "(",
54 currencyNegativeSuffix: ")",
55 monthsAbbreviated: []string{"", "zan", "feb", "mar", "awi", "mɛ", "zuw", "zul", "uti", "sɛt", "ɔku", "now", "des"},
56 monthsNarrow: []string{"", "Z", "F", "M", "A", "M", "Z", "Z", "U", "S", "Ɔ", "N", "D"},
57 monthsWide: []string{"", "zanwuye", "feburuye", "marisi", "awirili", "mɛ", "zuwɛn", "zuluye", "uti", "sɛtanburu", "ɔkutɔburu", "nowanburu", "desanburu"},
58 daysAbbreviated: []string{"kar", "ntɛ", "tar", "ara", "ala", "jum", "sib"},
59 daysNarrow: []string{"K", "N", "T", "A", "A", "J", "S"},
60 daysWide: []string{"kari", "ntɛnɛ", "tarata", "araba", "alamisa", "juma", "sibiri"},
61 erasAbbreviated: []string{"J.-C. ɲɛ", "ni J.-C."},
62 erasNarrow: []string{"", ""},
63 erasWide: []string{"jezu krisiti ɲɛ", "jezu krisiti minkɛ"},
64 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", "∅∅∅": "∅∅∅"},
65 }
66 }
67
68
69 func (bm *bm) Locale() string {
70 return bm.locale
71 }
72
73
74 func (bm *bm) PluralsCardinal() []locales.PluralRule {
75 return bm.pluralsCardinal
76 }
77
78
79 func (bm *bm) PluralsOrdinal() []locales.PluralRule {
80 return bm.pluralsOrdinal
81 }
82
83
84 func (bm *bm) PluralsRange() []locales.PluralRule {
85 return bm.pluralsRange
86 }
87
88
89 func (bm *bm) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
90 return locales.PluralRuleOther
91 }
92
93
94 func (bm *bm) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
95 return locales.PluralRuleUnknown
96 }
97
98
99 func (bm *bm) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
100 return locales.PluralRuleUnknown
101 }
102
103
104 func (bm *bm) MonthAbbreviated(month time.Month) string {
105 return bm.monthsAbbreviated[month]
106 }
107
108
109 func (bm *bm) MonthsAbbreviated() []string {
110 return bm.monthsAbbreviated[1:]
111 }
112
113
114 func (bm *bm) MonthNarrow(month time.Month) string {
115 return bm.monthsNarrow[month]
116 }
117
118
119 func (bm *bm) MonthsNarrow() []string {
120 return bm.monthsNarrow[1:]
121 }
122
123
124 func (bm *bm) MonthWide(month time.Month) string {
125 return bm.monthsWide[month]
126 }
127
128
129 func (bm *bm) MonthsWide() []string {
130 return bm.monthsWide[1:]
131 }
132
133
134 func (bm *bm) WeekdayAbbreviated(weekday time.Weekday) string {
135 return bm.daysAbbreviated[weekday]
136 }
137
138
139 func (bm *bm) WeekdaysAbbreviated() []string {
140 return bm.daysAbbreviated
141 }
142
143
144 func (bm *bm) WeekdayNarrow(weekday time.Weekday) string {
145 return bm.daysNarrow[weekday]
146 }
147
148
149 func (bm *bm) WeekdaysNarrow() []string {
150 return bm.daysNarrow
151 }
152
153
154 func (bm *bm) WeekdayShort(weekday time.Weekday) string {
155 return bm.daysShort[weekday]
156 }
157
158
159 func (bm *bm) WeekdaysShort() []string {
160 return bm.daysShort
161 }
162
163
164 func (bm *bm) WeekdayWide(weekday time.Weekday) string {
165 return bm.daysWide[weekday]
166 }
167
168
169 func (bm *bm) WeekdaysWide() []string {
170 return bm.daysWide
171 }
172
173
174 func (bm *bm) Decimal() string {
175 return bm.decimal
176 }
177
178
179 func (bm *bm) Group() string {
180 return bm.group
181 }
182
183
184 func (bm *bm) Minus() string {
185 return bm.minus
186 }
187
188
189 func (bm *bm) FmtNumber(num float64, v uint64) string {
190
191 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
192 }
193
194
195
196 func (bm *bm) FmtPercent(num float64, v uint64) string {
197 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
198 }
199
200
201 func (bm *bm) FmtCurrency(num float64, v uint64, currency currency.Type) string {
202
203 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
204 symbol := bm.currencies[currency]
205 l := len(s) + len(symbol) + 0
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, bm.decimal[0])
214 inWhole = true
215 continue
216 }
217
218 if inWhole {
219 if count == 3 {
220 b = append(b, bm.group[0])
221 count = 1
222 } else {
223 count++
224 }
225 }
226
227 b = append(b, s[i])
228 }
229
230 for j := len(symbol) - 1; j >= 0; j-- {
231 b = append(b, symbol[j])
232 }
233
234 if num < 0 {
235 b = append(b, bm.minus[0])
236 }
237
238
239 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
240 b[i], b[j] = b[j], b[i]
241 }
242
243 if int(v) < 2 {
244
245 if v == 0 {
246 b = append(b, bm.decimal...)
247 }
248
249 for i := 0; i < 2-int(v); i++ {
250 b = append(b, '0')
251 }
252 }
253
254 return string(b)
255 }
256
257
258
259 func (bm *bm) FmtAccounting(num float64, v uint64, currency currency.Type) string {
260
261 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
262 symbol := bm.currencies[currency]
263 l := len(s) + len(symbol) + 2
264 count := 0
265 inWhole := v == 0
266 b := make([]byte, 0, l)
267
268 for i := len(s) - 1; i >= 0; i-- {
269
270 if s[i] == '.' {
271 b = append(b, bm.decimal[0])
272 inWhole = true
273 continue
274 }
275
276 if inWhole {
277 if count == 3 {
278 b = append(b, bm.group[0])
279 count = 1
280 } else {
281 count++
282 }
283 }
284
285 b = append(b, s[i])
286 }
287
288 if num < 0 {
289
290 for j := len(symbol) - 1; j >= 0; j-- {
291 b = append(b, symbol[j])
292 }
293
294 b = append(b, bm.currencyNegativePrefix[0])
295
296 } else {
297
298 for j := len(symbol) - 1; j >= 0; j-- {
299 b = append(b, symbol[j])
300 }
301
302 }
303
304
305 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
306 b[i], b[j] = b[j], b[i]
307 }
308
309 if int(v) < 2 {
310
311 if v == 0 {
312 b = append(b, bm.decimal...)
313 }
314
315 for i := 0; i < 2-int(v); i++ {
316 b = append(b, '0')
317 }
318 }
319
320 if num < 0 {
321 b = append(b, bm.currencyNegativeSuffix...)
322 }
323
324 return string(b)
325 }
326
327
328 func (bm *bm) FmtDateShort(t time.Time) string {
329
330 b := make([]byte, 0, 32)
331
332 b = strconv.AppendInt(b, int64(t.Day()), 10)
333 b = append(b, []byte{0x2f}...)
334 b = strconv.AppendInt(b, int64(t.Month()), 10)
335 b = append(b, []byte{0x2f}...)
336
337 if t.Year() > 0 {
338 b = strconv.AppendInt(b, int64(t.Year()), 10)
339 } else {
340 b = strconv.AppendInt(b, int64(-t.Year()), 10)
341 }
342
343 return string(b)
344 }
345
346
347 func (bm *bm) FmtDateMedium(t time.Time) string {
348
349 b := make([]byte, 0, 32)
350
351 b = strconv.AppendInt(b, int64(t.Day()), 10)
352 b = append(b, []byte{0x20}...)
353 b = append(b, bm.monthsAbbreviated[t.Month()]...)
354 b = append(b, []byte{0x2c, 0x20}...)
355
356 if t.Year() > 0 {
357 b = strconv.AppendInt(b, int64(t.Year()), 10)
358 } else {
359 b = strconv.AppendInt(b, int64(-t.Year()), 10)
360 }
361
362 return string(b)
363 }
364
365
366 func (bm *bm) FmtDateLong(t time.Time) string {
367
368 b := make([]byte, 0, 32)
369
370 b = strconv.AppendInt(b, int64(t.Day()), 10)
371 b = append(b, []byte{0x20}...)
372 b = append(b, bm.monthsWide[t.Month()]...)
373 b = append(b, []byte{0x20}...)
374
375 if t.Year() > 0 {
376 b = strconv.AppendInt(b, int64(t.Year()), 10)
377 } else {
378 b = strconv.AppendInt(b, int64(-t.Year()), 10)
379 }
380
381 return string(b)
382 }
383
384
385 func (bm *bm) FmtDateFull(t time.Time) string {
386
387 b := make([]byte, 0, 32)
388
389 b = append(b, bm.daysWide[t.Weekday()]...)
390 b = append(b, []byte{0x20}...)
391 b = strconv.AppendInt(b, int64(t.Day()), 10)
392 b = append(b, []byte{0x20}...)
393 b = append(b, bm.monthsWide[t.Month()]...)
394 b = append(b, []byte{0x20}...)
395
396 if t.Year() > 0 {
397 b = strconv.AppendInt(b, int64(t.Year()), 10)
398 } else {
399 b = strconv.AppendInt(b, int64(-t.Year()), 10)
400 }
401
402 return string(b)
403 }
404
405
406 func (bm *bm) FmtTimeShort(t time.Time) string {
407
408 b := make([]byte, 0, 32)
409
410 if t.Hour() < 10 {
411 b = append(b, '0')
412 }
413
414 b = strconv.AppendInt(b, int64(t.Hour()), 10)
415 b = append(b, bm.timeSeparator...)
416
417 if t.Minute() < 10 {
418 b = append(b, '0')
419 }
420
421 b = strconv.AppendInt(b, int64(t.Minute()), 10)
422
423 return string(b)
424 }
425
426
427 func (bm *bm) FmtTimeMedium(t time.Time) string {
428
429 b := make([]byte, 0, 32)
430
431 if t.Hour() < 10 {
432 b = append(b, '0')
433 }
434
435 b = strconv.AppendInt(b, int64(t.Hour()), 10)
436 b = append(b, bm.timeSeparator...)
437
438 if t.Minute() < 10 {
439 b = append(b, '0')
440 }
441
442 b = strconv.AppendInt(b, int64(t.Minute()), 10)
443 b = append(b, bm.timeSeparator...)
444
445 if t.Second() < 10 {
446 b = append(b, '0')
447 }
448
449 b = strconv.AppendInt(b, int64(t.Second()), 10)
450
451 return string(b)
452 }
453
454
455 func (bm *bm) FmtTimeLong(t time.Time) string {
456
457 b := make([]byte, 0, 32)
458
459 if t.Hour() < 10 {
460 b = append(b, '0')
461 }
462
463 b = strconv.AppendInt(b, int64(t.Hour()), 10)
464 b = append(b, bm.timeSeparator...)
465
466 if t.Minute() < 10 {
467 b = append(b, '0')
468 }
469
470 b = strconv.AppendInt(b, int64(t.Minute()), 10)
471 b = append(b, bm.timeSeparator...)
472
473 if t.Second() < 10 {
474 b = append(b, '0')
475 }
476
477 b = strconv.AppendInt(b, int64(t.Second()), 10)
478 b = append(b, []byte{0x20}...)
479
480 tz, _ := t.Zone()
481 b = append(b, tz...)
482
483 return string(b)
484 }
485
486
487 func (bm *bm) FmtTimeFull(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, bm.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, bm.timeSeparator...)
504
505 if t.Second() < 10 {
506 b = append(b, '0')
507 }
508
509 b = strconv.AppendInt(b, int64(t.Second()), 10)
510 b = append(b, []byte{0x20}...)
511
512 tz, _ := t.Zone()
513
514 if btz, ok := bm.timezones[tz]; ok {
515 b = append(b, btz...)
516 } else {
517 b = append(b, tz...)
518 }
519
520 return string(b)
521 }
522
View as plain text