1 package lrc
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 lrc 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 &lrc{
47 locale: "lrc",
48 pluralsCardinal: nil,
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", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CN¥", "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", "€", "FIM", "FJD", "FKP", "FRF", "£", "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", "₹", "د.ع.\u200f", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JP¥", "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", "US$", "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 currencyPositivePrefix: " ",
54 currencyNegativePrefix: " ",
55 monthsAbbreviated: []string{"", "جانڤیە", "فئڤریە", "مارس", "آڤریل", "مئی", "جوٙأن", "جوٙلا", "آگوست", "سئپتامر", "ئوکتوڤر", "نوڤامر", "دئسامر"},
56 monthsNarrow: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
57 monthsWide: []string{"", "جانڤیە", "فئڤریە", "مارس", "آڤریل", "مئی", "جوٙأن", "جوٙلا", "آگوست", "سئپتامر", "ئوکتوڤر", "نوڤامر", "دئسامر"},
58 daysAbbreviated: []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
59 daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
60 daysWide: []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
61 periodsAbbreviated: []string{"AM", "PM"},
62 periodsWide: []string{"AM", "PM"},
63 erasAbbreviated: []string{"BCE", "CE"},
64 erasNarrow: []string{"", ""},
65 erasWide: []string{"", ""},
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": "روٙشنایی نئهادار روٙز", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "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 (lrc *lrc) Locale() string {
72 return lrc.locale
73 }
74
75
76 func (lrc *lrc) PluralsCardinal() []locales.PluralRule {
77 return lrc.pluralsCardinal
78 }
79
80
81 func (lrc *lrc) PluralsOrdinal() []locales.PluralRule {
82 return lrc.pluralsOrdinal
83 }
84
85
86 func (lrc *lrc) PluralsRange() []locales.PluralRule {
87 return lrc.pluralsRange
88 }
89
90
91 func (lrc *lrc) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
92 return locales.PluralRuleUnknown
93 }
94
95
96 func (lrc *lrc) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
97 return locales.PluralRuleUnknown
98 }
99
100
101 func (lrc *lrc) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
102 return locales.PluralRuleUnknown
103 }
104
105
106 func (lrc *lrc) MonthAbbreviated(month time.Month) string {
107 return lrc.monthsAbbreviated[month]
108 }
109
110
111 func (lrc *lrc) MonthsAbbreviated() []string {
112 return lrc.monthsAbbreviated[1:]
113 }
114
115
116 func (lrc *lrc) MonthNarrow(month time.Month) string {
117 return lrc.monthsNarrow[month]
118 }
119
120
121 func (lrc *lrc) MonthsNarrow() []string {
122 return lrc.monthsNarrow[1:]
123 }
124
125
126 func (lrc *lrc) MonthWide(month time.Month) string {
127 return lrc.monthsWide[month]
128 }
129
130
131 func (lrc *lrc) MonthsWide() []string {
132 return lrc.monthsWide[1:]
133 }
134
135
136 func (lrc *lrc) WeekdayAbbreviated(weekday time.Weekday) string {
137 return lrc.daysAbbreviated[weekday]
138 }
139
140
141 func (lrc *lrc) WeekdaysAbbreviated() []string {
142 return lrc.daysAbbreviated
143 }
144
145
146 func (lrc *lrc) WeekdayNarrow(weekday time.Weekday) string {
147 return lrc.daysNarrow[weekday]
148 }
149
150
151 func (lrc *lrc) WeekdaysNarrow() []string {
152 return lrc.daysNarrow
153 }
154
155
156 func (lrc *lrc) WeekdayShort(weekday time.Weekday) string {
157 return lrc.daysShort[weekday]
158 }
159
160
161 func (lrc *lrc) WeekdaysShort() []string {
162 return lrc.daysShort
163 }
164
165
166 func (lrc *lrc) WeekdayWide(weekday time.Weekday) string {
167 return lrc.daysWide[weekday]
168 }
169
170
171 func (lrc *lrc) WeekdaysWide() []string {
172 return lrc.daysWide
173 }
174
175
176 func (lrc *lrc) Decimal() string {
177 return lrc.decimal
178 }
179
180
181 func (lrc *lrc) Group() string {
182 return lrc.group
183 }
184
185
186 func (lrc *lrc) Minus() string {
187 return lrc.minus
188 }
189
190
191 func (lrc *lrc) FmtNumber(num float64, v uint64) string {
192
193 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
194 l := len(s) + 0
195 count := 0
196 inWhole := v == 0
197 b := make([]byte, 0, l)
198
199 for i := len(s) - 1; i >= 0; i-- {
200
201 if s[i] == '.' {
202 b = append(b, lrc.decimal[0])
203 inWhole = true
204 continue
205 }
206
207 if inWhole {
208 if count == 3 {
209 b = append(b, lrc.group[0])
210 count = 1
211 } else {
212 count++
213 }
214 }
215
216 b = append(b, s[i])
217 }
218
219 if num < 0 {
220 b = append(b, lrc.minus[0])
221 }
222
223
224 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
225 b[i], b[j] = b[j], b[i]
226 }
227
228 return string(b)
229 }
230
231
232
233 func (lrc *lrc) FmtPercent(num float64, v uint64) string {
234 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
235 l := len(s) + 0
236 b := make([]byte, 0, l)
237
238 for i := len(s) - 1; i >= 0; i-- {
239
240 if s[i] == '.' {
241 b = append(b, lrc.decimal[0])
242 continue
243 }
244
245 b = append(b, s[i])
246 }
247
248 if num < 0 {
249 b = append(b, lrc.minus[0])
250 }
251
252
253 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
254 b[i], b[j] = b[j], b[i]
255 }
256
257 b = append(b, lrc.percent...)
258
259 return string(b)
260 }
261
262
263 func (lrc *lrc) FmtCurrency(num float64, v uint64, currency currency.Type) string {
264
265 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
266 symbol := lrc.currencies[currency]
267 l := len(s) + len(symbol) + 2
268 count := 0
269 inWhole := v == 0
270 b := make([]byte, 0, l)
271
272 for i := len(s) - 1; i >= 0; i-- {
273
274 if s[i] == '.' {
275 b = append(b, lrc.decimal[0])
276 inWhole = true
277 continue
278 }
279
280 if inWhole {
281 if count == 3 {
282 b = append(b, lrc.group[0])
283 count = 1
284 } else {
285 count++
286 }
287 }
288
289 b = append(b, s[i])
290 }
291
292 for j := len(symbol) - 1; j >= 0; j-- {
293 b = append(b, symbol[j])
294 }
295
296 for j := len(lrc.currencyPositivePrefix) - 1; j >= 0; j-- {
297 b = append(b, lrc.currencyPositivePrefix[j])
298 }
299
300 if num < 0 {
301 b = append(b, lrc.minus[0])
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, lrc.decimal...)
313 }
314
315 for i := 0; i < 2-int(v); i++ {
316 b = append(b, '0')
317 }
318 }
319
320 return string(b)
321 }
322
323
324
325 func (lrc *lrc) FmtAccounting(num float64, v uint64, currency currency.Type) string {
326
327 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
328 symbol := lrc.currencies[currency]
329 l := len(s) + len(symbol) + 2
330 count := 0
331 inWhole := v == 0
332 b := make([]byte, 0, l)
333
334 for i := len(s) - 1; i >= 0; i-- {
335
336 if s[i] == '.' {
337 b = append(b, lrc.decimal[0])
338 inWhole = true
339 continue
340 }
341
342 if inWhole {
343 if count == 3 {
344 b = append(b, lrc.group[0])
345 count = 1
346 } else {
347 count++
348 }
349 }
350
351 b = append(b, s[i])
352 }
353
354 if num < 0 {
355
356 for j := len(symbol) - 1; j >= 0; j-- {
357 b = append(b, symbol[j])
358 }
359
360 for j := len(lrc.currencyNegativePrefix) - 1; j >= 0; j-- {
361 b = append(b, lrc.currencyNegativePrefix[j])
362 }
363
364 b = append(b, lrc.minus[0])
365
366 } else {
367
368 for j := len(symbol) - 1; j >= 0; j-- {
369 b = append(b, symbol[j])
370 }
371
372 for j := len(lrc.currencyPositivePrefix) - 1; j >= 0; j-- {
373 b = append(b, lrc.currencyPositivePrefix[j])
374 }
375
376 }
377
378
379 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
380 b[i], b[j] = b[j], b[i]
381 }
382
383 if int(v) < 2 {
384
385 if v == 0 {
386 b = append(b, lrc.decimal...)
387 }
388
389 for i := 0; i < 2-int(v); i++ {
390 b = append(b, '0')
391 }
392 }
393
394 return string(b)
395 }
396
397
398 func (lrc *lrc) FmtDateShort(t time.Time) string {
399
400 b := make([]byte, 0, 32)
401
402 if t.Year() > 0 {
403 b = strconv.AppendInt(b, int64(t.Year()), 10)
404 } else {
405 b = strconv.AppendInt(b, int64(-t.Year()), 10)
406 }
407
408 b = append(b, []byte{0x2d}...)
409
410 if t.Month() < 10 {
411 b = append(b, '0')
412 }
413
414 b = strconv.AppendInt(b, int64(t.Month()), 10)
415
416 b = append(b, []byte{0x2d}...)
417
418 if t.Day() < 10 {
419 b = append(b, '0')
420 }
421
422 b = strconv.AppendInt(b, int64(t.Day()), 10)
423
424 return string(b)
425 }
426
427
428 func (lrc *lrc) FmtDateMedium(t time.Time) string {
429
430 b := make([]byte, 0, 32)
431
432 if t.Year() > 0 {
433 b = strconv.AppendInt(b, int64(t.Year()), 10)
434 } else {
435 b = strconv.AppendInt(b, int64(-t.Year()), 10)
436 }
437
438 b = append(b, []byte{0x20}...)
439 b = append(b, lrc.monthsAbbreviated[t.Month()]...)
440 b = append(b, []byte{0x20}...)
441 b = strconv.AppendInt(b, int64(t.Day()), 10)
442
443 return string(b)
444 }
445
446
447 func (lrc *lrc) FmtDateLong(t time.Time) string {
448
449 b := make([]byte, 0, 32)
450
451 if t.Year() > 0 {
452 b = strconv.AppendInt(b, int64(t.Year()), 10)
453 } else {
454 b = strconv.AppendInt(b, int64(-t.Year()), 10)
455 }
456
457 b = append(b, []byte{0x20}...)
458 b = append(b, lrc.monthsWide[t.Month()]...)
459 b = append(b, []byte{0x20}...)
460 b = strconv.AppendInt(b, int64(t.Day()), 10)
461
462 return string(b)
463 }
464
465
466 func (lrc *lrc) FmtDateFull(t time.Time) string {
467
468 b := make([]byte, 0, 32)
469
470 if t.Year() > 0 {
471 b = strconv.AppendInt(b, int64(t.Year()), 10)
472 } else {
473 b = strconv.AppendInt(b, int64(-t.Year()), 10)
474 }
475
476 b = append(b, []byte{0x20}...)
477 b = append(b, lrc.monthsWide[t.Month()]...)
478 b = append(b, []byte{0x20}...)
479 b = strconv.AppendInt(b, int64(t.Day()), 10)
480 b = append(b, []byte{0x2c, 0x20}...)
481 b = append(b, lrc.daysWide[t.Weekday()]...)
482
483 return string(b)
484 }
485
486
487 func (lrc *lrc) FmtTimeShort(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, lrc.timeSeparator...)
497
498 if t.Minute() < 10 {
499 b = append(b, '0')
500 }
501
502 b = strconv.AppendInt(b, int64(t.Minute()), 10)
503
504 return string(b)
505 }
506
507
508 func (lrc *lrc) FmtTimeMedium(t time.Time) string {
509
510 b := make([]byte, 0, 32)
511
512 if t.Hour() < 10 {
513 b = append(b, '0')
514 }
515
516 b = strconv.AppendInt(b, int64(t.Hour()), 10)
517 b = append(b, lrc.timeSeparator...)
518
519 if t.Minute() < 10 {
520 b = append(b, '0')
521 }
522
523 b = strconv.AppendInt(b, int64(t.Minute()), 10)
524 b = append(b, lrc.timeSeparator...)
525
526 if t.Second() < 10 {
527 b = append(b, '0')
528 }
529
530 b = strconv.AppendInt(b, int64(t.Second()), 10)
531
532 return string(b)
533 }
534
535
536 func (lrc *lrc) FmtTimeLong(t time.Time) string {
537
538 b := make([]byte, 0, 32)
539
540 if t.Hour() < 10 {
541 b = append(b, '0')
542 }
543
544 b = strconv.AppendInt(b, int64(t.Hour()), 10)
545 b = append(b, lrc.timeSeparator...)
546
547 if t.Minute() < 10 {
548 b = append(b, '0')
549 }
550
551 b = strconv.AppendInt(b, int64(t.Minute()), 10)
552 b = append(b, lrc.timeSeparator...)
553
554 if t.Second() < 10 {
555 b = append(b, '0')
556 }
557
558 b = strconv.AppendInt(b, int64(t.Second()), 10)
559 b = append(b, []byte{0x20}...)
560
561 tz, _ := t.Zone()
562 b = append(b, tz...)
563
564 return string(b)
565 }
566
567
568 func (lrc *lrc) FmtTimeFull(t time.Time) string {
569
570 b := make([]byte, 0, 32)
571
572 if t.Hour() < 10 {
573 b = append(b, '0')
574 }
575
576 b = strconv.AppendInt(b, int64(t.Hour()), 10)
577 b = append(b, lrc.timeSeparator...)
578
579 if t.Minute() < 10 {
580 b = append(b, '0')
581 }
582
583 b = strconv.AppendInt(b, int64(t.Minute()), 10)
584 b = append(b, lrc.timeSeparator...)
585
586 if t.Second() < 10 {
587 b = append(b, '0')
588 }
589
590 b = strconv.AppendInt(b, int64(t.Second()), 10)
591 b = append(b, []byte{0x20}...)
592
593 tz, _ := t.Zone()
594
595 if btz, ok := lrc.timezones[tz]; ok {
596 b = append(b, btz...)
597 } else {
598 b = append(b, tz...)
599 }
600
601 return string(b)
602 }
603
View as plain text