1 package prg
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 prg 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 &prg{
47 locale: "prg",
48 pluralsCardinal: []locales.PluralRule{1, 2, 6},
49 pluralsOrdinal: []locales.PluralRule{6},
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", "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"},
57 currencyPositiveSuffix: " ",
58 currencyNegativeSuffix: " ",
59 monthsAbbreviated: []string{"", "rag", "was", "pūl", "sak", "zal", "sīm", "līp", "dag", "sil", "spa", "lap", "sal"},
60 monthsNarrow: []string{"", "R", "W", "P", "S", "Z", "S", "L", "D", "S", "S", "L", "S"},
61 monthsWide: []string{"", "rags", "wassarins", "pūlis", "sakkis", "zallaws", "sīmenis", "līpa", "daggis", "sillins", "spallins", "lapkrūtis", "sallaws"},
62 daysAbbreviated: []string{"nad", "pan", "wis", "pus", "ket", "pēn", "sab"},
63 daysNarrow: []string{"N", "P", "W", "P", "K", "P", "S"},
64 daysWide: []string{"nadīli", "panadīli", "wisasīdis", "pussisawaiti", "ketwirtiks", "pēntniks", "sabattika"},
65 periodsAbbreviated: []string{"AM", "PM"},
66 periodsWide: []string{"ankstāinan", "pa pussideinan"},
67 erasAbbreviated: []string{"BC", "AD"},
68 erasNarrow: []string{"", ""},
69 erasWide: []string{"", ""},
70 timezones: map[string]string{"ACDT": "ACDT", "ACST": "ACST", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "Atlāntiska daggas kerdā", "AEDT": "AEDT", "AEST": "AEST", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "Atlāntiska zēimas kerdā", "AWDT": "AWDT", "AWST": "AWST", "BOT": "BOT", "BT": "BT", "CAT": "CAT", "CDT": "Centrālas Amērikas daggas kerdā", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "CST": "Centrālas Amērikas zēimas kerdā", "ChST": "ChST", "EAT": "EAT", "ECT": "ECT", "EDT": "Dēiniskas Amērikas daggas kerdā", "EST": "Dēiniskas Amērikas zēimas kerdā", "GFT": "GFT", "GMT": "Greenwich kerdā", "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": "Amērikas gārban daggas kerdā", "MESZ": "Centrālas Eurōpas daggas kerdā", "MEZ": "Centrālas Eurōpas zēimas kerdā", "MST": "Amērikas gārban zēimas kerdā", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "Dēiniskas Eurōpas daggas kerdā", "OEZ": "Dēiniskas Eurōpas zēimas kerdā", "PDT": "Pacīfiskas Amērikas daggas kerdā", "PST": "Pacīfiskas Amērikas zēimas kerdā", "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": "Wakkariskas Eurōpas daggas kerdā", "WEZ": "Wakkariskas Eurōpas zēimas kerdā", "WIB": "WIB", "WIT": "WIT", "WITA": "WITA", "∅∅∅": "∅∅∅"},
71 }
72 }
73
74
75 func (prg *prg) Locale() string {
76 return prg.locale
77 }
78
79
80 func (prg *prg) PluralsCardinal() []locales.PluralRule {
81 return prg.pluralsCardinal
82 }
83
84
85 func (prg *prg) PluralsOrdinal() []locales.PluralRule {
86 return prg.pluralsOrdinal
87 }
88
89
90 func (prg *prg) PluralsRange() []locales.PluralRule {
91 return prg.pluralsRange
92 }
93
94
95 func (prg *prg) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
96
97 n := math.Abs(num)
98 f := locales.F(n, v)
99 nMod10 := math.Mod(n, 10)
100 nMod100 := math.Mod(n, 100)
101 fMod100 := f % 100
102 fMod10 := f % 10
103
104 if (nMod10 == 0) || (nMod100 >= 11 && nMod100 <= 19) || (v == 2 && fMod100 >= 11 && fMod100 <= 19) {
105 return locales.PluralRuleZero
106 } else if (nMod10 == 1 && nMod100 != 11) || (v == 2 && fMod10 == 1 && fMod100 != 11) || (v != 2 && fMod10 == 1) {
107 return locales.PluralRuleOne
108 }
109
110 return locales.PluralRuleOther
111 }
112
113
114 func (prg *prg) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
115 return locales.PluralRuleOther
116 }
117
118
119 func (prg *prg) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
120 return locales.PluralRuleUnknown
121 }
122
123
124 func (prg *prg) MonthAbbreviated(month time.Month) string {
125 return prg.monthsAbbreviated[month]
126 }
127
128
129 func (prg *prg) MonthsAbbreviated() []string {
130 return prg.monthsAbbreviated[1:]
131 }
132
133
134 func (prg *prg) MonthNarrow(month time.Month) string {
135 return prg.monthsNarrow[month]
136 }
137
138
139 func (prg *prg) MonthsNarrow() []string {
140 return prg.monthsNarrow[1:]
141 }
142
143
144 func (prg *prg) MonthWide(month time.Month) string {
145 return prg.monthsWide[month]
146 }
147
148
149 func (prg *prg) MonthsWide() []string {
150 return prg.monthsWide[1:]
151 }
152
153
154 func (prg *prg) WeekdayAbbreviated(weekday time.Weekday) string {
155 return prg.daysAbbreviated[weekday]
156 }
157
158
159 func (prg *prg) WeekdaysAbbreviated() []string {
160 return prg.daysAbbreviated
161 }
162
163
164 func (prg *prg) WeekdayNarrow(weekday time.Weekday) string {
165 return prg.daysNarrow[weekday]
166 }
167
168
169 func (prg *prg) WeekdaysNarrow() []string {
170 return prg.daysNarrow
171 }
172
173
174 func (prg *prg) WeekdayShort(weekday time.Weekday) string {
175 return prg.daysShort[weekday]
176 }
177
178
179 func (prg *prg) WeekdaysShort() []string {
180 return prg.daysShort
181 }
182
183
184 func (prg *prg) WeekdayWide(weekday time.Weekday) string {
185 return prg.daysWide[weekday]
186 }
187
188
189 func (prg *prg) WeekdaysWide() []string {
190 return prg.daysWide
191 }
192
193
194 func (prg *prg) Decimal() string {
195 return prg.decimal
196 }
197
198
199 func (prg *prg) Group() string {
200 return prg.group
201 }
202
203
204 func (prg *prg) Minus() string {
205 return prg.minus
206 }
207
208
209 func (prg *prg) FmtNumber(num float64, v uint64) string {
210
211 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
212 l := len(s) + 2 + 2*len(s[:len(s)-int(v)-1])/3
213 count := 0
214 inWhole := v == 0
215 b := make([]byte, 0, l)
216
217 for i := len(s) - 1; i >= 0; i-- {
218
219 if s[i] == '.' {
220 b = append(b, prg.decimal[0])
221 inWhole = true
222 continue
223 }
224
225 if inWhole {
226 if count == 3 {
227 for j := len(prg.group) - 1; j >= 0; j-- {
228 b = append(b, prg.group[j])
229 }
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, prg.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 return string(b)
249 }
250
251
252
253 func (prg *prg) FmtPercent(num float64, v uint64) string {
254 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
255 l := len(s) + 3
256 b := make([]byte, 0, l)
257
258 for i := len(s) - 1; i >= 0; i-- {
259
260 if s[i] == '.' {
261 b = append(b, prg.decimal[0])
262 continue
263 }
264
265 b = append(b, s[i])
266 }
267
268 if num < 0 {
269 b = append(b, prg.minus[0])
270 }
271
272
273 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
274 b[i], b[j] = b[j], b[i]
275 }
276
277 b = append(b, prg.percent...)
278
279 return string(b)
280 }
281
282
283 func (prg *prg) FmtCurrency(num float64, v uint64, currency currency.Type) string {
284
285 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
286 symbol := prg.currencies[currency]
287 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
288 count := 0
289 inWhole := v == 0
290 b := make([]byte, 0, l)
291
292 for i := len(s) - 1; i >= 0; i-- {
293
294 if s[i] == '.' {
295 b = append(b, prg.decimal[0])
296 inWhole = true
297 continue
298 }
299
300 if inWhole {
301 if count == 3 {
302 for j := len(prg.group) - 1; j >= 0; j-- {
303 b = append(b, prg.group[j])
304 }
305 count = 1
306 } else {
307 count++
308 }
309 }
310
311 b = append(b, s[i])
312 }
313
314 if num < 0 {
315 b = append(b, prg.minus[0])
316 }
317
318
319 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
320 b[i], b[j] = b[j], b[i]
321 }
322
323 if int(v) < 2 {
324
325 if v == 0 {
326 b = append(b, prg.decimal...)
327 }
328
329 for i := 0; i < 2-int(v); i++ {
330 b = append(b, '0')
331 }
332 }
333
334 b = append(b, prg.currencyPositiveSuffix...)
335
336 b = append(b, symbol...)
337
338 return string(b)
339 }
340
341
342
343 func (prg *prg) FmtAccounting(num float64, v uint64, currency currency.Type) string {
344
345 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
346 symbol := prg.currencies[currency]
347 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
348 count := 0
349 inWhole := v == 0
350 b := make([]byte, 0, l)
351
352 for i := len(s) - 1; i >= 0; i-- {
353
354 if s[i] == '.' {
355 b = append(b, prg.decimal[0])
356 inWhole = true
357 continue
358 }
359
360 if inWhole {
361 if count == 3 {
362 for j := len(prg.group) - 1; j >= 0; j-- {
363 b = append(b, prg.group[j])
364 }
365 count = 1
366 } else {
367 count++
368 }
369 }
370
371 b = append(b, s[i])
372 }
373
374 if num < 0 {
375
376 b = append(b, prg.minus[0])
377
378 }
379
380
381 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
382 b[i], b[j] = b[j], b[i]
383 }
384
385 if int(v) < 2 {
386
387 if v == 0 {
388 b = append(b, prg.decimal...)
389 }
390
391 for i := 0; i < 2-int(v); i++ {
392 b = append(b, '0')
393 }
394 }
395
396 if num < 0 {
397 b = append(b, prg.currencyNegativeSuffix...)
398 b = append(b, symbol...)
399 } else {
400
401 b = append(b, prg.currencyPositiveSuffix...)
402 b = append(b, symbol...)
403 }
404
405 return string(b)
406 }
407
408
409 func (prg *prg) FmtDateShort(t time.Time) string {
410
411 b := make([]byte, 0, 32)
412
413 if t.Day() < 10 {
414 b = append(b, '0')
415 }
416
417 b = strconv.AppendInt(b, int64(t.Day()), 10)
418 b = append(b, []byte{0x2e}...)
419
420 if t.Month() < 10 {
421 b = append(b, '0')
422 }
423
424 b = strconv.AppendInt(b, int64(t.Month()), 10)
425
426 b = append(b, []byte{0x2e}...)
427
428 if t.Year() > 9 {
429 b = append(b, strconv.Itoa(t.Year())[2:]...)
430 } else {
431 b = append(b, strconv.Itoa(t.Year())[1:]...)
432 }
433
434 return string(b)
435 }
436
437
438 func (prg *prg) FmtDateMedium(t time.Time) string {
439
440 b := make([]byte, 0, 32)
441
442 if t.Day() < 10 {
443 b = append(b, '0')
444 }
445
446 b = strconv.AppendInt(b, int64(t.Day()), 10)
447 b = append(b, []byte{0x2e}...)
448
449 if t.Month() < 10 {
450 b = append(b, '0')
451 }
452
453 b = strconv.AppendInt(b, int64(t.Month()), 10)
454
455 b = append(b, []byte{0x20, 0x73, 0x74}...)
456 b = append(b, []byte{0x2e, 0x20}...)
457
458 if t.Year() > 0 {
459 b = strconv.AppendInt(b, int64(t.Year()), 10)
460 } else {
461 b = strconv.AppendInt(b, int64(-t.Year()), 10)
462 }
463
464 return string(b)
465 }
466
467
468 func (prg *prg) FmtDateLong(t time.Time) string {
469
470 b := make([]byte, 0, 32)
471
472 if t.Year() > 0 {
473 b = strconv.AppendInt(b, int64(t.Year()), 10)
474 } else {
475 b = strconv.AppendInt(b, int64(-t.Year()), 10)
476 }
477
478 b = append(b, []byte{0x20, 0x6d, 0x65, 0x74, 0x74, 0x61, 0x73}...)
479 b = append(b, []byte{0x20}...)
480 b = strconv.AppendInt(b, int64(t.Day()), 10)
481 b = append(b, []byte{0x2e, 0x20}...)
482 b = append(b, prg.monthsWide[t.Month()]...)
483
484 return string(b)
485 }
486
487
488 func (prg *prg) FmtDateFull(t time.Time) string {
489
490 b := make([]byte, 0, 32)
491
492 b = append(b, prg.daysWide[t.Weekday()]...)
493 b = append(b, []byte{0x2c, 0x20}...)
494
495 if t.Year() > 0 {
496 b = strconv.AppendInt(b, int64(t.Year()), 10)
497 } else {
498 b = strconv.AppendInt(b, int64(-t.Year()), 10)
499 }
500
501 b = append(b, []byte{0x20, 0x6d, 0x65, 0x74, 0x74, 0x61, 0x73}...)
502 b = append(b, []byte{0x20}...)
503 b = strconv.AppendInt(b, int64(t.Day()), 10)
504 b = append(b, []byte{0x2e, 0x20}...)
505 b = append(b, prg.monthsWide[t.Month()]...)
506
507 return string(b)
508 }
509
510
511 func (prg *prg) FmtTimeShort(t time.Time) string {
512
513 b := make([]byte, 0, 32)
514
515 if t.Hour() < 10 {
516 b = append(b, '0')
517 }
518
519 b = strconv.AppendInt(b, int64(t.Hour()), 10)
520 b = append(b, prg.timeSeparator...)
521
522 if t.Minute() < 10 {
523 b = append(b, '0')
524 }
525
526 b = strconv.AppendInt(b, int64(t.Minute()), 10)
527
528 return string(b)
529 }
530
531
532 func (prg *prg) FmtTimeMedium(t time.Time) string {
533
534 b := make([]byte, 0, 32)
535
536 if t.Hour() < 10 {
537 b = append(b, '0')
538 }
539
540 b = strconv.AppendInt(b, int64(t.Hour()), 10)
541 b = append(b, prg.timeSeparator...)
542
543 if t.Minute() < 10 {
544 b = append(b, '0')
545 }
546
547 b = strconv.AppendInt(b, int64(t.Minute()), 10)
548 b = append(b, prg.timeSeparator...)
549
550 if t.Second() < 10 {
551 b = append(b, '0')
552 }
553
554 b = strconv.AppendInt(b, int64(t.Second()), 10)
555
556 return string(b)
557 }
558
559
560 func (prg *prg) FmtTimeLong(t time.Time) string {
561
562 b := make([]byte, 0, 32)
563
564 if t.Hour() < 10 {
565 b = append(b, '0')
566 }
567
568 b = strconv.AppendInt(b, int64(t.Hour()), 10)
569 b = append(b, prg.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, prg.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 tz, _ := t.Zone()
586 b = append(b, tz...)
587
588 return string(b)
589 }
590
591
592 func (prg *prg) FmtTimeFull(t time.Time) string {
593
594 b := make([]byte, 0, 32)
595
596 if t.Hour() < 10 {
597 b = append(b, '0')
598 }
599
600 b = strconv.AppendInt(b, int64(t.Hour()), 10)
601 b = append(b, prg.timeSeparator...)
602
603 if t.Minute() < 10 {
604 b = append(b, '0')
605 }
606
607 b = strconv.AppendInt(b, int64(t.Minute()), 10)
608 b = append(b, prg.timeSeparator...)
609
610 if t.Second() < 10 {
611 b = append(b, '0')
612 }
613
614 b = strconv.AppendInt(b, int64(t.Second()), 10)
615 b = append(b, []byte{0x20}...)
616
617 tz, _ := t.Zone()
618
619 if btz, ok := prg.timezones[tz]; ok {
620 b = append(b, btz...)
621 } else {
622 b = append(b, tz...)
623 }
624
625 return string(b)
626 }
627
View as plain text