1 package tg
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 tg 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 &tg{
47 locale: "tg",
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", "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", "₹", "IQD", "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", "сом.", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "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 currencyPositiveSuffix: " ",
60 currencyNegativeSuffix: " ",
61 monthsAbbreviated: []string{"", "Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"},
62 monthsNarrow: []string{"", "Я", "Ф", "М", "А", "М", "И", "И", "А", "С", "О", "Н", "Д"},
63 monthsWide: []string{"", "Январ", "Феврал", "Март", "Апрел", "Май", "Июн", "Июл", "Август", "Сентябр", "Октябр", "Ноябр", "Декабр"},
64 daysAbbreviated: []string{"Яшб", "Дшб", "Сшб", "Чшб", "Пшб", "Ҷмъ", "Шнб"},
65 daysNarrow: []string{"Я", "Д", "С", "Ч", "П", "Ҷ", "Ш"},
66 daysShort: []string{"Яшб", "Дшб", "Сшб", "Чшб", "Пшб", "Ҷмъ", "Шнб"},
67 daysWide: []string{"Якшанбе", "Душанбе", "Сешанбе", "Чоршанбе", "Панҷшанбе", "Ҷумъа", "Шанбе"},
68 periodsWide: []string{"AM", "PM"},
69 erasAbbreviated: []string{"ПеМ", "ПаМ"},
70 erasNarrow: []string{"", ""},
71 erasWide: []string{"Пеш аз милод", "ПаМ"},
72 timezones: map[string]string{"ACDT": "ACDT", "ACST": "ACST", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "Вақти рӯзонаи атлантикӣ", "AEDT": "AEDT", "AEST": "AEST", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "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": "Вақти рӯзонаи шарқӣ", "EST": "Вақти стандартии шарқӣ", "GFT": "GFT", "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": "Вақти рӯзонаи кӯҳӣ", "MESZ": "Вақти тобистонаи аврупоии марказӣ", "MEZ": "Вақти стандартии аврупоии марказӣ", "MST": "Вақти стандартии кӯҳӣ", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "Вақти тобистонаи аврупоии шарқӣ", "OEZ": "Вақти стандартии аврупоии шарқӣ", "PDT": "Вақти рӯзонаи Уқёнуси Ором", "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": "Вақти тобистонаи аврупоии ғарбӣ", "WEZ": "Вақти стандартии аврупоии ғарбӣ", "WIB": "WIB", "WIT": "WIT", "WITA": "WITA", "∅∅∅": "∅∅∅"},
73 }
74 }
75
76
77 func (tg *tg) Locale() string {
78 return tg.locale
79 }
80
81
82 func (tg *tg) PluralsCardinal() []locales.PluralRule {
83 return tg.pluralsCardinal
84 }
85
86
87 func (tg *tg) PluralsOrdinal() []locales.PluralRule {
88 return tg.pluralsOrdinal
89 }
90
91
92 func (tg *tg) PluralsRange() []locales.PluralRule {
93 return tg.pluralsRange
94 }
95
96
97 func (tg *tg) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
98 return locales.PluralRuleUnknown
99 }
100
101
102 func (tg *tg) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
103 return locales.PluralRuleUnknown
104 }
105
106
107 func (tg *tg) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
108 return locales.PluralRuleUnknown
109 }
110
111
112 func (tg *tg) MonthAbbreviated(month time.Month) string {
113 return tg.monthsAbbreviated[month]
114 }
115
116
117 func (tg *tg) MonthsAbbreviated() []string {
118 return tg.monthsAbbreviated[1:]
119 }
120
121
122 func (tg *tg) MonthNarrow(month time.Month) string {
123 return tg.monthsNarrow[month]
124 }
125
126
127 func (tg *tg) MonthsNarrow() []string {
128 return tg.monthsNarrow[1:]
129 }
130
131
132 func (tg *tg) MonthWide(month time.Month) string {
133 return tg.monthsWide[month]
134 }
135
136
137 func (tg *tg) MonthsWide() []string {
138 return tg.monthsWide[1:]
139 }
140
141
142 func (tg *tg) WeekdayAbbreviated(weekday time.Weekday) string {
143 return tg.daysAbbreviated[weekday]
144 }
145
146
147 func (tg *tg) WeekdaysAbbreviated() []string {
148 return tg.daysAbbreviated
149 }
150
151
152 func (tg *tg) WeekdayNarrow(weekday time.Weekday) string {
153 return tg.daysNarrow[weekday]
154 }
155
156
157 func (tg *tg) WeekdaysNarrow() []string {
158 return tg.daysNarrow
159 }
160
161
162 func (tg *tg) WeekdayShort(weekday time.Weekday) string {
163 return tg.daysShort[weekday]
164 }
165
166
167 func (tg *tg) WeekdaysShort() []string {
168 return tg.daysShort
169 }
170
171
172 func (tg *tg) WeekdayWide(weekday time.Weekday) string {
173 return tg.daysWide[weekday]
174 }
175
176
177 func (tg *tg) WeekdaysWide() []string {
178 return tg.daysWide
179 }
180
181
182 func (tg *tg) Decimal() string {
183 return tg.decimal
184 }
185
186
187 func (tg *tg) Group() string {
188 return tg.group
189 }
190
191
192 func (tg *tg) Minus() string {
193 return tg.minus
194 }
195
196
197 func (tg *tg) FmtNumber(num float64, v uint64) string {
198
199 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
200 l := len(s) + 2 + 2*len(s[:len(s)-int(v)-1])/3
201 count := 0
202 inWhole := v == 0
203 b := make([]byte, 0, l)
204
205 for i := len(s) - 1; i >= 0; i-- {
206
207 if s[i] == '.' {
208 b = append(b, tg.decimal[0])
209 inWhole = true
210 continue
211 }
212
213 if inWhole {
214 if count == 3 {
215 for j := len(tg.group) - 1; j >= 0; j-- {
216 b = append(b, tg.group[j])
217 }
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, tg.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 (tg *tg) 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, tg.decimal[0])
250 continue
251 }
252
253 b = append(b, s[i])
254 }
255
256 if num < 0 {
257 b = append(b, tg.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, tg.percent...)
266
267 return string(b)
268 }
269
270
271 func (tg *tg) FmtCurrency(num float64, v uint64, currency currency.Type) string {
272
273 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
274 symbol := tg.currencies[currency]
275 l := len(s) + len(symbol) + 4 + 2*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, tg.decimal[0])
284 inWhole = true
285 continue
286 }
287
288 if inWhole {
289 if count == 3 {
290 for j := len(tg.group) - 1; j >= 0; j-- {
291 b = append(b, tg.group[j])
292 }
293 count = 1
294 } else {
295 count++
296 }
297 }
298
299 b = append(b, s[i])
300 }
301
302 if num < 0 {
303 b = append(b, tg.minus[0])
304 }
305
306
307 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
308 b[i], b[j] = b[j], b[i]
309 }
310
311 if int(v) < 2 {
312
313 if v == 0 {
314 b = append(b, tg.decimal...)
315 }
316
317 for i := 0; i < 2-int(v); i++ {
318 b = append(b, '0')
319 }
320 }
321
322 b = append(b, tg.currencyPositiveSuffix...)
323
324 b = append(b, symbol...)
325
326 return string(b)
327 }
328
329
330
331 func (tg *tg) FmtAccounting(num float64, v uint64, currency currency.Type) string {
332
333 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
334 symbol := tg.currencies[currency]
335 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
336 count := 0
337 inWhole := v == 0
338 b := make([]byte, 0, l)
339
340 for i := len(s) - 1; i >= 0; i-- {
341
342 if s[i] == '.' {
343 b = append(b, tg.decimal[0])
344 inWhole = true
345 continue
346 }
347
348 if inWhole {
349 if count == 3 {
350 for j := len(tg.group) - 1; j >= 0; j-- {
351 b = append(b, tg.group[j])
352 }
353 count = 1
354 } else {
355 count++
356 }
357 }
358
359 b = append(b, s[i])
360 }
361
362 if num < 0 {
363
364 b = append(b, tg.minus[0])
365
366 }
367
368
369 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
370 b[i], b[j] = b[j], b[i]
371 }
372
373 if int(v) < 2 {
374
375 if v == 0 {
376 b = append(b, tg.decimal...)
377 }
378
379 for i := 0; i < 2-int(v); i++ {
380 b = append(b, '0')
381 }
382 }
383
384 if num < 0 {
385 b = append(b, tg.currencyNegativeSuffix...)
386 b = append(b, symbol...)
387 } else {
388
389 b = append(b, tg.currencyPositiveSuffix...)
390 b = append(b, symbol...)
391 }
392
393 return string(b)
394 }
395
396
397 func (tg *tg) FmtDateShort(t time.Time) string {
398
399 b := make([]byte, 0, 32)
400
401 if t.Day() < 10 {
402 b = append(b, '0')
403 }
404
405 b = strconv.AppendInt(b, int64(t.Day()), 10)
406 b = append(b, []byte{0x2f}...)
407
408 if t.Month() < 10 {
409 b = append(b, '0')
410 }
411
412 b = strconv.AppendInt(b, int64(t.Month()), 10)
413
414 b = append(b, []byte{0x2f}...)
415
416 if t.Year() > 9 {
417 b = append(b, strconv.Itoa(t.Year())[2:]...)
418 } else {
419 b = append(b, strconv.Itoa(t.Year())[1:]...)
420 }
421
422 return string(b)
423 }
424
425
426 func (tg *tg) FmtDateMedium(t time.Time) string {
427
428 b := make([]byte, 0, 32)
429
430 if t.Day() < 10 {
431 b = append(b, '0')
432 }
433
434 b = strconv.AppendInt(b, int64(t.Day()), 10)
435 b = append(b, []byte{0x20}...)
436 b = append(b, tg.monthsAbbreviated[t.Month()]...)
437 b = append(b, []byte{0x20}...)
438
439 if t.Year() > 0 {
440 b = strconv.AppendInt(b, int64(t.Year()), 10)
441 } else {
442 b = strconv.AppendInt(b, int64(-t.Year()), 10)
443 }
444
445 return string(b)
446 }
447
448
449 func (tg *tg) FmtDateLong(t time.Time) string {
450
451 b := make([]byte, 0, 32)
452
453 if t.Day() < 10 {
454 b = append(b, '0')
455 }
456
457 b = strconv.AppendInt(b, int64(t.Day()), 10)
458 b = append(b, []byte{0x20}...)
459 b = append(b, tg.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 (tg *tg) FmtDateFull(t time.Time) string {
473
474 b := make([]byte, 0, 32)
475
476 b = append(b, tg.daysWide[t.Weekday()]...)
477 b = append(b, []byte{0x2c, 0x20}...)
478
479 if t.Day() < 10 {
480 b = append(b, '0')
481 }
482
483 b = strconv.AppendInt(b, int64(t.Day()), 10)
484 b = append(b, []byte{0x20}...)
485 b = append(b, tg.monthsWide[t.Month()]...)
486 b = append(b, []byte{0x20}...)
487
488 if t.Year() > 0 {
489 b = strconv.AppendInt(b, int64(t.Year()), 10)
490 } else {
491 b = strconv.AppendInt(b, int64(-t.Year()), 10)
492 }
493
494 return string(b)
495 }
496
497
498 func (tg *tg) FmtTimeShort(t time.Time) string {
499
500 b := make([]byte, 0, 32)
501
502 if t.Hour() < 10 {
503 b = append(b, '0')
504 }
505
506 b = strconv.AppendInt(b, int64(t.Hour()), 10)
507 b = append(b, tg.timeSeparator...)
508
509 if t.Minute() < 10 {
510 b = append(b, '0')
511 }
512
513 b = strconv.AppendInt(b, int64(t.Minute()), 10)
514
515 return string(b)
516 }
517
518
519 func (tg *tg) FmtTimeMedium(t time.Time) string {
520
521 b := make([]byte, 0, 32)
522
523 if t.Hour() < 10 {
524 b = append(b, '0')
525 }
526
527 b = strconv.AppendInt(b, int64(t.Hour()), 10)
528 b = append(b, tg.timeSeparator...)
529
530 if t.Minute() < 10 {
531 b = append(b, '0')
532 }
533
534 b = strconv.AppendInt(b, int64(t.Minute()), 10)
535 b = append(b, tg.timeSeparator...)
536
537 if t.Second() < 10 {
538 b = append(b, '0')
539 }
540
541 b = strconv.AppendInt(b, int64(t.Second()), 10)
542
543 return string(b)
544 }
545
546
547 func (tg *tg) FmtTimeLong(t time.Time) string {
548
549 b := make([]byte, 0, 32)
550
551 if t.Hour() < 10 {
552 b = append(b, '0')
553 }
554
555 b = strconv.AppendInt(b, int64(t.Hour()), 10)
556 b = append(b, tg.timeSeparator...)
557
558 if t.Minute() < 10 {
559 b = append(b, '0')
560 }
561
562 b = strconv.AppendInt(b, int64(t.Minute()), 10)
563 b = append(b, tg.timeSeparator...)
564
565 if t.Second() < 10 {
566 b = append(b, '0')
567 }
568
569 b = strconv.AppendInt(b, int64(t.Second()), 10)
570 b = append(b, []byte{0x20}...)
571
572 tz, _ := t.Zone()
573 b = append(b, tz...)
574
575 return string(b)
576 }
577
578
579 func (tg *tg) FmtTimeFull(t time.Time) string {
580
581 b := make([]byte, 0, 32)
582
583 if t.Hour() < 10 {
584 b = append(b, '0')
585 }
586
587 b = strconv.AppendInt(b, int64(t.Hour()), 10)
588 b = append(b, tg.timeSeparator...)
589
590 if t.Minute() < 10 {
591 b = append(b, '0')
592 }
593
594 b = strconv.AppendInt(b, int64(t.Minute()), 10)
595 b = append(b, tg.timeSeparator...)
596
597 if t.Second() < 10 {
598 b = append(b, '0')
599 }
600
601 b = strconv.AppendInt(b, int64(t.Second()), 10)
602 b = append(b, []byte{0x20}...)
603
604 tz, _ := t.Zone()
605
606 if btz, ok := tg.timezones[tz]; ok {
607 b = append(b, btz...)
608 } else {
609 b = append(b, tz...)
610 }
611
612 return string(b)
613 }
614
View as plain text