1 package zgh
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 zgh 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 monthsAbbreviated []string
27 monthsNarrow []string
28 monthsWide []string
29 daysAbbreviated []string
30 daysNarrow []string
31 daysShort []string
32 daysWide []string
33 periodsAbbreviated []string
34 periodsNarrow []string
35 periodsShort []string
36 periodsWide []string
37 erasAbbreviated []string
38 erasNarrow []string
39 erasWide []string
40 timezones map[string]string
41 }
42
43
44 func New() locales.Translator {
45 return &zgh{
46 locale: "zgh",
47 pluralsCardinal: nil,
48 pluralsOrdinal: nil,
49 pluralsRange: nil,
50 decimal: ",",
51 group: " ",
52 minus: "-",
53 percent: "%",
54 perMille: "‰",
55 timeSeparator: ":",
56 inifinity: "∞",
57 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"},
58 percentSuffix: " ",
59 monthsAbbreviated: []string{"", "ⵉⵏⵏ", "ⴱⵕⴰ", "ⵎⴰⵕ", "ⵉⴱⵔ", "ⵎⴰⵢ", "ⵢⵓⵏ", "ⵢⵓⵍ", "ⵖⵓⵛ", "ⵛⵓⵜ", "ⴽⵜⵓ", "ⵏⵓⵡ", "ⴷⵓⵊ"},
60 monthsNarrow: []string{"", "ⵉ", "ⴱ", "ⵎ", "ⵉ", "ⵎ", "ⵢ", "ⵢ", "ⵖ", "ⵛ", "ⴽ", "ⵏ", "ⴷ"},
61 monthsWide: []string{"", "ⵉⵏⵏⴰⵢⵔ", "ⴱⵕⴰⵢⵕ", "ⵎⴰⵕⵚ", "ⵉⴱⵔⵉⵔ", "ⵎⴰⵢⵢⵓ", "ⵢⵓⵏⵢⵓ", "ⵢⵓⵍⵢⵓⵣ", "ⵖⵓⵛⵜ", "ⵛⵓⵜⴰⵏⴱⵉⵔ", "ⴽⵜⵓⴱⵔ", "ⵏⵓⵡⴰⵏⴱⵉⵔ", "ⴷⵓⵊⴰⵏⴱⵉⵔ"},
62 daysAbbreviated: []string{"ⴰⵙⴰ", "ⴰⵢⵏ", "ⴰⵙⵉ", "ⴰⴽⵕ", "ⴰⴽⵡ", "ⴰⵙⵉⵎ", "ⴰⵙⵉⴹ"},
63 daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
64 daysShort: []string{"ⴰⵙⴰ", "ⴰⵢⵏ", "ⴰⵙⵉ", "ⴰⴽⵕ", "ⴰⴽⵡ", "ⴰⵙⵉⵎ", "ⴰⵙⵉⴹ"},
65 daysWide: []string{"ⴰⵙⴰⵎⴰⵙ", "ⴰⵢⵏⴰⵙ", "ⴰⵙⵉⵏⴰⵙ", "ⴰⴽⵕⴰⵙ", "ⴰⴽⵡⴰⵙ", "ⴰⵙⵉⵎⵡⴰⵙ", "ⴰⵙⵉⴹⵢⴰⵙ"},
66 periodsAbbreviated: []string{"ⵜⵉⴼⴰⵡⵜ", "ⵜⴰⴷⴳⴳⵯⴰⵜ"},
67 periodsNarrow: []string{"ⵜⵉⴼⴰⵡⵜ", "ⵜⴰⴷⴳⴳⵯⴰⵜ"},
68 periodsWide: []string{"ⵜⵉⴼⴰⵡⵜ", "ⵜⴰⴷⴳⴳⵯⴰⵜ"},
69 erasAbbreviated: []string{"ⴷⴰⵄ", "ⴷⴼⵄ"},
70 erasNarrow: []string{"", ""},
71 erasWide: []string{"ⴷⴰⵜ ⵏ ⵄⵉⵙⴰ", "ⴷⴼⴼⵉⵔ ⵏ ⵄⵉⵙⴰ"},
72 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", "∅∅∅": "∅∅∅"},
73 }
74 }
75
76
77 func (zgh *zgh) Locale() string {
78 return zgh.locale
79 }
80
81
82 func (zgh *zgh) PluralsCardinal() []locales.PluralRule {
83 return zgh.pluralsCardinal
84 }
85
86
87 func (zgh *zgh) PluralsOrdinal() []locales.PluralRule {
88 return zgh.pluralsOrdinal
89 }
90
91
92 func (zgh *zgh) PluralsRange() []locales.PluralRule {
93 return zgh.pluralsRange
94 }
95
96
97 func (zgh *zgh) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
98 return locales.PluralRuleUnknown
99 }
100
101
102 func (zgh *zgh) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
103 return locales.PluralRuleUnknown
104 }
105
106
107 func (zgh *zgh) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
108 return locales.PluralRuleUnknown
109 }
110
111
112 func (zgh *zgh) MonthAbbreviated(month time.Month) string {
113 return zgh.monthsAbbreviated[month]
114 }
115
116
117 func (zgh *zgh) MonthsAbbreviated() []string {
118 return zgh.monthsAbbreviated[1:]
119 }
120
121
122 func (zgh *zgh) MonthNarrow(month time.Month) string {
123 return zgh.monthsNarrow[month]
124 }
125
126
127 func (zgh *zgh) MonthsNarrow() []string {
128 return zgh.monthsNarrow[1:]
129 }
130
131
132 func (zgh *zgh) MonthWide(month time.Month) string {
133 return zgh.monthsWide[month]
134 }
135
136
137 func (zgh *zgh) MonthsWide() []string {
138 return zgh.monthsWide[1:]
139 }
140
141
142 func (zgh *zgh) WeekdayAbbreviated(weekday time.Weekday) string {
143 return zgh.daysAbbreviated[weekday]
144 }
145
146
147 func (zgh *zgh) WeekdaysAbbreviated() []string {
148 return zgh.daysAbbreviated
149 }
150
151
152 func (zgh *zgh) WeekdayNarrow(weekday time.Weekday) string {
153 return zgh.daysNarrow[weekday]
154 }
155
156
157 func (zgh *zgh) WeekdaysNarrow() []string {
158 return zgh.daysNarrow
159 }
160
161
162 func (zgh *zgh) WeekdayShort(weekday time.Weekday) string {
163 return zgh.daysShort[weekday]
164 }
165
166
167 func (zgh *zgh) WeekdaysShort() []string {
168 return zgh.daysShort
169 }
170
171
172 func (zgh *zgh) WeekdayWide(weekday time.Weekday) string {
173 return zgh.daysWide[weekday]
174 }
175
176
177 func (zgh *zgh) WeekdaysWide() []string {
178 return zgh.daysWide
179 }
180
181
182 func (zgh *zgh) Decimal() string {
183 return zgh.decimal
184 }
185
186
187 func (zgh *zgh) Group() string {
188 return zgh.group
189 }
190
191
192 func (zgh *zgh) Minus() string {
193 return zgh.minus
194 }
195
196
197 func (zgh *zgh) 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, zgh.decimal[0])
209 inWhole = true
210 continue
211 }
212
213 if inWhole {
214 if count == 3 {
215 for j := len(zgh.group) - 1; j >= 0; j-- {
216 b = append(b, zgh.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, zgh.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 (zgh *zgh) FmtPercent(num float64, v uint64) string {
242 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
243 l := len(s) + 5
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, zgh.decimal[0])
250 continue
251 }
252
253 b = append(b, s[i])
254 }
255
256 if num < 0 {
257 b = append(b, zgh.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, zgh.percentSuffix...)
266
267 b = append(b, zgh.percent...)
268
269 return string(b)
270 }
271
272
273 func (zgh *zgh) FmtCurrency(num float64, v uint64, currency currency.Type) string {
274
275 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
276 symbol := zgh.currencies[currency]
277 l := len(s) + len(symbol) + 2 + 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, zgh.decimal[0])
286 inWhole = true
287 continue
288 }
289
290 if inWhole {
291 if count == 3 {
292 for j := len(zgh.group) - 1; j >= 0; j-- {
293 b = append(b, zgh.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, zgh.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, zgh.decimal...)
317 }
318
319 for i := 0; i < 2-int(v); i++ {
320 b = append(b, '0')
321 }
322 }
323
324 b = append(b, symbol...)
325
326 return string(b)
327 }
328
329
330
331 func (zgh *zgh) FmtAccounting(num float64, v uint64, currency currency.Type) string {
332
333 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
334 symbol := zgh.currencies[currency]
335 l := len(s) + len(symbol) + 2 + 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, zgh.decimal[0])
344 inWhole = true
345 continue
346 }
347
348 if inWhole {
349 if count == 3 {
350 for j := len(zgh.group) - 1; j >= 0; j-- {
351 b = append(b, zgh.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, zgh.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, zgh.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, symbol...)
386 } else {
387
388 b = append(b, symbol...)
389 }
390
391 return string(b)
392 }
393
394
395 func (zgh *zgh) FmtDateShort(t time.Time) string {
396
397 b := make([]byte, 0, 32)
398
399 b = strconv.AppendInt(b, int64(t.Day()), 10)
400 b = append(b, []byte{0x2f}...)
401 b = strconv.AppendInt(b, int64(t.Month()), 10)
402 b = append(b, []byte{0x2f}...)
403
404 if t.Year() > 0 {
405 b = strconv.AppendInt(b, int64(t.Year()), 10)
406 } else {
407 b = strconv.AppendInt(b, int64(-t.Year()), 10)
408 }
409
410 return string(b)
411 }
412
413
414 func (zgh *zgh) FmtDateMedium(t time.Time) string {
415
416 b := make([]byte, 0, 32)
417
418 b = strconv.AppendInt(b, int64(t.Day()), 10)
419 b = append(b, []byte{0x20}...)
420 b = append(b, zgh.monthsAbbreviated[t.Month()]...)
421 b = append(b, []byte{0x2c, 0x20}...)
422
423 if t.Year() > 0 {
424 b = strconv.AppendInt(b, int64(t.Year()), 10)
425 } else {
426 b = strconv.AppendInt(b, int64(-t.Year()), 10)
427 }
428
429 return string(b)
430 }
431
432
433 func (zgh *zgh) FmtDateLong(t time.Time) string {
434
435 b := make([]byte, 0, 32)
436
437 b = strconv.AppendInt(b, int64(t.Day()), 10)
438 b = append(b, []byte{0x20}...)
439 b = append(b, zgh.monthsWide[t.Month()]...)
440 b = append(b, []byte{0x20}...)
441
442 if t.Year() > 0 {
443 b = strconv.AppendInt(b, int64(t.Year()), 10)
444 } else {
445 b = strconv.AppendInt(b, int64(-t.Year()), 10)
446 }
447
448 return string(b)
449 }
450
451
452 func (zgh *zgh) FmtDateFull(t time.Time) string {
453
454 b := make([]byte, 0, 32)
455
456 b = append(b, zgh.daysWide[t.Weekday()]...)
457 b = append(b, []byte{0x20}...)
458 b = strconv.AppendInt(b, int64(t.Day()), 10)
459 b = append(b, []byte{0x20}...)
460 b = append(b, zgh.monthsWide[t.Month()]...)
461 b = append(b, []byte{0x20}...)
462
463 if t.Year() > 0 {
464 b = strconv.AppendInt(b, int64(t.Year()), 10)
465 } else {
466 b = strconv.AppendInt(b, int64(-t.Year()), 10)
467 }
468
469 return string(b)
470 }
471
472
473 func (zgh *zgh) FmtTimeShort(t time.Time) string {
474
475 b := make([]byte, 0, 32)
476
477 if t.Hour() < 10 {
478 b = append(b, '0')
479 }
480
481 b = strconv.AppendInt(b, int64(t.Hour()), 10)
482 b = append(b, zgh.timeSeparator...)
483
484 if t.Minute() < 10 {
485 b = append(b, '0')
486 }
487
488 b = strconv.AppendInt(b, int64(t.Minute()), 10)
489
490 return string(b)
491 }
492
493
494 func (zgh *zgh) FmtTimeMedium(t time.Time) string {
495
496 b := make([]byte, 0, 32)
497
498 if t.Hour() < 10 {
499 b = append(b, '0')
500 }
501
502 b = strconv.AppendInt(b, int64(t.Hour()), 10)
503 b = append(b, zgh.timeSeparator...)
504
505 if t.Minute() < 10 {
506 b = append(b, '0')
507 }
508
509 b = strconv.AppendInt(b, int64(t.Minute()), 10)
510 b = append(b, zgh.timeSeparator...)
511
512 if t.Second() < 10 {
513 b = append(b, '0')
514 }
515
516 b = strconv.AppendInt(b, int64(t.Second()), 10)
517
518 return string(b)
519 }
520
521
522 func (zgh *zgh) FmtTimeLong(t time.Time) string {
523
524 b := make([]byte, 0, 32)
525
526 if t.Hour() < 10 {
527 b = append(b, '0')
528 }
529
530 b = strconv.AppendInt(b, int64(t.Hour()), 10)
531 b = append(b, zgh.timeSeparator...)
532
533 if t.Minute() < 10 {
534 b = append(b, '0')
535 }
536
537 b = strconv.AppendInt(b, int64(t.Minute()), 10)
538 b = append(b, zgh.timeSeparator...)
539
540 if t.Second() < 10 {
541 b = append(b, '0')
542 }
543
544 b = strconv.AppendInt(b, int64(t.Second()), 10)
545 b = append(b, []byte{0x20}...)
546
547 tz, _ := t.Zone()
548 b = append(b, tz...)
549
550 return string(b)
551 }
552
553
554 func (zgh *zgh) FmtTimeFull(t time.Time) string {
555
556 b := make([]byte, 0, 32)
557
558 if t.Hour() < 10 {
559 b = append(b, '0')
560 }
561
562 b = strconv.AppendInt(b, int64(t.Hour()), 10)
563 b = append(b, zgh.timeSeparator...)
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, zgh.timeSeparator...)
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 := zgh.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