1 package lkt
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 lkt 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 currencyPositivePrefix string
26 currencyPositiveSuffix string
27 currencyNegativePrefix string
28 currencyNegativeSuffix string
29 monthsAbbreviated []string
30 monthsNarrow []string
31 monthsWide []string
32 daysAbbreviated []string
33 daysNarrow []string
34 daysShort []string
35 daysWide []string
36 periodsAbbreviated []string
37 periodsNarrow []string
38 periodsShort []string
39 periodsWide []string
40 erasAbbreviated []string
41 erasNarrow []string
42 erasWide []string
43 timezones map[string]string
44 }
45
46
47 func New() locales.Translator {
48 return &lkt{
49 locale: "lkt",
50 pluralsCardinal: []locales.PluralRule{6},
51 pluralsOrdinal: nil,
52 pluralsRange: nil,
53 decimal: ".",
54 group: ",",
55 minus: "-",
56 percent: "%",
57 perMille: "‰",
58 timeSeparator: ":",
59 inifinity: "∞",
60 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", "$", "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"},
61 currencyPositivePrefix: " ",
62 currencyPositiveSuffix: "K",
63 currencyNegativePrefix: " ",
64 currencyNegativeSuffix: "K",
65 monthsWide: []string{"", "Wiótheȟika Wí", "Thiyóȟeyuŋka Wí", "Ištáwičhayazaŋ Wí", "Pȟežítȟo Wí", "Čhaŋwápetȟo Wí", "Wípazukȟa-wašté Wí", "Čhaŋpȟásapa Wí", "Wasútȟuŋ Wí", "Čhaŋwápeǧi Wí", "Čhaŋwápe-kasná Wí", "Waníyetu Wí", "Tȟahékapšuŋ Wí"},
66 daysNarrow: []string{"A", "W", "N", "Y", "T", "Z", "O"},
67 daysWide: []string{"Aŋpétuwakȟaŋ", "Aŋpétuwaŋži", "Aŋpétunuŋpa", "Aŋpétuyamni", "Aŋpétutopa", "Aŋpétuzaptaŋ", "Owáŋgyužažapi"},
68 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", "∅∅∅": "∅∅∅"},
69 }
70 }
71
72
73 func (lkt *lkt) Locale() string {
74 return lkt.locale
75 }
76
77
78 func (lkt *lkt) PluralsCardinal() []locales.PluralRule {
79 return lkt.pluralsCardinal
80 }
81
82
83 func (lkt *lkt) PluralsOrdinal() []locales.PluralRule {
84 return lkt.pluralsOrdinal
85 }
86
87
88 func (lkt *lkt) PluralsRange() []locales.PluralRule {
89 return lkt.pluralsRange
90 }
91
92
93 func (lkt *lkt) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
94 return locales.PluralRuleOther
95 }
96
97
98 func (lkt *lkt) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
99 return locales.PluralRuleUnknown
100 }
101
102
103 func (lkt *lkt) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
104 return locales.PluralRuleUnknown
105 }
106
107
108 func (lkt *lkt) MonthAbbreviated(month time.Month) string {
109 return lkt.monthsAbbreviated[month]
110 }
111
112
113 func (lkt *lkt) MonthsAbbreviated() []string {
114 return nil
115 }
116
117
118 func (lkt *lkt) MonthNarrow(month time.Month) string {
119 return lkt.monthsNarrow[month]
120 }
121
122
123 func (lkt *lkt) MonthsNarrow() []string {
124 return nil
125 }
126
127
128 func (lkt *lkt) MonthWide(month time.Month) string {
129 return lkt.monthsWide[month]
130 }
131
132
133 func (lkt *lkt) MonthsWide() []string {
134 return lkt.monthsWide[1:]
135 }
136
137
138 func (lkt *lkt) WeekdayAbbreviated(weekday time.Weekday) string {
139 return lkt.daysAbbreviated[weekday]
140 }
141
142
143 func (lkt *lkt) WeekdaysAbbreviated() []string {
144 return lkt.daysAbbreviated
145 }
146
147
148 func (lkt *lkt) WeekdayNarrow(weekday time.Weekday) string {
149 return lkt.daysNarrow[weekday]
150 }
151
152
153 func (lkt *lkt) WeekdaysNarrow() []string {
154 return lkt.daysNarrow
155 }
156
157
158 func (lkt *lkt) WeekdayShort(weekday time.Weekday) string {
159 return lkt.daysShort[weekday]
160 }
161
162
163 func (lkt *lkt) WeekdaysShort() []string {
164 return lkt.daysShort
165 }
166
167
168 func (lkt *lkt) WeekdayWide(weekday time.Weekday) string {
169 return lkt.daysWide[weekday]
170 }
171
172
173 func (lkt *lkt) WeekdaysWide() []string {
174 return lkt.daysWide
175 }
176
177
178 func (lkt *lkt) Decimal() string {
179 return lkt.decimal
180 }
181
182
183 func (lkt *lkt) Group() string {
184 return lkt.group
185 }
186
187
188 func (lkt *lkt) Minus() string {
189 return lkt.minus
190 }
191
192
193 func (lkt *lkt) FmtNumber(num float64, v uint64) string {
194
195 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
196 }
197
198
199
200 func (lkt *lkt) FmtPercent(num float64, v uint64) string {
201 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
202 }
203
204
205 func (lkt *lkt) FmtCurrency(num float64, v uint64, currency currency.Type) string {
206
207 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
208 symbol := lkt.currencies[currency]
209 l := len(s) + len(symbol) + 5
210
211 b := make([]byte, 0, l)
212
213 for i := len(s) - 1; i >= 0; i-- {
214
215 if s[i] == '.' {
216 b = append(b, lkt.decimal[0])
217 continue
218 }
219
220 b = append(b, s[i])
221 }
222
223 for j := len(symbol) - 1; j >= 0; j-- {
224 b = append(b, symbol[j])
225 }
226
227 for j := len(lkt.currencyPositivePrefix) - 1; j >= 0; j-- {
228 b = append(b, lkt.currencyPositivePrefix[j])
229 }
230
231 if num < 0 {
232 b = append(b, lkt.minus[0])
233 }
234
235
236 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
237 b[i], b[j] = b[j], b[i]
238 }
239
240 b = append(b, lkt.currencyPositiveSuffix...)
241
242 return string(b)
243 }
244
245
246
247 func (lkt *lkt) FmtAccounting(num float64, v uint64, currency currency.Type) string {
248
249 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
250 symbol := lkt.currencies[currency]
251 l := len(s) + len(symbol) + 5
252
253 b := make([]byte, 0, l)
254
255 for i := len(s) - 1; i >= 0; i-- {
256
257 if s[i] == '.' {
258 b = append(b, lkt.decimal[0])
259 continue
260 }
261
262 b = append(b, s[i])
263 }
264
265 if num < 0 {
266
267 for j := len(symbol) - 1; j >= 0; j-- {
268 b = append(b, symbol[j])
269 }
270
271 for j := len(lkt.currencyNegativePrefix) - 1; j >= 0; j-- {
272 b = append(b, lkt.currencyNegativePrefix[j])
273 }
274
275 b = append(b, lkt.minus[0])
276
277 } else {
278
279 for j := len(symbol) - 1; j >= 0; j-- {
280 b = append(b, symbol[j])
281 }
282
283 for j := len(lkt.currencyPositivePrefix) - 1; j >= 0; j-- {
284 b = append(b, lkt.currencyPositivePrefix[j])
285 }
286
287 }
288
289
290 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
291 b[i], b[j] = b[j], b[i]
292 }
293
294 if num < 0 {
295 b = append(b, lkt.currencyNegativeSuffix...)
296 } else {
297
298 b = append(b, lkt.currencyPositiveSuffix...)
299 }
300
301 return string(b)
302 }
303
304
305 func (lkt *lkt) FmtDateShort(t time.Time) string {
306
307 b := make([]byte, 0, 32)
308
309 b = strconv.AppendInt(b, int64(t.Month()), 10)
310 b = append(b, []byte{0x2f}...)
311 b = strconv.AppendInt(b, int64(t.Day()), 10)
312 b = append(b, []byte{0x2f}...)
313
314 if t.Year() > 9 {
315 b = append(b, strconv.Itoa(t.Year())[2:]...)
316 } else {
317 b = append(b, strconv.Itoa(t.Year())[1:]...)
318 }
319
320 return string(b)
321 }
322
323
324 func (lkt *lkt) FmtDateMedium(t time.Time) string {
325
326 b := make([]byte, 0, 32)
327
328 b = append(b, lkt.monthsAbbreviated[t.Month()]...)
329 b = append(b, []byte{0x20}...)
330 b = strconv.AppendInt(b, int64(t.Day()), 10)
331 b = append(b, []byte{0x2c, 0x20}...)
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 (lkt *lkt) FmtDateLong(t time.Time) string {
344
345 b := make([]byte, 0, 32)
346
347 b = append(b, lkt.monthsWide[t.Month()]...)
348 b = append(b, []byte{0x20}...)
349 b = strconv.AppendInt(b, int64(t.Day()), 10)
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 (lkt *lkt) FmtDateFull(t time.Time) string {
363
364 b := make([]byte, 0, 32)
365
366 b = append(b, lkt.daysWide[t.Weekday()]...)
367 b = append(b, []byte{0x2c, 0x20}...)
368 b = append(b, lkt.monthsWide[t.Month()]...)
369 b = append(b, []byte{0x20}...)
370 b = strconv.AppendInt(b, int64(t.Day()), 10)
371 b = append(b, []byte{0x2c, 0x20}...)
372
373 if t.Year() > 0 {
374 b = strconv.AppendInt(b, int64(t.Year()), 10)
375 } else {
376 b = strconv.AppendInt(b, int64(-t.Year()), 10)
377 }
378
379 return string(b)
380 }
381
382
383 func (lkt *lkt) FmtTimeShort(t time.Time) string {
384
385 b := make([]byte, 0, 32)
386
387 h := t.Hour()
388
389 if h > 12 {
390 h -= 12
391 }
392
393 b = strconv.AppendInt(b, int64(h), 10)
394 b = append(b, lkt.timeSeparator...)
395
396 if t.Minute() < 10 {
397 b = append(b, '0')
398 }
399
400 b = strconv.AppendInt(b, int64(t.Minute()), 10)
401 b = append(b, []byte{0x20}...)
402
403 if t.Hour() < 12 {
404 b = append(b, lkt.periodsAbbreviated[0]...)
405 } else {
406 b = append(b, lkt.periodsAbbreviated[1]...)
407 }
408
409 return string(b)
410 }
411
412
413 func (lkt *lkt) FmtTimeMedium(t time.Time) string {
414
415 b := make([]byte, 0, 32)
416
417 h := t.Hour()
418
419 if h > 12 {
420 h -= 12
421 }
422
423 b = strconv.AppendInt(b, int64(h), 10)
424 b = append(b, lkt.timeSeparator...)
425
426 if t.Minute() < 10 {
427 b = append(b, '0')
428 }
429
430 b = strconv.AppendInt(b, int64(t.Minute()), 10)
431 b = append(b, lkt.timeSeparator...)
432
433 if t.Second() < 10 {
434 b = append(b, '0')
435 }
436
437 b = strconv.AppendInt(b, int64(t.Second()), 10)
438 b = append(b, []byte{0x20}...)
439
440 if t.Hour() < 12 {
441 b = append(b, lkt.periodsAbbreviated[0]...)
442 } else {
443 b = append(b, lkt.periodsAbbreviated[1]...)
444 }
445
446 return string(b)
447 }
448
449
450 func (lkt *lkt) FmtTimeLong(t time.Time) string {
451
452 b := make([]byte, 0, 32)
453
454 h := t.Hour()
455
456 if h > 12 {
457 h -= 12
458 }
459
460 b = strconv.AppendInt(b, int64(h), 10)
461 b = append(b, lkt.timeSeparator...)
462
463 if t.Minute() < 10 {
464 b = append(b, '0')
465 }
466
467 b = strconv.AppendInt(b, int64(t.Minute()), 10)
468 b = append(b, lkt.timeSeparator...)
469
470 if t.Second() < 10 {
471 b = append(b, '0')
472 }
473
474 b = strconv.AppendInt(b, int64(t.Second()), 10)
475 b = append(b, []byte{0x20}...)
476
477 if t.Hour() < 12 {
478 b = append(b, lkt.periodsAbbreviated[0]...)
479 } else {
480 b = append(b, lkt.periodsAbbreviated[1]...)
481 }
482
483 b = append(b, []byte{0x20}...)
484
485 tz, _ := t.Zone()
486 b = append(b, tz...)
487
488 return string(b)
489 }
490
491
492 func (lkt *lkt) FmtTimeFull(t time.Time) string {
493
494 b := make([]byte, 0, 32)
495
496 h := t.Hour()
497
498 if h > 12 {
499 h -= 12
500 }
501
502 b = strconv.AppendInt(b, int64(h), 10)
503 b = append(b, lkt.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, lkt.timeSeparator...)
511
512 if t.Second() < 10 {
513 b = append(b, '0')
514 }
515
516 b = strconv.AppendInt(b, int64(t.Second()), 10)
517 b = append(b, []byte{0x20}...)
518
519 if t.Hour() < 12 {
520 b = append(b, lkt.periodsAbbreviated[0]...)
521 } else {
522 b = append(b, lkt.periodsAbbreviated[1]...)
523 }
524
525 b = append(b, []byte{0x20}...)
526
527 tz, _ := t.Zone()
528
529 if btz, ok := lkt.timezones[tz]; ok {
530 b = append(b, btz...)
531 } else {
532 b = append(b, tz...)
533 }
534
535 return string(b)
536 }
537
View as plain text