1 package smn
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 smn 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 percentSuffix string
22 perMille string
23 timeSeparator string
24 inifinity string
25 currencies []string
26 currencyPositiveSuffix 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 &smn{
48 locale: "smn",
49 pluralsCardinal: []locales.PluralRule{2, 3, 6},
50 pluralsOrdinal: nil,
51 pluralsRange: nil,
52 decimal: ",",
53 group: " ",
54 minus: "-",
55 percent: "%",
56 perMille: "‰",
57 timeSeparator: ".",
58 inifinity: "∞",
59 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"},
60 percentSuffix: " ",
61 currencyPositiveSuffix: " ",
62 currencyNegativeSuffix: " ",
63 monthsAbbreviated: []string{"", "uđiv", "kuovâ", "njuhčâ", "cuáŋui", "vyesi", "kesi", "syeini", "porge", "čohčâ", "roovvâd", "skammâ", "juovlâ"},
64 monthsNarrow: []string{"", "U", "K", "NJ", "C", "V", "K", "S", "P", "Č", "R", "S", "J"},
65 monthsWide: []string{"", "uđđâivemáánu", "kuovâmáánu", "njuhčâmáánu", "cuáŋuimáánu", "vyesimáánu", "kesimáánu", "syeinimáánu", "porgemáánu", "čohčâmáánu", "roovvâdmáánu", "skammâmáánu", "juovlâmáánu"},
66 daysAbbreviated: []string{"pas", "vuo", "maj", "kos", "tuo", "vás", "láv"},
67 daysNarrow: []string{"p", "V", "M", "K", "T", "V", "L"},
68 daysShort: []string{"pa", "vu", "ma", "ko", "tu", "vá", "lá"},
69 daysWide: []string{"pasepeeivi", "vuossaargâ", "majebaargâ", "koskoho", "tuorâstuv", "vástuppeeivi", "lávurduv"},
70 periodsAbbreviated: []string{"ip.", "ep."},
71 periodsNarrow: []string{"ip.", "ep."},
72 periodsWide: []string{"ip.", "ep."},
73 erasAbbreviated: []string{"oKr.", "mKr."},
74 erasNarrow: []string{"", ""},
75 erasWide: []string{"Ovdil Kristus šoddâm", "maŋa Kristus šoddâm"},
76 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", "∅∅∅": "∅∅∅"},
77 }
78 }
79
80
81 func (smn *smn) Locale() string {
82 return smn.locale
83 }
84
85
86 func (smn *smn) PluralsCardinal() []locales.PluralRule {
87 return smn.pluralsCardinal
88 }
89
90
91 func (smn *smn) PluralsOrdinal() []locales.PluralRule {
92 return smn.pluralsOrdinal
93 }
94
95
96 func (smn *smn) PluralsRange() []locales.PluralRule {
97 return smn.pluralsRange
98 }
99
100
101 func (smn *smn) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
102
103 n := math.Abs(num)
104
105 if n == 1 {
106 return locales.PluralRuleOne
107 } else if n == 2 {
108 return locales.PluralRuleTwo
109 }
110
111 return locales.PluralRuleOther
112 }
113
114
115 func (smn *smn) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
116 return locales.PluralRuleUnknown
117 }
118
119
120 func (smn *smn) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
121 return locales.PluralRuleUnknown
122 }
123
124
125 func (smn *smn) MonthAbbreviated(month time.Month) string {
126 return smn.monthsAbbreviated[month]
127 }
128
129
130 func (smn *smn) MonthsAbbreviated() []string {
131 return smn.monthsAbbreviated[1:]
132 }
133
134
135 func (smn *smn) MonthNarrow(month time.Month) string {
136 return smn.monthsNarrow[month]
137 }
138
139
140 func (smn *smn) MonthsNarrow() []string {
141 return smn.monthsNarrow[1:]
142 }
143
144
145 func (smn *smn) MonthWide(month time.Month) string {
146 return smn.monthsWide[month]
147 }
148
149
150 func (smn *smn) MonthsWide() []string {
151 return smn.monthsWide[1:]
152 }
153
154
155 func (smn *smn) WeekdayAbbreviated(weekday time.Weekday) string {
156 return smn.daysAbbreviated[weekday]
157 }
158
159
160 func (smn *smn) WeekdaysAbbreviated() []string {
161 return smn.daysAbbreviated
162 }
163
164
165 func (smn *smn) WeekdayNarrow(weekday time.Weekday) string {
166 return smn.daysNarrow[weekday]
167 }
168
169
170 func (smn *smn) WeekdaysNarrow() []string {
171 return smn.daysNarrow
172 }
173
174
175 func (smn *smn) WeekdayShort(weekday time.Weekday) string {
176 return smn.daysShort[weekday]
177 }
178
179
180 func (smn *smn) WeekdaysShort() []string {
181 return smn.daysShort
182 }
183
184
185 func (smn *smn) WeekdayWide(weekday time.Weekday) string {
186 return smn.daysWide[weekday]
187 }
188
189
190 func (smn *smn) WeekdaysWide() []string {
191 return smn.daysWide
192 }
193
194
195 func (smn *smn) Decimal() string {
196 return smn.decimal
197 }
198
199
200 func (smn *smn) Group() string {
201 return smn.group
202 }
203
204
205 func (smn *smn) Minus() string {
206 return smn.minus
207 }
208
209
210 func (smn *smn) FmtNumber(num float64, v uint64) string {
211
212 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
213 l := len(s) + 2 + 2*len(s[:len(s)-int(v)-1])/3
214 count := 0
215 inWhole := v == 0
216 b := make([]byte, 0, l)
217
218 for i := len(s) - 1; i >= 0; i-- {
219
220 if s[i] == '.' {
221 b = append(b, smn.decimal[0])
222 inWhole = true
223 continue
224 }
225
226 if inWhole {
227 if count == 3 {
228 for j := len(smn.group) - 1; j >= 0; j-- {
229 b = append(b, smn.group[j])
230 }
231 count = 1
232 } else {
233 count++
234 }
235 }
236
237 b = append(b, s[i])
238 }
239
240 if num < 0 {
241 b = append(b, smn.minus[0])
242 }
243
244
245 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
246 b[i], b[j] = b[j], b[i]
247 }
248
249 return string(b)
250 }
251
252
253
254 func (smn *smn) FmtPercent(num float64, v uint64) string {
255 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
256 l := len(s) + 5
257 b := make([]byte, 0, l)
258
259 for i := len(s) - 1; i >= 0; i-- {
260
261 if s[i] == '.' {
262 b = append(b, smn.decimal[0])
263 continue
264 }
265
266 b = append(b, s[i])
267 }
268
269 if num < 0 {
270 b = append(b, smn.minus[0])
271 }
272
273
274 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
275 b[i], b[j] = b[j], b[i]
276 }
277
278 b = append(b, smn.percentSuffix...)
279
280 b = append(b, smn.percent...)
281
282 return string(b)
283 }
284
285
286 func (smn *smn) FmtCurrency(num float64, v uint64, currency currency.Type) string {
287
288 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
289 symbol := smn.currencies[currency]
290 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
291 count := 0
292 inWhole := v == 0
293 b := make([]byte, 0, l)
294
295 for i := len(s) - 1; i >= 0; i-- {
296
297 if s[i] == '.' {
298 b = append(b, smn.decimal[0])
299 inWhole = true
300 continue
301 }
302
303 if inWhole {
304 if count == 3 {
305 for j := len(smn.group) - 1; j >= 0; j-- {
306 b = append(b, smn.group[j])
307 }
308 count = 1
309 } else {
310 count++
311 }
312 }
313
314 b = append(b, s[i])
315 }
316
317 if num < 0 {
318 b = append(b, smn.minus[0])
319 }
320
321
322 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
323 b[i], b[j] = b[j], b[i]
324 }
325
326 if int(v) < 2 {
327
328 if v == 0 {
329 b = append(b, smn.decimal...)
330 }
331
332 for i := 0; i < 2-int(v); i++ {
333 b = append(b, '0')
334 }
335 }
336
337 b = append(b, smn.currencyPositiveSuffix...)
338
339 b = append(b, symbol...)
340
341 return string(b)
342 }
343
344
345
346 func (smn *smn) FmtAccounting(num float64, v uint64, currency currency.Type) string {
347
348 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
349 symbol := smn.currencies[currency]
350 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
351 count := 0
352 inWhole := v == 0
353 b := make([]byte, 0, l)
354
355 for i := len(s) - 1; i >= 0; i-- {
356
357 if s[i] == '.' {
358 b = append(b, smn.decimal[0])
359 inWhole = true
360 continue
361 }
362
363 if inWhole {
364 if count == 3 {
365 for j := len(smn.group) - 1; j >= 0; j-- {
366 b = append(b, smn.group[j])
367 }
368 count = 1
369 } else {
370 count++
371 }
372 }
373
374 b = append(b, s[i])
375 }
376
377 if num < 0 {
378
379 b = append(b, smn.minus[0])
380
381 }
382
383
384 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
385 b[i], b[j] = b[j], b[i]
386 }
387
388 if int(v) < 2 {
389
390 if v == 0 {
391 b = append(b, smn.decimal...)
392 }
393
394 for i := 0; i < 2-int(v); i++ {
395 b = append(b, '0')
396 }
397 }
398
399 if num < 0 {
400 b = append(b, smn.currencyNegativeSuffix...)
401 b = append(b, symbol...)
402 } else {
403
404 b = append(b, smn.currencyPositiveSuffix...)
405 b = append(b, symbol...)
406 }
407
408 return string(b)
409 }
410
411
412 func (smn *smn) FmtDateShort(t time.Time) string {
413
414 b := make([]byte, 0, 32)
415
416 b = strconv.AppendInt(b, int64(t.Day()), 10)
417 b = append(b, []byte{0x2e}...)
418 b = strconv.AppendInt(b, int64(t.Month()), 10)
419 b = append(b, []byte{0x2e}...)
420
421 if t.Year() > 0 {
422 b = strconv.AppendInt(b, int64(t.Year()), 10)
423 } else {
424 b = strconv.AppendInt(b, int64(-t.Year()), 10)
425 }
426
427 return string(b)
428 }
429
430
431 func (smn *smn) FmtDateMedium(t time.Time) string {
432
433 b := make([]byte, 0, 32)
434
435 b = append(b, smn.monthsAbbreviated[t.Month()]...)
436 b = append(b, []byte{0x20}...)
437 b = strconv.AppendInt(b, int64(t.Day()), 10)
438 b = append(b, []byte{0x2e, 0x20}...)
439
440 if t.Year() > 0 {
441 b = strconv.AppendInt(b, int64(t.Year()), 10)
442 } else {
443 b = strconv.AppendInt(b, int64(-t.Year()), 10)
444 }
445
446 return string(b)
447 }
448
449
450 func (smn *smn) FmtDateLong(t time.Time) string {
451
452 b := make([]byte, 0, 32)
453
454 b = append(b, smn.monthsWide[t.Month()]...)
455 b = append(b, []byte{0x20}...)
456 b = strconv.AppendInt(b, int64(t.Day()), 10)
457 b = append(b, []byte{0x2e, 0x20}...)
458
459 if t.Year() > 0 {
460 b = strconv.AppendInt(b, int64(t.Year()), 10)
461 } else {
462 b = strconv.AppendInt(b, int64(-t.Year()), 10)
463 }
464
465 return string(b)
466 }
467
468
469 func (smn *smn) FmtDateFull(t time.Time) string {
470
471 b := make([]byte, 0, 32)
472
473 b = append(b, []byte{0x63, 0x63, 0x63, 0x63, 0x2c, 0x20}...)
474 b = append(b, smn.monthsWide[t.Month()]...)
475 b = append(b, []byte{0x20}...)
476 b = strconv.AppendInt(b, int64(t.Day()), 10)
477 b = append(b, []byte{0x2e, 0x20}...)
478
479 if t.Year() > 0 {
480 b = strconv.AppendInt(b, int64(t.Year()), 10)
481 } else {
482 b = strconv.AppendInt(b, int64(-t.Year()), 10)
483 }
484
485 return string(b)
486 }
487
488
489 func (smn *smn) FmtTimeShort(t time.Time) string {
490
491 b := make([]byte, 0, 32)
492
493 b = strconv.AppendInt(b, int64(t.Hour()), 10)
494 b = append(b, []byte{0x2e}...)
495
496 if t.Minute() < 10 {
497 b = append(b, '0')
498 }
499
500 b = strconv.AppendInt(b, int64(t.Minute()), 10)
501
502 return string(b)
503 }
504
505
506 func (smn *smn) FmtTimeMedium(t time.Time) string {
507
508 b := make([]byte, 0, 32)
509
510 b = strconv.AppendInt(b, int64(t.Hour()), 10)
511 b = append(b, []byte{0x2e}...)
512
513 if t.Minute() < 10 {
514 b = append(b, '0')
515 }
516
517 b = strconv.AppendInt(b, int64(t.Minute()), 10)
518 b = append(b, []byte{0x2e}...)
519
520 if t.Second() < 10 {
521 b = append(b, '0')
522 }
523
524 b = strconv.AppendInt(b, int64(t.Second()), 10)
525
526 return string(b)
527 }
528
529
530 func (smn *smn) FmtTimeLong(t time.Time) string {
531
532 b := make([]byte, 0, 32)
533
534 b = strconv.AppendInt(b, int64(t.Hour()), 10)
535 b = append(b, []byte{0x2e}...)
536
537 if t.Minute() < 10 {
538 b = append(b, '0')
539 }
540
541 b = strconv.AppendInt(b, int64(t.Minute()), 10)
542 b = append(b, []byte{0x2e}...)
543
544 if t.Second() < 10 {
545 b = append(b, '0')
546 }
547
548 b = strconv.AppendInt(b, int64(t.Second()), 10)
549 b = append(b, []byte{0x20}...)
550
551 tz, _ := t.Zone()
552 b = append(b, tz...)
553
554 return string(b)
555 }
556
557
558 func (smn *smn) FmtTimeFull(t time.Time) string {
559
560 b := make([]byte, 0, 32)
561
562 b = strconv.AppendInt(b, int64(t.Hour()), 10)
563 b = append(b, []byte{0x2e}...)
564
565 if t.Minute() < 10 {
566 b = append(b, '0')
567 }
568
569 b = strconv.AppendInt(b, int64(t.Minute()), 10)
570 b = append(b, []byte{0x2e}...)
571
572 if t.Second() < 10 {
573 b = append(b, '0')
574 }
575
576 b = strconv.AppendInt(b, int64(t.Second()), 10)
577 b = append(b, []byte{0x20}...)
578
579 tz, _ := t.Zone()
580
581 if btz, ok := smn.timezones[tz]; ok {
582 b = append(b, btz...)
583 } else {
584 b = append(b, tz...)
585 }
586
587 return string(b)
588 }
589
View as plain text