1 package sg
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 sg 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 monthsAbbreviated []string
26 monthsNarrow []string
27 monthsWide []string
28 daysAbbreviated []string
29 daysNarrow []string
30 daysShort []string
31 daysWide []string
32 periodsAbbreviated []string
33 periodsNarrow []string
34 periodsShort []string
35 periodsWide []string
36 erasAbbreviated []string
37 erasNarrow []string
38 erasWide []string
39 timezones map[string]string
40 }
41
42
43 func New() locales.Translator {
44 return &sg{
45 locale: "sg",
46 pluralsCardinal: []locales.PluralRule{6},
47 pluralsOrdinal: nil,
48 pluralsRange: nil,
49 decimal: ",",
50 group: ".",
51 timeSeparator: ":",
52 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"},
53 monthsAbbreviated: []string{"", "Nye", "Ful", "Mbä", "Ngu", "Bêl", "Fön", "Len", "Kük", "Mvu", "Ngb", "Nab", "Kak"},
54 monthsNarrow: []string{"", "N", "F", "M", "N", "B", "F", "L", "K", "M", "N", "N", "K"},
55 monthsWide: []string{"", "Nyenye", "Fulundïgi", "Mbängü", "Ngubùe", "Bêläwü", "Föndo", "Lengua", "Kükürü", "Mvuka", "Ngberere", "Nabändüru", "Kakauka"},
56 daysAbbreviated: []string{"Bk1", "Bk2", "Bk3", "Bk4", "Bk5", "Lâp", "Lây"},
57 daysNarrow: []string{"K", "S", "T", "S", "K", "P", "Y"},
58 daysWide: []string{"Bikua-ôko", "Bïkua-ûse", "Bïkua-ptâ", "Bïkua-usïö", "Bïkua-okü", "Lâpôsö", "Lâyenga"},
59 periodsAbbreviated: []string{"ND", "LK"},
60 periodsWide: []string{"ND", "LK"},
61 erasAbbreviated: []string{"KnK", "NpK"},
62 erasNarrow: []string{"", ""},
63 erasWide: []string{"Kôzo na Krîstu", "Na pekô tî Krîstu"},
64 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", "∅∅∅": "∅∅∅"},
65 }
66 }
67
68
69 func (sg *sg) Locale() string {
70 return sg.locale
71 }
72
73
74 func (sg *sg) PluralsCardinal() []locales.PluralRule {
75 return sg.pluralsCardinal
76 }
77
78
79 func (sg *sg) PluralsOrdinal() []locales.PluralRule {
80 return sg.pluralsOrdinal
81 }
82
83
84 func (sg *sg) PluralsRange() []locales.PluralRule {
85 return sg.pluralsRange
86 }
87
88
89 func (sg *sg) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
90 return locales.PluralRuleOther
91 }
92
93
94 func (sg *sg) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
95 return locales.PluralRuleUnknown
96 }
97
98
99 func (sg *sg) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
100 return locales.PluralRuleUnknown
101 }
102
103
104 func (sg *sg) MonthAbbreviated(month time.Month) string {
105 return sg.monthsAbbreviated[month]
106 }
107
108
109 func (sg *sg) MonthsAbbreviated() []string {
110 return sg.monthsAbbreviated[1:]
111 }
112
113
114 func (sg *sg) MonthNarrow(month time.Month) string {
115 return sg.monthsNarrow[month]
116 }
117
118
119 func (sg *sg) MonthsNarrow() []string {
120 return sg.monthsNarrow[1:]
121 }
122
123
124 func (sg *sg) MonthWide(month time.Month) string {
125 return sg.monthsWide[month]
126 }
127
128
129 func (sg *sg) MonthsWide() []string {
130 return sg.monthsWide[1:]
131 }
132
133
134 func (sg *sg) WeekdayAbbreviated(weekday time.Weekday) string {
135 return sg.daysAbbreviated[weekday]
136 }
137
138
139 func (sg *sg) WeekdaysAbbreviated() []string {
140 return sg.daysAbbreviated
141 }
142
143
144 func (sg *sg) WeekdayNarrow(weekday time.Weekday) string {
145 return sg.daysNarrow[weekday]
146 }
147
148
149 func (sg *sg) WeekdaysNarrow() []string {
150 return sg.daysNarrow
151 }
152
153
154 func (sg *sg) WeekdayShort(weekday time.Weekday) string {
155 return sg.daysShort[weekday]
156 }
157
158
159 func (sg *sg) WeekdaysShort() []string {
160 return sg.daysShort
161 }
162
163
164 func (sg *sg) WeekdayWide(weekday time.Weekday) string {
165 return sg.daysWide[weekday]
166 }
167
168
169 func (sg *sg) WeekdaysWide() []string {
170 return sg.daysWide
171 }
172
173
174 func (sg *sg) Decimal() string {
175 return sg.decimal
176 }
177
178
179 func (sg *sg) Group() string {
180 return sg.group
181 }
182
183
184 func (sg *sg) Minus() string {
185 return sg.minus
186 }
187
188
189 func (sg *sg) FmtNumber(num float64, v uint64) string {
190
191 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
192 }
193
194
195
196 func (sg *sg) FmtPercent(num float64, v uint64) string {
197 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
198 }
199
200
201 func (sg *sg) FmtCurrency(num float64, v uint64, currency currency.Type) string {
202
203 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
204 symbol := sg.currencies[currency]
205 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
206 count := 0
207 inWhole := v == 0
208 b := make([]byte, 0, l)
209
210 for i := len(s) - 1; i >= 0; i-- {
211
212 if s[i] == '.' {
213 b = append(b, sg.decimal[0])
214 inWhole = true
215 continue
216 }
217
218 if inWhole {
219 if count == 3 {
220 b = append(b, sg.group[0])
221 count = 1
222 } else {
223 count++
224 }
225 }
226
227 b = append(b, s[i])
228 }
229
230 for j := len(symbol) - 1; j >= 0; j-- {
231 b = append(b, symbol[j])
232 }
233
234 if num < 0 {
235 b = append(b, sg.minus[0])
236 }
237
238
239 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
240 b[i], b[j] = b[j], b[i]
241 }
242
243 if int(v) < 2 {
244
245 if v == 0 {
246 b = append(b, sg.decimal...)
247 }
248
249 for i := 0; i < 2-int(v); i++ {
250 b = append(b, '0')
251 }
252 }
253
254 return string(b)
255 }
256
257
258
259 func (sg *sg) FmtAccounting(num float64, v uint64, currency currency.Type) string {
260
261 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
262 symbol := sg.currencies[currency]
263 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
264 count := 0
265 inWhole := v == 0
266 b := make([]byte, 0, l)
267
268 for i := len(s) - 1; i >= 0; i-- {
269
270 if s[i] == '.' {
271 b = append(b, sg.decimal[0])
272 inWhole = true
273 continue
274 }
275
276 if inWhole {
277 if count == 3 {
278 b = append(b, sg.group[0])
279 count = 1
280 } else {
281 count++
282 }
283 }
284
285 b = append(b, s[i])
286 }
287
288 if num < 0 {
289
290 for j := len(symbol) - 1; j >= 0; j-- {
291 b = append(b, symbol[j])
292 }
293
294 b = append(b, sg.minus[0])
295
296 } else {
297
298 for j := len(symbol) - 1; j >= 0; j-- {
299 b = append(b, symbol[j])
300 }
301
302 }
303
304
305 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
306 b[i], b[j] = b[j], b[i]
307 }
308
309 if int(v) < 2 {
310
311 if v == 0 {
312 b = append(b, sg.decimal...)
313 }
314
315 for i := 0; i < 2-int(v); i++ {
316 b = append(b, '0')
317 }
318 }
319
320 return string(b)
321 }
322
323
324 func (sg *sg) FmtDateShort(t time.Time) string {
325
326 b := make([]byte, 0, 32)
327
328 b = strconv.AppendInt(b, int64(t.Day()), 10)
329 b = append(b, []byte{0x2f}...)
330 b = strconv.AppendInt(b, int64(t.Month()), 10)
331 b = append(b, []byte{0x2f}...)
332
333 if t.Year() > 0 {
334 b = strconv.AppendInt(b, int64(t.Year()), 10)
335 } else {
336 b = strconv.AppendInt(b, int64(-t.Year()), 10)
337 }
338
339 return string(b)
340 }
341
342
343 func (sg *sg) FmtDateMedium(t time.Time) string {
344
345 b := make([]byte, 0, 32)
346
347 b = strconv.AppendInt(b, int64(t.Day()), 10)
348 b = append(b, []byte{0x20}...)
349 b = append(b, sg.monthsAbbreviated[t.Month()]...)
350 b = append(b, []byte{0x2c, 0x20}...)
351
352 if t.Year() > 0 {
353 b = strconv.AppendInt(b, int64(t.Year()), 10)
354 } else {
355 b = strconv.AppendInt(b, int64(-t.Year()), 10)
356 }
357
358 return string(b)
359 }
360
361
362 func (sg *sg) FmtDateLong(t time.Time) string {
363
364 b := make([]byte, 0, 32)
365
366 b = strconv.AppendInt(b, int64(t.Day()), 10)
367 b = append(b, []byte{0x20}...)
368 b = append(b, sg.monthsWide[t.Month()]...)
369 b = append(b, []byte{0x20}...)
370
371 if t.Year() > 0 {
372 b = strconv.AppendInt(b, int64(t.Year()), 10)
373 } else {
374 b = strconv.AppendInt(b, int64(-t.Year()), 10)
375 }
376
377 return string(b)
378 }
379
380
381 func (sg *sg) FmtDateFull(t time.Time) string {
382
383 b := make([]byte, 0, 32)
384
385 b = append(b, sg.daysWide[t.Weekday()]...)
386 b = append(b, []byte{0x20}...)
387 b = strconv.AppendInt(b, int64(t.Day()), 10)
388 b = append(b, []byte{0x20}...)
389 b = append(b, sg.monthsWide[t.Month()]...)
390 b = append(b, []byte{0x20}...)
391
392 if t.Year() > 0 {
393 b = strconv.AppendInt(b, int64(t.Year()), 10)
394 } else {
395 b = strconv.AppendInt(b, int64(-t.Year()), 10)
396 }
397
398 return string(b)
399 }
400
401
402 func (sg *sg) FmtTimeShort(t time.Time) string {
403
404 b := make([]byte, 0, 32)
405
406 if t.Hour() < 10 {
407 b = append(b, '0')
408 }
409
410 b = strconv.AppendInt(b, int64(t.Hour()), 10)
411 b = append(b, sg.timeSeparator...)
412
413 if t.Minute() < 10 {
414 b = append(b, '0')
415 }
416
417 b = strconv.AppendInt(b, int64(t.Minute()), 10)
418
419 return string(b)
420 }
421
422
423 func (sg *sg) FmtTimeMedium(t time.Time) string {
424
425 b := make([]byte, 0, 32)
426
427 if t.Hour() < 10 {
428 b = append(b, '0')
429 }
430
431 b = strconv.AppendInt(b, int64(t.Hour()), 10)
432 b = append(b, sg.timeSeparator...)
433
434 if t.Minute() < 10 {
435 b = append(b, '0')
436 }
437
438 b = strconv.AppendInt(b, int64(t.Minute()), 10)
439 b = append(b, sg.timeSeparator...)
440
441 if t.Second() < 10 {
442 b = append(b, '0')
443 }
444
445 b = strconv.AppendInt(b, int64(t.Second()), 10)
446
447 return string(b)
448 }
449
450
451 func (sg *sg) FmtTimeLong(t time.Time) string {
452
453 b := make([]byte, 0, 32)
454
455 if t.Hour() < 10 {
456 b = append(b, '0')
457 }
458
459 b = strconv.AppendInt(b, int64(t.Hour()), 10)
460 b = append(b, sg.timeSeparator...)
461
462 if t.Minute() < 10 {
463 b = append(b, '0')
464 }
465
466 b = strconv.AppendInt(b, int64(t.Minute()), 10)
467 b = append(b, sg.timeSeparator...)
468
469 if t.Second() < 10 {
470 b = append(b, '0')
471 }
472
473 b = strconv.AppendInt(b, int64(t.Second()), 10)
474 b = append(b, []byte{0x20}...)
475
476 tz, _ := t.Zone()
477 b = append(b, tz...)
478
479 return string(b)
480 }
481
482
483 func (sg *sg) FmtTimeFull(t time.Time) string {
484
485 b := make([]byte, 0, 32)
486
487 if t.Hour() < 10 {
488 b = append(b, '0')
489 }
490
491 b = strconv.AppendInt(b, int64(t.Hour()), 10)
492 b = append(b, sg.timeSeparator...)
493
494 if t.Minute() < 10 {
495 b = append(b, '0')
496 }
497
498 b = strconv.AppendInt(b, int64(t.Minute()), 10)
499 b = append(b, sg.timeSeparator...)
500
501 if t.Second() < 10 {
502 b = append(b, '0')
503 }
504
505 b = strconv.AppendInt(b, int64(t.Second()), 10)
506 b = append(b, []byte{0x20}...)
507
508 tz, _ := t.Zone()
509
510 if btz, ok := sg.timezones[tz]; ok {
511 b = append(b, btz...)
512 } else {
513 b = append(b, tz...)
514 }
515
516 return string(b)
517 }
518
View as plain text