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