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