1 package yav
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 yav 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 currencyNegativePrefix string
27 currencyNegativeSuffix string
28 monthsAbbreviated []string
29 monthsNarrow []string
30 monthsWide []string
31 daysAbbreviated []string
32 daysNarrow []string
33 daysShort []string
34 daysWide []string
35 periodsAbbreviated []string
36 periodsNarrow []string
37 periodsShort []string
38 periodsWide []string
39 erasAbbreviated []string
40 erasNarrow []string
41 erasWide []string
42 timezones map[string]string
43 }
44
45
46 func New() locales.Translator {
47 return &yav{
48 locale: "yav",
49 pluralsCardinal: nil,
50 pluralsOrdinal: nil,
51 pluralsRange: nil,
52 decimal: ",",
53 group: " ",
54 timeSeparator: ":",
55 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"},
56 currencyPositiveSuffix: " ",
57 currencyNegativePrefix: "(",
58 currencyNegativeSuffix: " )",
59 monthsAbbreviated: []string{"", "o.1", "o.2", "o.3", "o.4", "o.5", "o.6", "o.7", "o.8", "o.9", "o.10", "o.11", "o.12"},
60 monthsWide: []string{"", "pikítíkítie, oólí ú kutúan", "siɛyɛ́, oóli ú kándíɛ", "ɔnsúmbɔl, oóli ú kátátúɛ", "mesiŋ, oóli ú kénie", "ensil, oóli ú kátánuɛ", "ɔsɔn", "efute", "pisuyú", "imɛŋ i puɔs", "imɛŋ i putúk,oóli ú kátíɛ", "makandikɛ", "pilɔndɔ́"},
61 daysAbbreviated: []string{"sd", "md", "mw", "et", "kl", "fl", "ss"},
62 daysNarrow: []string{"s", "m", "m", "e", "k", "f", "s"},
63 daysWide: []string{"sɔ́ndiɛ", "móndie", "muányáŋmóndie", "metúkpíápɛ", "kúpélimetúkpiapɛ", "feléte", "séselé"},
64 periodsAbbreviated: []string{"kiɛmɛ́ɛm", "kisɛ́ndɛ"},
65 periodsWide: []string{"kiɛmɛ́ɛm", "kisɛ́ndɛ"},
66 erasAbbreviated: []string{"k.Y.", "+J.C."},
67 erasNarrow: []string{"", ""},
68 erasWide: []string{"katikupíen Yésuse", "ékélémkúnupíén n"},
69 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", "∅∅∅": "∅∅∅"},
70 }
71 }
72
73
74 func (yav *yav) Locale() string {
75 return yav.locale
76 }
77
78
79 func (yav *yav) PluralsCardinal() []locales.PluralRule {
80 return yav.pluralsCardinal
81 }
82
83
84 func (yav *yav) PluralsOrdinal() []locales.PluralRule {
85 return yav.pluralsOrdinal
86 }
87
88
89 func (yav *yav) PluralsRange() []locales.PluralRule {
90 return yav.pluralsRange
91 }
92
93
94 func (yav *yav) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
95 return locales.PluralRuleUnknown
96 }
97
98
99 func (yav *yav) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
100 return locales.PluralRuleUnknown
101 }
102
103
104 func (yav *yav) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
105 return locales.PluralRuleUnknown
106 }
107
108
109 func (yav *yav) MonthAbbreviated(month time.Month) string {
110 return yav.monthsAbbreviated[month]
111 }
112
113
114 func (yav *yav) MonthsAbbreviated() []string {
115 return yav.monthsAbbreviated[1:]
116 }
117
118
119 func (yav *yav) MonthNarrow(month time.Month) string {
120 return yav.monthsNarrow[month]
121 }
122
123
124 func (yav *yav) MonthsNarrow() []string {
125 return nil
126 }
127
128
129 func (yav *yav) MonthWide(month time.Month) string {
130 return yav.monthsWide[month]
131 }
132
133
134 func (yav *yav) MonthsWide() []string {
135 return yav.monthsWide[1:]
136 }
137
138
139 func (yav *yav) WeekdayAbbreviated(weekday time.Weekday) string {
140 return yav.daysAbbreviated[weekday]
141 }
142
143
144 func (yav *yav) WeekdaysAbbreviated() []string {
145 return yav.daysAbbreviated
146 }
147
148
149 func (yav *yav) WeekdayNarrow(weekday time.Weekday) string {
150 return yav.daysNarrow[weekday]
151 }
152
153
154 func (yav *yav) WeekdaysNarrow() []string {
155 return yav.daysNarrow
156 }
157
158
159 func (yav *yav) WeekdayShort(weekday time.Weekday) string {
160 return yav.daysShort[weekday]
161 }
162
163
164 func (yav *yav) WeekdaysShort() []string {
165 return yav.daysShort
166 }
167
168
169 func (yav *yav) WeekdayWide(weekday time.Weekday) string {
170 return yav.daysWide[weekday]
171 }
172
173
174 func (yav *yav) WeekdaysWide() []string {
175 return yav.daysWide
176 }
177
178
179 func (yav *yav) Decimal() string {
180 return yav.decimal
181 }
182
183
184 func (yav *yav) Group() string {
185 return yav.group
186 }
187
188
189 func (yav *yav) Minus() string {
190 return yav.minus
191 }
192
193
194 func (yav *yav) FmtNumber(num float64, v uint64) string {
195
196 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
197 l := len(s) + 1 + 2*len(s[:len(s)-int(v)-1])/3
198 count := 0
199 inWhole := v == 0
200 b := make([]byte, 0, l)
201
202 for i := len(s) - 1; i >= 0; i-- {
203
204 if s[i] == '.' {
205 b = append(b, yav.decimal[0])
206 inWhole = true
207 continue
208 }
209
210 if inWhole {
211 if count == 3 {
212 for j := len(yav.group) - 1; j >= 0; j-- {
213 b = append(b, yav.group[j])
214 }
215 count = 1
216 } else {
217 count++
218 }
219 }
220
221 b = append(b, s[i])
222 }
223
224 if num < 0 {
225 b = append(b, yav.minus[0])
226 }
227
228
229 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
230 b[i], b[j] = b[j], b[i]
231 }
232
233 return string(b)
234 }
235
236
237
238 func (yav *yav) FmtPercent(num float64, v uint64) string {
239 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
240 l := len(s) + 1
241 b := make([]byte, 0, l)
242
243 for i := len(s) - 1; i >= 0; i-- {
244
245 if s[i] == '.' {
246 b = append(b, yav.decimal[0])
247 continue
248 }
249
250 b = append(b, s[i])
251 }
252
253 if num < 0 {
254 b = append(b, yav.minus[0])
255 }
256
257
258 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
259 b[i], b[j] = b[j], b[i]
260 }
261
262 b = append(b, yav.percent...)
263
264 return string(b)
265 }
266
267
268 func (yav *yav) FmtCurrency(num float64, v uint64, currency currency.Type) string {
269
270 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
271 symbol := yav.currencies[currency]
272 l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
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, yav.decimal[0])
281 inWhole = true
282 continue
283 }
284
285 if inWhole {
286 if count == 3 {
287 for j := len(yav.group) - 1; j >= 0; j-- {
288 b = append(b, yav.group[j])
289 }
290 count = 1
291 } else {
292 count++
293 }
294 }
295
296 b = append(b, s[i])
297 }
298
299 if num < 0 {
300 b = append(b, yav.minus[0])
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, yav.decimal...)
312 }
313
314 for i := 0; i < 2-int(v); i++ {
315 b = append(b, '0')
316 }
317 }
318
319 b = append(b, yav.currencyPositiveSuffix...)
320
321 b = append(b, symbol...)
322
323 return string(b)
324 }
325
326
327
328 func (yav *yav) FmtAccounting(num float64, v uint64, currency currency.Type) string {
329
330 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
331 symbol := yav.currencies[currency]
332 l := len(s) + len(symbol) + 5 + 2*len(s[:len(s)-int(v)-1])/3
333 count := 0
334 inWhole := v == 0
335 b := make([]byte, 0, l)
336
337 for i := len(s) - 1; i >= 0; i-- {
338
339 if s[i] == '.' {
340 b = append(b, yav.decimal[0])
341 inWhole = true
342 continue
343 }
344
345 if inWhole {
346 if count == 3 {
347 for j := len(yav.group) - 1; j >= 0; j-- {
348 b = append(b, yav.group[j])
349 }
350 count = 1
351 } else {
352 count++
353 }
354 }
355
356 b = append(b, s[i])
357 }
358
359 if num < 0 {
360
361 b = append(b, yav.currencyNegativePrefix[0])
362
363 }
364
365
366 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
367 b[i], b[j] = b[j], b[i]
368 }
369
370 if int(v) < 2 {
371
372 if v == 0 {
373 b = append(b, yav.decimal...)
374 }
375
376 for i := 0; i < 2-int(v); i++ {
377 b = append(b, '0')
378 }
379 }
380
381 if num < 0 {
382 b = append(b, yav.currencyNegativeSuffix...)
383 b = append(b, symbol...)
384 } else {
385
386 b = append(b, yav.currencyPositiveSuffix...)
387 b = append(b, symbol...)
388 }
389
390 return string(b)
391 }
392
393
394 func (yav *yav) FmtDateShort(t time.Time) string {
395
396 b := make([]byte, 0, 32)
397
398 b = strconv.AppendInt(b, int64(t.Day()), 10)
399 b = append(b, []byte{0x2f}...)
400 b = strconv.AppendInt(b, int64(t.Month()), 10)
401 b = append(b, []byte{0x2f}...)
402
403 if t.Year() > 0 {
404 b = strconv.AppendInt(b, int64(t.Year()), 10)
405 } else {
406 b = strconv.AppendInt(b, int64(-t.Year()), 10)
407 }
408
409 return string(b)
410 }
411
412
413 func (yav *yav) FmtDateMedium(t time.Time) string {
414
415 b := make([]byte, 0, 32)
416
417 b = strconv.AppendInt(b, int64(t.Day()), 10)
418 b = append(b, []byte{0x20}...)
419 b = append(b, yav.monthsAbbreviated[t.Month()]...)
420 b = append(b, []byte{0x20}...)
421
422 if t.Year() > 0 {
423 b = strconv.AppendInt(b, int64(t.Year()), 10)
424 } else {
425 b = strconv.AppendInt(b, int64(-t.Year()), 10)
426 }
427
428 return string(b)
429 }
430
431
432 func (yav *yav) FmtDateLong(t time.Time) string {
433
434 b := make([]byte, 0, 32)
435
436 b = strconv.AppendInt(b, int64(t.Day()), 10)
437 b = append(b, []byte{0x20}...)
438 b = append(b, yav.monthsWide[t.Month()]...)
439 b = append(b, []byte{0x20}...)
440
441 if t.Year() > 0 {
442 b = strconv.AppendInt(b, int64(t.Year()), 10)
443 } else {
444 b = strconv.AppendInt(b, int64(-t.Year()), 10)
445 }
446
447 return string(b)
448 }
449
450
451 func (yav *yav) FmtDateFull(t time.Time) string {
452
453 b := make([]byte, 0, 32)
454
455 b = append(b, yav.daysWide[t.Weekday()]...)
456 b = append(b, []byte{0x20}...)
457 b = strconv.AppendInt(b, int64(t.Day()), 10)
458 b = append(b, []byte{0x20}...)
459 b = append(b, yav.monthsWide[t.Month()]...)
460 b = append(b, []byte{0x20}...)
461
462 if t.Year() > 0 {
463 b = strconv.AppendInt(b, int64(t.Year()), 10)
464 } else {
465 b = strconv.AppendInt(b, int64(-t.Year()), 10)
466 }
467
468 return string(b)
469 }
470
471
472 func (yav *yav) FmtTimeShort(t time.Time) string {
473
474 b := make([]byte, 0, 32)
475
476 if t.Hour() < 10 {
477 b = append(b, '0')
478 }
479
480 b = strconv.AppendInt(b, int64(t.Hour()), 10)
481 b = append(b, yav.timeSeparator...)
482
483 if t.Minute() < 10 {
484 b = append(b, '0')
485 }
486
487 b = strconv.AppendInt(b, int64(t.Minute()), 10)
488
489 return string(b)
490 }
491
492
493 func (yav *yav) FmtTimeMedium(t time.Time) string {
494
495 b := make([]byte, 0, 32)
496
497 if t.Hour() < 10 {
498 b = append(b, '0')
499 }
500
501 b = strconv.AppendInt(b, int64(t.Hour()), 10)
502 b = append(b, yav.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, yav.timeSeparator...)
510
511 if t.Second() < 10 {
512 b = append(b, '0')
513 }
514
515 b = strconv.AppendInt(b, int64(t.Second()), 10)
516
517 return string(b)
518 }
519
520
521 func (yav *yav) FmtTimeLong(t time.Time) string {
522
523 b := make([]byte, 0, 32)
524
525 if t.Hour() < 10 {
526 b = append(b, '0')
527 }
528
529 b = strconv.AppendInt(b, int64(t.Hour()), 10)
530 b = append(b, yav.timeSeparator...)
531
532 if t.Minute() < 10 {
533 b = append(b, '0')
534 }
535
536 b = strconv.AppendInt(b, int64(t.Minute()), 10)
537 b = append(b, yav.timeSeparator...)
538
539 if t.Second() < 10 {
540 b = append(b, '0')
541 }
542
543 b = strconv.AppendInt(b, int64(t.Second()), 10)
544 b = append(b, []byte{0x20}...)
545
546 tz, _ := t.Zone()
547 b = append(b, tz...)
548
549 return string(b)
550 }
551
552
553 func (yav *yav) FmtTimeFull(t time.Time) string {
554
555 b := make([]byte, 0, 32)
556
557 if t.Hour() < 10 {
558 b = append(b, '0')
559 }
560
561 b = strconv.AppendInt(b, int64(t.Hour()), 10)
562 b = append(b, yav.timeSeparator...)
563
564 if t.Minute() < 10 {
565 b = append(b, '0')
566 }
567
568 b = strconv.AppendInt(b, int64(t.Minute()), 10)
569 b = append(b, yav.timeSeparator...)
570
571 if t.Second() < 10 {
572 b = append(b, '0')
573 }
574
575 b = strconv.AppendInt(b, int64(t.Second()), 10)
576 b = append(b, []byte{0x20}...)
577
578 tz, _ := t.Zone()
579
580 if btz, ok := yav.timezones[tz]; ok {
581 b = append(b, btz...)
582 } else {
583 b = append(b, tz...)
584 }
585
586 return string(b)
587 }
588
View as plain text