1 package lu
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 lu 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 &lu{
45 locale: "lu",
46 pluralsCardinal: nil,
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", "FC", "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{"", "Cio", "Lui", "Lus", "Muu", "Lum", "Luf", "Kab", "Lush", "Lut", "Lun", "Kas", "Cis"},
54 monthsNarrow: []string{"", "C", "L", "L", "M", "L", "L", "K", "L", "L", "L", "K", "C"},
55 monthsWide: []string{"", "Ciongo", "Lùishi", "Lusòlo", "Mùuyà", "Lumùngùlù", "Lufuimi", "Kabàlàshìpù", "Lùshìkà", "Lutongolo", "Lungùdi", "Kaswèkèsè", "Ciswà"},
56 daysAbbreviated: []string{"Lum", "Nko", "Ndy", "Ndg", "Njw", "Ngv", "Lub"},
57 daysNarrow: []string{"L", "N", "N", "N", "N", "N", "L"},
58 daysWide: []string{"Lumingu", "Nkodya", "Ndàayà", "Ndangù", "Njòwa", "Ngòvya", "Lubingu"},
59 periodsAbbreviated: []string{"Dinda", "Dilolo"},
60 periodsWide: []string{"Dinda", "Dilolo"},
61 erasAbbreviated: []string{"kmp. Y.K.", "kny. Y. K."},
62 erasNarrow: []string{"", ""},
63 erasWide: []string{"Kumpala kwa Yezu Kli", "Kunyima kwa Yezu Kli"},
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 (lu *lu) Locale() string {
70 return lu.locale
71 }
72
73
74 func (lu *lu) PluralsCardinal() []locales.PluralRule {
75 return lu.pluralsCardinal
76 }
77
78
79 func (lu *lu) PluralsOrdinal() []locales.PluralRule {
80 return lu.pluralsOrdinal
81 }
82
83
84 func (lu *lu) PluralsRange() []locales.PluralRule {
85 return lu.pluralsRange
86 }
87
88
89 func (lu *lu) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
90 return locales.PluralRuleUnknown
91 }
92
93
94 func (lu *lu) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
95 return locales.PluralRuleUnknown
96 }
97
98
99 func (lu *lu) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
100 return locales.PluralRuleUnknown
101 }
102
103
104 func (lu *lu) MonthAbbreviated(month time.Month) string {
105 return lu.monthsAbbreviated[month]
106 }
107
108
109 func (lu *lu) MonthsAbbreviated() []string {
110 return lu.monthsAbbreviated[1:]
111 }
112
113
114 func (lu *lu) MonthNarrow(month time.Month) string {
115 return lu.monthsNarrow[month]
116 }
117
118
119 func (lu *lu) MonthsNarrow() []string {
120 return lu.monthsNarrow[1:]
121 }
122
123
124 func (lu *lu) MonthWide(month time.Month) string {
125 return lu.monthsWide[month]
126 }
127
128
129 func (lu *lu) MonthsWide() []string {
130 return lu.monthsWide[1:]
131 }
132
133
134 func (lu *lu) WeekdayAbbreviated(weekday time.Weekday) string {
135 return lu.daysAbbreviated[weekday]
136 }
137
138
139 func (lu *lu) WeekdaysAbbreviated() []string {
140 return lu.daysAbbreviated
141 }
142
143
144 func (lu *lu) WeekdayNarrow(weekday time.Weekday) string {
145 return lu.daysNarrow[weekday]
146 }
147
148
149 func (lu *lu) WeekdaysNarrow() []string {
150 return lu.daysNarrow
151 }
152
153
154 func (lu *lu) WeekdayShort(weekday time.Weekday) string {
155 return lu.daysShort[weekday]
156 }
157
158
159 func (lu *lu) WeekdaysShort() []string {
160 return lu.daysShort
161 }
162
163
164 func (lu *lu) WeekdayWide(weekday time.Weekday) string {
165 return lu.daysWide[weekday]
166 }
167
168
169 func (lu *lu) WeekdaysWide() []string {
170 return lu.daysWide
171 }
172
173
174 func (lu *lu) Decimal() string {
175 return lu.decimal
176 }
177
178
179 func (lu *lu) Group() string {
180 return lu.group
181 }
182
183
184 func (lu *lu) Minus() string {
185 return lu.minus
186 }
187
188
189 func (lu *lu) FmtNumber(num float64, v uint64) string {
190
191 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
192 l := len(s) + 1 + 1*len(s[:len(s)-int(v)-1])/3
193 count := 0
194 inWhole := v == 0
195 b := make([]byte, 0, l)
196
197 for i := len(s) - 1; i >= 0; i-- {
198
199 if s[i] == '.' {
200 b = append(b, lu.decimal[0])
201 inWhole = true
202 continue
203 }
204
205 if inWhole {
206 if count == 3 {
207 b = append(b, lu.group[0])
208 count = 1
209 } else {
210 count++
211 }
212 }
213
214 b = append(b, s[i])
215 }
216
217 if num < 0 {
218 b = append(b, lu.minus[0])
219 }
220
221
222 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
223 b[i], b[j] = b[j], b[i]
224 }
225
226 return string(b)
227 }
228
229
230
231 func (lu *lu) FmtPercent(num float64, v uint64) string {
232 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
233 }
234
235
236 func (lu *lu) FmtCurrency(num float64, v uint64, currency currency.Type) string {
237
238 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
239 symbol := lu.currencies[currency]
240 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
241 count := 0
242 inWhole := v == 0
243 b := make([]byte, 0, l)
244
245 for i := len(s) - 1; i >= 0; i-- {
246
247 if s[i] == '.' {
248 b = append(b, lu.decimal[0])
249 inWhole = true
250 continue
251 }
252
253 if inWhole {
254 if count == 3 {
255 b = append(b, lu.group[0])
256 count = 1
257 } else {
258 count++
259 }
260 }
261
262 b = append(b, s[i])
263 }
264
265 if num < 0 {
266 b = append(b, lu.minus[0])
267 }
268
269
270 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
271 b[i], b[j] = b[j], b[i]
272 }
273
274 if int(v) < 2 {
275
276 if v == 0 {
277 b = append(b, lu.decimal...)
278 }
279
280 for i := 0; i < 2-int(v); i++ {
281 b = append(b, '0')
282 }
283 }
284
285 b = append(b, symbol...)
286
287 return string(b)
288 }
289
290
291
292 func (lu *lu) FmtAccounting(num float64, v uint64, currency currency.Type) string {
293
294 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
295 symbol := lu.currencies[currency]
296 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
297 count := 0
298 inWhole := v == 0
299 b := make([]byte, 0, l)
300
301 for i := len(s) - 1; i >= 0; i-- {
302
303 if s[i] == '.' {
304 b = append(b, lu.decimal[0])
305 inWhole = true
306 continue
307 }
308
309 if inWhole {
310 if count == 3 {
311 b = append(b, lu.group[0])
312 count = 1
313 } else {
314 count++
315 }
316 }
317
318 b = append(b, s[i])
319 }
320
321 if num < 0 {
322
323 b = append(b, lu.minus[0])
324
325 }
326
327
328 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
329 b[i], b[j] = b[j], b[i]
330 }
331
332 if int(v) < 2 {
333
334 if v == 0 {
335 b = append(b, lu.decimal...)
336 }
337
338 for i := 0; i < 2-int(v); i++ {
339 b = append(b, '0')
340 }
341 }
342
343 if num < 0 {
344 b = append(b, symbol...)
345 } else {
346
347 b = append(b, symbol...)
348 }
349
350 return string(b)
351 }
352
353
354 func (lu *lu) FmtDateShort(t time.Time) string {
355
356 b := make([]byte, 0, 32)
357
358 b = strconv.AppendInt(b, int64(t.Day()), 10)
359 b = append(b, []byte{0x2f}...)
360 b = strconv.AppendInt(b, int64(t.Month()), 10)
361 b = append(b, []byte{0x2f}...)
362
363 if t.Year() > 0 {
364 b = strconv.AppendInt(b, int64(t.Year()), 10)
365 } else {
366 b = strconv.AppendInt(b, int64(-t.Year()), 10)
367 }
368
369 return string(b)
370 }
371
372
373 func (lu *lu) FmtDateMedium(t time.Time) string {
374
375 b := make([]byte, 0, 32)
376
377 b = strconv.AppendInt(b, int64(t.Day()), 10)
378 b = append(b, []byte{0x20}...)
379 b = append(b, lu.monthsAbbreviated[t.Month()]...)
380 b = append(b, []byte{0x20}...)
381
382 if t.Year() > 0 {
383 b = strconv.AppendInt(b, int64(t.Year()), 10)
384 } else {
385 b = strconv.AppendInt(b, int64(-t.Year()), 10)
386 }
387
388 return string(b)
389 }
390
391
392 func (lu *lu) FmtDateLong(t time.Time) string {
393
394 b := make([]byte, 0, 32)
395
396 b = strconv.AppendInt(b, int64(t.Day()), 10)
397 b = append(b, []byte{0x20}...)
398 b = append(b, lu.monthsWide[t.Month()]...)
399 b = append(b, []byte{0x20}...)
400
401 if t.Year() > 0 {
402 b = strconv.AppendInt(b, int64(t.Year()), 10)
403 } else {
404 b = strconv.AppendInt(b, int64(-t.Year()), 10)
405 }
406
407 return string(b)
408 }
409
410
411 func (lu *lu) FmtDateFull(t time.Time) string {
412
413 b := make([]byte, 0, 32)
414
415 b = append(b, lu.daysWide[t.Weekday()]...)
416 b = append(b, []byte{0x20}...)
417 b = strconv.AppendInt(b, int64(t.Day()), 10)
418 b = append(b, []byte{0x20}...)
419 b = append(b, lu.monthsWide[t.Month()]...)
420 b = append(b, []byte{0x20}...)
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 return string(b)
429 }
430
431
432 func (lu *lu) FmtTimeShort(t time.Time) string {
433
434 b := make([]byte, 0, 32)
435
436 if t.Hour() < 10 {
437 b = append(b, '0')
438 }
439
440 b = strconv.AppendInt(b, int64(t.Hour()), 10)
441 b = append(b, lu.timeSeparator...)
442
443 if t.Minute() < 10 {
444 b = append(b, '0')
445 }
446
447 b = strconv.AppendInt(b, int64(t.Minute()), 10)
448
449 return string(b)
450 }
451
452
453 func (lu *lu) FmtTimeMedium(t time.Time) string {
454
455 b := make([]byte, 0, 32)
456
457 if t.Hour() < 10 {
458 b = append(b, '0')
459 }
460
461 b = strconv.AppendInt(b, int64(t.Hour()), 10)
462 b = append(b, lu.timeSeparator...)
463
464 if t.Minute() < 10 {
465 b = append(b, '0')
466 }
467
468 b = strconv.AppendInt(b, int64(t.Minute()), 10)
469 b = append(b, lu.timeSeparator...)
470
471 if t.Second() < 10 {
472 b = append(b, '0')
473 }
474
475 b = strconv.AppendInt(b, int64(t.Second()), 10)
476
477 return string(b)
478 }
479
480
481 func (lu *lu) FmtTimeLong(t time.Time) string {
482
483 b := make([]byte, 0, 32)
484
485 if t.Hour() < 10 {
486 b = append(b, '0')
487 }
488
489 b = strconv.AppendInt(b, int64(t.Hour()), 10)
490 b = append(b, lu.timeSeparator...)
491
492 if t.Minute() < 10 {
493 b = append(b, '0')
494 }
495
496 b = strconv.AppendInt(b, int64(t.Minute()), 10)
497 b = append(b, lu.timeSeparator...)
498
499 if t.Second() < 10 {
500 b = append(b, '0')
501 }
502
503 b = strconv.AppendInt(b, int64(t.Second()), 10)
504 b = append(b, []byte{0x20}...)
505
506 tz, _ := t.Zone()
507 b = append(b, tz...)
508
509 return string(b)
510 }
511
512
513 func (lu *lu) FmtTimeFull(t time.Time) string {
514
515 b := make([]byte, 0, 32)
516
517 if t.Hour() < 10 {
518 b = append(b, '0')
519 }
520
521 b = strconv.AppendInt(b, int64(t.Hour()), 10)
522 b = append(b, lu.timeSeparator...)
523
524 if t.Minute() < 10 {
525 b = append(b, '0')
526 }
527
528 b = strconv.AppendInt(b, int64(t.Minute()), 10)
529 b = append(b, lu.timeSeparator...)
530
531 if t.Second() < 10 {
532 b = append(b, '0')
533 }
534
535 b = strconv.AppendInt(b, int64(t.Second()), 10)
536 b = append(b, []byte{0x20}...)
537
538 tz, _ := t.Zone()
539
540 if btz, ok := lu.timezones[tz]; ok {
541 b = append(b, btz...)
542 } else {
543 b = append(b, tz...)
544 }
545
546 return string(b)
547 }
548
View as plain text