1 package sah
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 sah 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 &sah{
47 locale: "sah",
48 pluralsCardinal: []locales.PluralRule{6},
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", "CA$", "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", "MX$", "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", "₽", "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", "$", "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 periodsAbbreviated: []string{"ЭИ", "ЭК"},
69 periodsNarrow: []string{"ЭИ", "ЭК"},
70 periodsWide: []string{"ЭИ", "ЭК"},
71 erasAbbreviated: []string{"б. э. и.", "б. э"},
72 erasNarrow: []string{"", ""},
73 erasWide: []string{"б. э. и.", "б. э"},
74 timezones: map[string]string{"ACDT": "Киин Австралия сайыҥҥы кэмэ", "ACST": "Киин Австралия сүрүн кэмэ", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "ADT", "AEDT": "Илин Австралия сайыҥҥы кэмэ", "AEST": "Илин Австралия сүрүн кэмэ", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "AST", "AWDT": "Арҕаа Австралия сайыҥҥы кэмэ", "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": "Ииндийэ сүрүн кэмэ", "JDT": "Дьоппуон сайыҥҥы кэмэ", "JST": "Дьоппуон сүрүн кэмэ", "LHDT": "LHDT", "LHST": "LHST", "MDT": "MDT", "MESZ": "MESZ", "MEZ": "MEZ", "MST": "MST", "MYT": "MYT", "NZDT": "Саҥа Сэйлэнд сайыҥҥы кэмэ", "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 (sah *sah) Locale() string {
80 return sah.locale
81 }
82
83
84 func (sah *sah) PluralsCardinal() []locales.PluralRule {
85 return sah.pluralsCardinal
86 }
87
88
89 func (sah *sah) PluralsOrdinal() []locales.PluralRule {
90 return sah.pluralsOrdinal
91 }
92
93
94 func (sah *sah) PluralsRange() []locales.PluralRule {
95 return sah.pluralsRange
96 }
97
98
99 func (sah *sah) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
100 return locales.PluralRuleOther
101 }
102
103
104 func (sah *sah) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
105 return locales.PluralRuleUnknown
106 }
107
108
109 func (sah *sah) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
110 return locales.PluralRuleUnknown
111 }
112
113
114 func (sah *sah) MonthAbbreviated(month time.Month) string {
115 return sah.monthsAbbreviated[month]
116 }
117
118
119 func (sah *sah) MonthsAbbreviated() []string {
120 return sah.monthsAbbreviated[1:]
121 }
122
123
124 func (sah *sah) MonthNarrow(month time.Month) string {
125 return sah.monthsNarrow[month]
126 }
127
128
129 func (sah *sah) MonthsNarrow() []string {
130 return sah.monthsNarrow[1:]
131 }
132
133
134 func (sah *sah) MonthWide(month time.Month) string {
135 return sah.monthsWide[month]
136 }
137
138
139 func (sah *sah) MonthsWide() []string {
140 return sah.monthsWide[1:]
141 }
142
143
144 func (sah *sah) WeekdayAbbreviated(weekday time.Weekday) string {
145 return sah.daysAbbreviated[weekday]
146 }
147
148
149 func (sah *sah) WeekdaysAbbreviated() []string {
150 return sah.daysAbbreviated
151 }
152
153
154 func (sah *sah) WeekdayNarrow(weekday time.Weekday) string {
155 return sah.daysNarrow[weekday]
156 }
157
158
159 func (sah *sah) WeekdaysNarrow() []string {
160 return sah.daysNarrow
161 }
162
163
164 func (sah *sah) WeekdayShort(weekday time.Weekday) string {
165 return sah.daysShort[weekday]
166 }
167
168
169 func (sah *sah) WeekdaysShort() []string {
170 return sah.daysShort
171 }
172
173
174 func (sah *sah) WeekdayWide(weekday time.Weekday) string {
175 return sah.daysWide[weekday]
176 }
177
178
179 func (sah *sah) WeekdaysWide() []string {
180 return sah.daysWide
181 }
182
183
184 func (sah *sah) Decimal() string {
185 return sah.decimal
186 }
187
188
189 func (sah *sah) Group() string {
190 return sah.group
191 }
192
193
194 func (sah *sah) Minus() string {
195 return sah.minus
196 }
197
198
199 func (sah *sah) FmtNumber(num float64, v uint64) string {
200
201 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
202 l := len(s) + 2 + 2*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, sah.decimal[0])
211 inWhole = true
212 continue
213 }
214
215 if inWhole {
216 if count == 3 {
217 for j := len(sah.group) - 1; j >= 0; j-- {
218 b = append(b, sah.group[j])
219 }
220 count = 1
221 } else {
222 count++
223 }
224 }
225
226 b = append(b, s[i])
227 }
228
229 if num < 0 {
230 b = append(b, sah.minus[0])
231 }
232
233
234 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
235 b[i], b[j] = b[j], b[i]
236 }
237
238 return string(b)
239 }
240
241
242
243 func (sah *sah) FmtPercent(num float64, v uint64) string {
244 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
245 l := len(s) + 3
246 b := make([]byte, 0, l)
247
248 for i := len(s) - 1; i >= 0; i-- {
249
250 if s[i] == '.' {
251 b = append(b, sah.decimal[0])
252 continue
253 }
254
255 b = append(b, s[i])
256 }
257
258 if num < 0 {
259 b = append(b, sah.minus[0])
260 }
261
262
263 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
264 b[i], b[j] = b[j], b[i]
265 }
266
267 b = append(b, sah.percent...)
268
269 return string(b)
270 }
271
272
273 func (sah *sah) FmtCurrency(num float64, v uint64, currency currency.Type) string {
274
275 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
276 symbol := sah.currencies[currency]
277 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
278 count := 0
279 inWhole := v == 0
280 b := make([]byte, 0, l)
281
282 for i := len(s) - 1; i >= 0; i-- {
283
284 if s[i] == '.' {
285 b = append(b, sah.decimal[0])
286 inWhole = true
287 continue
288 }
289
290 if inWhole {
291 if count == 3 {
292 for j := len(sah.group) - 1; j >= 0; j-- {
293 b = append(b, sah.group[j])
294 }
295 count = 1
296 } else {
297 count++
298 }
299 }
300
301 b = append(b, s[i])
302 }
303
304 if num < 0 {
305 b = append(b, sah.minus[0])
306 }
307
308
309 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
310 b[i], b[j] = b[j], b[i]
311 }
312
313 if int(v) < 2 {
314
315 if v == 0 {
316 b = append(b, sah.decimal...)
317 }
318
319 for i := 0; i < 2-int(v); i++ {
320 b = append(b, '0')
321 }
322 }
323
324 b = append(b, sah.currencyPositiveSuffix...)
325
326 b = append(b, symbol...)
327
328 return string(b)
329 }
330
331
332
333 func (sah *sah) FmtAccounting(num float64, v uint64, currency currency.Type) string {
334
335 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
336 symbol := sah.currencies[currency]
337 l := len(s) + len(symbol) + 4 + 2*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, sah.decimal[0])
346 inWhole = true
347 continue
348 }
349
350 if inWhole {
351 if count == 3 {
352 for j := len(sah.group) - 1; j >= 0; j-- {
353 b = append(b, sah.group[j])
354 }
355 count = 1
356 } else {
357 count++
358 }
359 }
360
361 b = append(b, s[i])
362 }
363
364 if num < 0 {
365
366 b = append(b, sah.minus[0])
367
368 }
369
370
371 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
372 b[i], b[j] = b[j], b[i]
373 }
374
375 if int(v) < 2 {
376
377 if v == 0 {
378 b = append(b, sah.decimal...)
379 }
380
381 for i := 0; i < 2-int(v); i++ {
382 b = append(b, '0')
383 }
384 }
385
386 if num < 0 {
387 b = append(b, sah.currencyNegativeSuffix...)
388 b = append(b, symbol...)
389 } else {
390
391 b = append(b, sah.currencyPositiveSuffix...)
392 b = append(b, symbol...)
393 }
394
395 return string(b)
396 }
397
398
399 func (sah *sah) FmtDateShort(t time.Time) string {
400
401 b := make([]byte, 0, 32)
402
403 if t.Year() > 9 {
404 b = append(b, strconv.Itoa(t.Year())[2:]...)
405 } else {
406 b = append(b, strconv.Itoa(t.Year())[1:]...)
407 }
408
409 b = append(b, []byte{0x2f}...)
410 b = strconv.AppendInt(b, int64(t.Month()), 10)
411 b = append(b, []byte{0x2f}...)
412 b = strconv.AppendInt(b, int64(t.Day()), 10)
413
414 return string(b)
415 }
416
417
418 func (sah *sah) FmtDateMedium(t time.Time) string {
419
420 b := make([]byte, 0, 32)
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 b = append(b, []byte{0x2c, 0x20}...)
429 b = append(b, sah.monthsAbbreviated[t.Month()]...)
430 b = append(b, []byte{0x20}...)
431 b = strconv.AppendInt(b, int64(t.Day()), 10)
432
433 return string(b)
434 }
435
436
437 func (sah *sah) FmtDateLong(t time.Time) string {
438
439 b := make([]byte, 0, 32)
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 b = append(b, []byte{0x2c, 0x20}...)
448 b = append(b, sah.monthsWide[t.Month()]...)
449 b = append(b, []byte{0x20}...)
450 b = strconv.AppendInt(b, int64(t.Day()), 10)
451
452 return string(b)
453 }
454
455
456 func (sah *sah) FmtDateFull(t time.Time) string {
457
458 b := make([]byte, 0, 32)
459
460 if t.Year() > 0 {
461 b = strconv.AppendInt(b, int64(t.Year()), 10)
462 } else {
463 b = strconv.AppendInt(b, int64(-t.Year()), 10)
464 }
465
466 b = append(b, []byte{0x20, 0xd1, 0x81, 0xd1, 0x8b, 0xd0, 0xbb}...)
467 b = append(b, []byte{0x20}...)
468 b = append(b, sah.monthsWide[t.Month()]...)
469 b = append(b, []byte{0x20}...)
470 b = strconv.AppendInt(b, int64(t.Day()), 10)
471 b = append(b, []byte{0x20, 0xd0, 0xba, 0xd2, 0xaf, 0xd0, 0xbd, 0xd1, 0x8d}...)
472 b = append(b, []byte{0x2c, 0x20}...)
473 b = append(b, sah.daysWide[t.Weekday()]...)
474
475 return string(b)
476 }
477
478
479 func (sah *sah) FmtTimeShort(t time.Time) string {
480
481 b := make([]byte, 0, 32)
482
483 if t.Hour() < 10 {
484 b = append(b, '0')
485 }
486
487 b = strconv.AppendInt(b, int64(t.Hour()), 10)
488 b = append(b, sah.timeSeparator...)
489
490 if t.Minute() < 10 {
491 b = append(b, '0')
492 }
493
494 b = strconv.AppendInt(b, int64(t.Minute()), 10)
495
496 return string(b)
497 }
498
499
500 func (sah *sah) FmtTimeMedium(t time.Time) string {
501
502 b := make([]byte, 0, 32)
503
504 if t.Hour() < 10 {
505 b = append(b, '0')
506 }
507
508 b = strconv.AppendInt(b, int64(t.Hour()), 10)
509 b = append(b, sah.timeSeparator...)
510
511 if t.Minute() < 10 {
512 b = append(b, '0')
513 }
514
515 b = strconv.AppendInt(b, int64(t.Minute()), 10)
516 b = append(b, sah.timeSeparator...)
517
518 if t.Second() < 10 {
519 b = append(b, '0')
520 }
521
522 b = strconv.AppendInt(b, int64(t.Second()), 10)
523
524 return string(b)
525 }
526
527
528 func (sah *sah) FmtTimeLong(t time.Time) string {
529
530 b := make([]byte, 0, 32)
531
532 if t.Hour() < 10 {
533 b = append(b, '0')
534 }
535
536 b = strconv.AppendInt(b, int64(t.Hour()), 10)
537 b = append(b, sah.timeSeparator...)
538
539 if t.Minute() < 10 {
540 b = append(b, '0')
541 }
542
543 b = strconv.AppendInt(b, int64(t.Minute()), 10)
544 b = append(b, sah.timeSeparator...)
545
546 if t.Second() < 10 {
547 b = append(b, '0')
548 }
549
550 b = strconv.AppendInt(b, int64(t.Second()), 10)
551 b = append(b, []byte{0x20}...)
552
553 tz, _ := t.Zone()
554 b = append(b, tz...)
555
556 return string(b)
557 }
558
559
560 func (sah *sah) FmtTimeFull(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, sah.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, sah.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
587 if btz, ok := sah.timezones[tz]; ok {
588 b = append(b, btz...)
589 } else {
590 b = append(b, tz...)
591 }
592
593 return string(b)
594 }
595
View as plain text