1 package ii
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 ii 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 currencyPositivePrefix string
26 currencyNegativePrefix 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 &ii{
47 locale: "ii",
48 pluralsCardinal: []locales.PluralRule{6},
49 pluralsOrdinal: nil,
50 pluralsRange: nil,
51 decimal: ".",
52 group: ",",
53 minus: "-",
54 percent: "%",
55 timeSeparator: ":",
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", "¥", "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"},
57 currencyPositivePrefix: " ",
58 currencyNegativePrefix: " ",
59 monthsAbbreviated: []string{"", "ꋍꆪ", "ꑍꆪ", "ꌕꆪ", "ꇖꆪ", "ꉬꆪ", "ꃘꆪ", "ꏃꆪ", "ꉆꆪ", "ꈬꆪ", "ꊰꆪ", "ꊰꊪꆪ", "ꊰꑋꆪ"},
60 monthsNarrow: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
61 monthsWide: []string{"", "ꋍꆪ", "ꑍꆪ", "ꌕꆪ", "ꇖꆪ", "ꉬꆪ", "ꃘꆪ", "ꏃꆪ", "ꉆꆪ", "ꈬꆪ", "ꊰꆪ", "ꊰꊪꆪ", "ꊰꑋꆪ"},
62 daysAbbreviated: []string{"ꑭꆏ", "ꆏꋍ", "ꆏꑍ", "ꆏꌕ", "ꆏꇖ", "ꆏꉬ", "ꆏꃘ"},
63 daysNarrow: []string{"ꆏ", "ꋍ", "ꑍ", "ꌕ", "ꇖ", "ꉬ", "ꃘ"},
64 daysWide: []string{"ꑭꆏꑍ", "ꆏꊂꋍ", "ꆏꊂꑍ", "ꆏꊂꌕ", "ꆏꊂꇖ", "ꆏꊂꉬ", "ꆏꊂꃘ"},
65 periodsAbbreviated: []string{"ꎸꄑ", "ꁯꋒ"},
66 periodsWide: []string{"ꎸꄑ", "ꁯꋒ"},
67 erasAbbreviated: []string{"ꃅꋊꂿ", "ꃅꋊꊂ"},
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 (ii *ii) Locale() string {
76 return ii.locale
77 }
78
79
80 func (ii *ii) PluralsCardinal() []locales.PluralRule {
81 return ii.pluralsCardinal
82 }
83
84
85 func (ii *ii) PluralsOrdinal() []locales.PluralRule {
86 return ii.pluralsOrdinal
87 }
88
89
90 func (ii *ii) PluralsRange() []locales.PluralRule {
91 return ii.pluralsRange
92 }
93
94
95 func (ii *ii) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
96 return locales.PluralRuleOther
97 }
98
99
100 func (ii *ii) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
101 return locales.PluralRuleUnknown
102 }
103
104
105 func (ii *ii) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
106 return locales.PluralRuleUnknown
107 }
108
109
110 func (ii *ii) MonthAbbreviated(month time.Month) string {
111 return ii.monthsAbbreviated[month]
112 }
113
114
115 func (ii *ii) MonthsAbbreviated() []string {
116 return ii.monthsAbbreviated[1:]
117 }
118
119
120 func (ii *ii) MonthNarrow(month time.Month) string {
121 return ii.monthsNarrow[month]
122 }
123
124
125 func (ii *ii) MonthsNarrow() []string {
126 return ii.monthsNarrow[1:]
127 }
128
129
130 func (ii *ii) MonthWide(month time.Month) string {
131 return ii.monthsWide[month]
132 }
133
134
135 func (ii *ii) MonthsWide() []string {
136 return ii.monthsWide[1:]
137 }
138
139
140 func (ii *ii) WeekdayAbbreviated(weekday time.Weekday) string {
141 return ii.daysAbbreviated[weekday]
142 }
143
144
145 func (ii *ii) WeekdaysAbbreviated() []string {
146 return ii.daysAbbreviated
147 }
148
149
150 func (ii *ii) WeekdayNarrow(weekday time.Weekday) string {
151 return ii.daysNarrow[weekday]
152 }
153
154
155 func (ii *ii) WeekdaysNarrow() []string {
156 return ii.daysNarrow
157 }
158
159
160 func (ii *ii) WeekdayShort(weekday time.Weekday) string {
161 return ii.daysShort[weekday]
162 }
163
164
165 func (ii *ii) WeekdaysShort() []string {
166 return ii.daysShort
167 }
168
169
170 func (ii *ii) WeekdayWide(weekday time.Weekday) string {
171 return ii.daysWide[weekday]
172 }
173
174
175 func (ii *ii) WeekdaysWide() []string {
176 return ii.daysWide
177 }
178
179
180 func (ii *ii) Decimal() string {
181 return ii.decimal
182 }
183
184
185 func (ii *ii) Group() string {
186 return ii.group
187 }
188
189
190 func (ii *ii) Minus() string {
191 return ii.minus
192 }
193
194
195 func (ii *ii) FmtNumber(num float64, v uint64) string {
196
197 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
198 l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
199 count := 0
200 inWhole := v == 0
201 b := make([]byte, 0, l)
202
203 for i := len(s) - 1; i >= 0; i-- {
204
205 if s[i] == '.' {
206 b = append(b, ii.decimal[0])
207 inWhole = true
208 continue
209 }
210
211 if inWhole {
212 if count == 3 {
213 b = append(b, ii.group[0])
214 count = 1
215 } else {
216 count++
217 }
218 }
219
220 b = append(b, s[i])
221 }
222
223 if num < 0 {
224 b = append(b, ii.minus[0])
225 }
226
227
228 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
229 b[i], b[j] = b[j], b[i]
230 }
231
232 return string(b)
233 }
234
235
236
237 func (ii *ii) FmtPercent(num float64, v uint64) string {
238 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
239 l := len(s) + 3
240 b := make([]byte, 0, l)
241
242 for i := len(s) - 1; i >= 0; i-- {
243
244 if s[i] == '.' {
245 b = append(b, ii.decimal[0])
246 continue
247 }
248
249 b = append(b, s[i])
250 }
251
252 if num < 0 {
253 b = append(b, ii.minus[0])
254 }
255
256
257 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
258 b[i], b[j] = b[j], b[i]
259 }
260
261 b = append(b, ii.percent...)
262
263 return string(b)
264 }
265
266
267 func (ii *ii) FmtCurrency(num float64, v uint64, currency currency.Type) string {
268
269 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
270 symbol := ii.currencies[currency]
271 l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
272 count := 0
273 inWhole := v == 0
274 b := make([]byte, 0, l)
275
276 for i := len(s) - 1; i >= 0; i-- {
277
278 if s[i] == '.' {
279 b = append(b, ii.decimal[0])
280 inWhole = true
281 continue
282 }
283
284 if inWhole {
285 if count == 3 {
286 b = append(b, ii.group[0])
287 count = 1
288 } else {
289 count++
290 }
291 }
292
293 b = append(b, s[i])
294 }
295
296 for j := len(symbol) - 1; j >= 0; j-- {
297 b = append(b, symbol[j])
298 }
299
300 for j := len(ii.currencyPositivePrefix) - 1; j >= 0; j-- {
301 b = append(b, ii.currencyPositivePrefix[j])
302 }
303
304 if num < 0 {
305 b = append(b, ii.minus[0])
306 }
307
308
309 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
310 b[i], b[j] = b[j], b[i]
311 }
312
313 if int(v) < 2 {
314
315 if v == 0 {
316 b = append(b, ii.decimal...)
317 }
318
319 for i := 0; i < 2-int(v); i++ {
320 b = append(b, '0')
321 }
322 }
323
324 return string(b)
325 }
326
327
328
329 func (ii *ii) FmtAccounting(num float64, v uint64, currency currency.Type) string {
330
331 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
332 symbol := ii.currencies[currency]
333 l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
334 count := 0
335 inWhole := v == 0
336 b := make([]byte, 0, l)
337
338 for i := len(s) - 1; i >= 0; i-- {
339
340 if s[i] == '.' {
341 b = append(b, ii.decimal[0])
342 inWhole = true
343 continue
344 }
345
346 if inWhole {
347 if count == 3 {
348 b = append(b, ii.group[0])
349 count = 1
350 } else {
351 count++
352 }
353 }
354
355 b = append(b, s[i])
356 }
357
358 if num < 0 {
359
360 for j := len(symbol) - 1; j >= 0; j-- {
361 b = append(b, symbol[j])
362 }
363
364 for j := len(ii.currencyNegativePrefix) - 1; j >= 0; j-- {
365 b = append(b, ii.currencyNegativePrefix[j])
366 }
367
368 b = append(b, ii.minus[0])
369
370 } else {
371
372 for j := len(symbol) - 1; j >= 0; j-- {
373 b = append(b, symbol[j])
374 }
375
376 for j := len(ii.currencyPositivePrefix) - 1; j >= 0; j-- {
377 b = append(b, ii.currencyPositivePrefix[j])
378 }
379
380 }
381
382
383 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
384 b[i], b[j] = b[j], b[i]
385 }
386
387 if int(v) < 2 {
388
389 if v == 0 {
390 b = append(b, ii.decimal...)
391 }
392
393 for i := 0; i < 2-int(v); i++ {
394 b = append(b, '0')
395 }
396 }
397
398 return string(b)
399 }
400
401
402 func (ii *ii) FmtDateShort(t time.Time) string {
403
404 b := make([]byte, 0, 32)
405
406 if t.Year() > 0 {
407 b = strconv.AppendInt(b, int64(t.Year()), 10)
408 } else {
409 b = strconv.AppendInt(b, int64(-t.Year()), 10)
410 }
411
412 b = append(b, []byte{0x2d}...)
413
414 if t.Month() < 10 {
415 b = append(b, '0')
416 }
417
418 b = strconv.AppendInt(b, int64(t.Month()), 10)
419
420 b = append(b, []byte{0x2d}...)
421
422 if t.Day() < 10 {
423 b = append(b, '0')
424 }
425
426 b = strconv.AppendInt(b, int64(t.Day()), 10)
427
428 return string(b)
429 }
430
431
432 func (ii *ii) FmtDateMedium(t time.Time) string {
433
434 b := make([]byte, 0, 32)
435
436 if t.Year() > 0 {
437 b = strconv.AppendInt(b, int64(t.Year()), 10)
438 } else {
439 b = strconv.AppendInt(b, int64(-t.Year()), 10)
440 }
441
442 b = append(b, []byte{0x20}...)
443 b = append(b, ii.monthsAbbreviated[t.Month()]...)
444 b = append(b, []byte{0x20}...)
445 b = strconv.AppendInt(b, int64(t.Day()), 10)
446
447 return string(b)
448 }
449
450
451 func (ii *ii) FmtDateLong(t time.Time) string {
452
453 b := make([]byte, 0, 32)
454
455 if t.Year() > 0 {
456 b = strconv.AppendInt(b, int64(t.Year()), 10)
457 } else {
458 b = strconv.AppendInt(b, int64(-t.Year()), 10)
459 }
460
461 b = append(b, []byte{0x20}...)
462 b = append(b, ii.monthsWide[t.Month()]...)
463 b = append(b, []byte{0x20}...)
464 b = strconv.AppendInt(b, int64(t.Day()), 10)
465
466 return string(b)
467 }
468
469
470 func (ii *ii) FmtDateFull(t time.Time) string {
471
472 b := make([]byte, 0, 32)
473
474 if t.Year() > 0 {
475 b = strconv.AppendInt(b, int64(t.Year()), 10)
476 } else {
477 b = strconv.AppendInt(b, int64(-t.Year()), 10)
478 }
479
480 b = append(b, []byte{0x20}...)
481 b = append(b, ii.monthsWide[t.Month()]...)
482 b = append(b, []byte{0x20}...)
483 b = strconv.AppendInt(b, int64(t.Day()), 10)
484 b = append(b, []byte{0x2c, 0x20}...)
485 b = append(b, ii.daysWide[t.Weekday()]...)
486
487 return string(b)
488 }
489
490
491 func (ii *ii) FmtTimeShort(t time.Time) string {
492
493 b := make([]byte, 0, 32)
494
495 h := t.Hour()
496
497 if h > 12 {
498 h -= 12
499 }
500
501 b = strconv.AppendInt(b, int64(h), 10)
502 b = append(b, ii.timeSeparator...)
503
504 if t.Minute() < 10 {
505 b = append(b, '0')
506 }
507
508 b = strconv.AppendInt(b, int64(t.Minute()), 10)
509 b = append(b, []byte{0x20}...)
510
511 if t.Hour() < 12 {
512 b = append(b, ii.periodsAbbreviated[0]...)
513 } else {
514 b = append(b, ii.periodsAbbreviated[1]...)
515 }
516
517 return string(b)
518 }
519
520
521 func (ii *ii) FmtTimeMedium(t time.Time) string {
522
523 b := make([]byte, 0, 32)
524
525 h := t.Hour()
526
527 if h > 12 {
528 h -= 12
529 }
530
531 b = strconv.AppendInt(b, int64(h), 10)
532 b = append(b, ii.timeSeparator...)
533
534 if t.Minute() < 10 {
535 b = append(b, '0')
536 }
537
538 b = strconv.AppendInt(b, int64(t.Minute()), 10)
539 b = append(b, ii.timeSeparator...)
540
541 if t.Second() < 10 {
542 b = append(b, '0')
543 }
544
545 b = strconv.AppendInt(b, int64(t.Second()), 10)
546 b = append(b, []byte{0x20}...)
547
548 if t.Hour() < 12 {
549 b = append(b, ii.periodsAbbreviated[0]...)
550 } else {
551 b = append(b, ii.periodsAbbreviated[1]...)
552 }
553
554 return string(b)
555 }
556
557
558 func (ii *ii) FmtTimeLong(t time.Time) string {
559
560 b := make([]byte, 0, 32)
561
562 h := t.Hour()
563
564 if h > 12 {
565 h -= 12
566 }
567
568 b = strconv.AppendInt(b, int64(h), 10)
569 b = append(b, ii.timeSeparator...)
570
571 if t.Minute() < 10 {
572 b = append(b, '0')
573 }
574
575 b = strconv.AppendInt(b, int64(t.Minute()), 10)
576 b = append(b, ii.timeSeparator...)
577
578 if t.Second() < 10 {
579 b = append(b, '0')
580 }
581
582 b = strconv.AppendInt(b, int64(t.Second()), 10)
583 b = append(b, []byte{0x20}...)
584
585 if t.Hour() < 12 {
586 b = append(b, ii.periodsAbbreviated[0]...)
587 } else {
588 b = append(b, ii.periodsAbbreviated[1]...)
589 }
590
591 b = append(b, []byte{0x20}...)
592
593 tz, _ := t.Zone()
594 b = append(b, tz...)
595
596 return string(b)
597 }
598
599
600 func (ii *ii) FmtTimeFull(t time.Time) string {
601
602 b := make([]byte, 0, 32)
603
604 h := t.Hour()
605
606 if h > 12 {
607 h -= 12
608 }
609
610 b = strconv.AppendInt(b, int64(h), 10)
611 b = append(b, ii.timeSeparator...)
612
613 if t.Minute() < 10 {
614 b = append(b, '0')
615 }
616
617 b = strconv.AppendInt(b, int64(t.Minute()), 10)
618 b = append(b, ii.timeSeparator...)
619
620 if t.Second() < 10 {
621 b = append(b, '0')
622 }
623
624 b = strconv.AppendInt(b, int64(t.Second()), 10)
625 b = append(b, []byte{0x20}...)
626
627 if t.Hour() < 12 {
628 b = append(b, ii.periodsAbbreviated[0]...)
629 } else {
630 b = append(b, ii.periodsAbbreviated[1]...)
631 }
632
633 b = append(b, []byte{0x20}...)
634
635 tz, _ := t.Zone()
636
637 if btz, ok := ii.timezones[tz]; ok {
638 b = append(b, btz...)
639 } else {
640 b = append(b, tz...)
641 }
642
643 return string(b)
644 }
645
View as plain text