1 package nds
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 nds 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 &nds{
48 locale: "nds",
49 pluralsCardinal: nil,
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", "AU$", "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", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CN¥", "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", "€", "FIM", "FJD", "FKP", "FRF", "£", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HK$", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "₹", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JP¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "₩", "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", "MX$", "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", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "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"},
60 percentSuffix: " ",
61 currencyPositiveSuffix: " ",
62 currencyNegativeSuffix: " ",
63 monthsAbbreviated: []string{"", "Jan.", "Feb.", "März", "Apr.", "Mai", "Juni", "Juli", "Aug.", "Sep.", "Okt.", "Nov.", "Dez."},
64 monthsNarrow: []string{"", "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
65 monthsWide: []string{"", "Januaar", "Februaar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktover", "November", "Dezember"},
66 daysAbbreviated: []string{"Sü.", "Ma.", "Di.", "Mi.", "Du.", "Fr.", "Sa."},
67 daysNarrow: []string{"S", "M", "D", "M", "D", "F", "S"},
68 daysWide: []string{"Sünndag", "Maandag", "Dingsdag", "Middeweken", "Dunnersdag", "Freedag", "Sünnavend"},
69 periodsAbbreviated: []string{"vm", "nm"},
70 periodsWide: []string{"vm", "nm"},
71 erasAbbreviated: []string{"v.Chr.", "n.Chr."},
72 erasNarrow: []string{"vC", "nC"},
73 erasWide: []string{"vör Christus", "na Christus"},
74 timezones: map[string]string{"ACDT": "Zentraalaustraalsch Summertiet", "ACST": "Zentraalaustraalsch Standardtiet", "ACWDT": "Westzentraalaustraalsch Summertiet", "ACWST": "Westzentraalaustraalsch Standardtiet", "ADT": "Noordamerikaansch Atlantik-Summertiet", "AEDT": "Oostaustraalsch Summertiet", "AEST": "Oostaustraalsch Standardtiet", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "Noordamerikaansch Atlantik-Standardtiet", "AWDT": "Westaustraalsch Summertiet", "AWST": "Westaustraalsch Standardtiet", "BOT": "BOT", "BT": "BT", "CAT": "Zentraalafrikaansch Tiet", "CDT": "Noordamerikaansch zentraal Summertiet", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "CST": "Noordamerikaansch zentraal Standardtiet", "ChST": "ChST", "EAT": "Oostafrikaansch Tiet", "ECT": "ECT", "EDT": "Noordamerikaansch oosten Summertiet", "EST": "Noordamerikaansch oosten Standardtiet", "GFT": "GFT", "GMT": "Gröönwisch-Welttiet", "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": "Indien-Tiet", "JDT": "Japaansch Summertiet", "JST": "Japaansch Standardtiet", "LHDT": "LHDT", "LHST": "LHST", "MDT": "Noordamerikaansch Barg-Summertiet", "MESZ": "Zentraaleuropääsch Summertiet", "MEZ": "Zentraaleuropääsch Standardtiet", "MST": "Noordamerikaansch Barg-Standardtiet", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "Oosteuropääsch Summertiet", "OEZ": "Oosteuropääsch Standardtiet", "PDT": "Noordamerikaansch Pazifik-Summertiet", "PST": "Noordamerikaansch Pazifik-Standardtiet", "SAST": "Söödafrikaansch Tiet", "SGT": "SGT", "SRT": "SRT", "TMST": "TMST", "TMT": "TMT", "UYST": "UYST", "UYT": "UYT", "VET": "VET", "WARST": "WARST", "WART": "WART", "WAST": "Westafrikaansch Summertiet", "WAT": "Westafrikaansch Standardtiet", "WESZ": "Westeuropääsch Summertiet", "WEZ": "Westeuropääsch Standardtiet", "WIB": "Westindoneesch Tiet", "WIT": "Oostindoneesch Tiet", "WITA": "Indoneesch Zentraaltiet", "∅∅∅": "∅∅∅"},
75 }
76 }
77
78
79 func (nds *nds) Locale() string {
80 return nds.locale
81 }
82
83
84 func (nds *nds) PluralsCardinal() []locales.PluralRule {
85 return nds.pluralsCardinal
86 }
87
88
89 func (nds *nds) PluralsOrdinal() []locales.PluralRule {
90 return nds.pluralsOrdinal
91 }
92
93
94 func (nds *nds) PluralsRange() []locales.PluralRule {
95 return nds.pluralsRange
96 }
97
98
99 func (nds *nds) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
100 return locales.PluralRuleUnknown
101 }
102
103
104 func (nds *nds) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
105 return locales.PluralRuleUnknown
106 }
107
108
109 func (nds *nds) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
110 return locales.PluralRuleUnknown
111 }
112
113
114 func (nds *nds) MonthAbbreviated(month time.Month) string {
115 return nds.monthsAbbreviated[month]
116 }
117
118
119 func (nds *nds) MonthsAbbreviated() []string {
120 return nds.monthsAbbreviated[1:]
121 }
122
123
124 func (nds *nds) MonthNarrow(month time.Month) string {
125 return nds.monthsNarrow[month]
126 }
127
128
129 func (nds *nds) MonthsNarrow() []string {
130 return nds.monthsNarrow[1:]
131 }
132
133
134 func (nds *nds) MonthWide(month time.Month) string {
135 return nds.monthsWide[month]
136 }
137
138
139 func (nds *nds) MonthsWide() []string {
140 return nds.monthsWide[1:]
141 }
142
143
144 func (nds *nds) WeekdayAbbreviated(weekday time.Weekday) string {
145 return nds.daysAbbreviated[weekday]
146 }
147
148
149 func (nds *nds) WeekdaysAbbreviated() []string {
150 return nds.daysAbbreviated
151 }
152
153
154 func (nds *nds) WeekdayNarrow(weekday time.Weekday) string {
155 return nds.daysNarrow[weekday]
156 }
157
158
159 func (nds *nds) WeekdaysNarrow() []string {
160 return nds.daysNarrow
161 }
162
163
164 func (nds *nds) WeekdayShort(weekday time.Weekday) string {
165 return nds.daysShort[weekday]
166 }
167
168
169 func (nds *nds) WeekdaysShort() []string {
170 return nds.daysShort
171 }
172
173
174 func (nds *nds) WeekdayWide(weekday time.Weekday) string {
175 return nds.daysWide[weekday]
176 }
177
178
179 func (nds *nds) WeekdaysWide() []string {
180 return nds.daysWide
181 }
182
183
184 func (nds *nds) Decimal() string {
185 return nds.decimal
186 }
187
188
189 func (nds *nds) Group() string {
190 return nds.group
191 }
192
193
194 func (nds *nds) Minus() string {
195 return nds.minus
196 }
197
198
199 func (nds *nds) FmtNumber(num float64, v uint64) string {
200
201 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
202 l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
203 count := 0
204 inWhole := v == 0
205 b := make([]byte, 0, l)
206
207 for i := len(s) - 1; i >= 0; i-- {
208
209 if s[i] == '.' {
210 b = append(b, nds.decimal[0])
211 inWhole = true
212 continue
213 }
214
215 if inWhole {
216 if count == 3 {
217 b = append(b, nds.group[0])
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, nds.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 (nds *nds) 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, nds.decimal[0])
250 continue
251 }
252
253 b = append(b, s[i])
254 }
255
256 if num < 0 {
257 b = append(b, nds.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, nds.percentSuffix...)
266
267 b = append(b, nds.percent...)
268
269 return string(b)
270 }
271
272
273 func (nds *nds) FmtCurrency(num float64, v uint64, currency currency.Type) string {
274
275 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
276 symbol := nds.currencies[currency]
277 l := len(s) + len(symbol) + 4 + 1*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, nds.decimal[0])
286 inWhole = true
287 continue
288 }
289
290 if inWhole {
291 if count == 3 {
292 b = append(b, nds.group[0])
293 count = 1
294 } else {
295 count++
296 }
297 }
298
299 b = append(b, s[i])
300 }
301
302 if num < 0 {
303 b = append(b, nds.minus[0])
304 }
305
306
307 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
308 b[i], b[j] = b[j], b[i]
309 }
310
311 if int(v) < 2 {
312
313 if v == 0 {
314 b = append(b, nds.decimal...)
315 }
316
317 for i := 0; i < 2-int(v); i++ {
318 b = append(b, '0')
319 }
320 }
321
322 b = append(b, nds.currencyPositiveSuffix...)
323
324 b = append(b, symbol...)
325
326 return string(b)
327 }
328
329
330
331 func (nds *nds) FmtAccounting(num float64, v uint64, currency currency.Type) string {
332
333 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
334 symbol := nds.currencies[currency]
335 l := len(s) + len(symbol) + 4 + 1*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, nds.decimal[0])
344 inWhole = true
345 continue
346 }
347
348 if inWhole {
349 if count == 3 {
350 b = append(b, nds.group[0])
351 count = 1
352 } else {
353 count++
354 }
355 }
356
357 b = append(b, s[i])
358 }
359
360 if num < 0 {
361
362 b = append(b, nds.minus[0])
363
364 }
365
366
367 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
368 b[i], b[j] = b[j], b[i]
369 }
370
371 if int(v) < 2 {
372
373 if v == 0 {
374 b = append(b, nds.decimal...)
375 }
376
377 for i := 0; i < 2-int(v); i++ {
378 b = append(b, '0')
379 }
380 }
381
382 if num < 0 {
383 b = append(b, nds.currencyNegativeSuffix...)
384 b = append(b, symbol...)
385 } else {
386
387 b = append(b, nds.currencyPositiveSuffix...)
388 b = append(b, symbol...)
389 }
390
391 return string(b)
392 }
393
394
395 func (nds *nds) 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{0x2e}...)
401
402 if t.Month() < 10 {
403 b = append(b, '0')
404 }
405
406 b = strconv.AppendInt(b, int64(t.Month()), 10)
407
408 b = append(b, []byte{0x2e}...)
409
410 if t.Year() > 9 {
411 b = append(b, strconv.Itoa(t.Year())[2:]...)
412 } else {
413 b = append(b, strconv.Itoa(t.Year())[1:]...)
414 }
415
416 return string(b)
417 }
418
419
420 func (nds *nds) FmtDateMedium(t time.Time) string {
421
422 b := make([]byte, 0, 32)
423
424 b = strconv.AppendInt(b, int64(t.Day()), 10)
425 b = append(b, []byte{0x2e, 0x20}...)
426 b = append(b, nds.monthsAbbreviated[t.Month()]...)
427 b = append(b, []byte{0x20}...)
428
429 if t.Year() > 0 {
430 b = strconv.AppendInt(b, int64(t.Year()), 10)
431 } else {
432 b = strconv.AppendInt(b, int64(-t.Year()), 10)
433 }
434
435 return string(b)
436 }
437
438
439 func (nds *nds) FmtDateLong(t time.Time) string {
440
441 b := make([]byte, 0, 32)
442
443 b = strconv.AppendInt(b, int64(t.Day()), 10)
444 b = append(b, []byte{0x2e, 0x20}...)
445 b = append(b, nds.monthsWide[t.Month()]...)
446 b = append(b, []byte{0x20}...)
447
448 if t.Year() > 0 {
449 b = strconv.AppendInt(b, int64(t.Year()), 10)
450 } else {
451 b = strconv.AppendInt(b, int64(-t.Year()), 10)
452 }
453
454 return string(b)
455 }
456
457
458 func (nds *nds) FmtDateFull(t time.Time) string {
459
460 b := make([]byte, 0, 32)
461
462 b = append(b, nds.daysWide[t.Weekday()]...)
463 b = append(b, []byte{0x2c, 0x20, 0x64, 0x65}...)
464 b = append(b, []byte{0x20}...)
465 b = strconv.AppendInt(b, int64(t.Day()), 10)
466 b = append(b, []byte{0x2e, 0x20}...)
467 b = append(b, nds.monthsWide[t.Month()]...)
468 b = append(b, []byte{0x20}...)
469
470 if t.Year() > 0 {
471 b = strconv.AppendInt(b, int64(t.Year()), 10)
472 } else {
473 b = strconv.AppendInt(b, int64(-t.Year()), 10)
474 }
475
476 return string(b)
477 }
478
479
480 func (nds *nds) FmtTimeShort(t time.Time) string {
481
482 b := make([]byte, 0, 32)
483
484 b = append(b, []byte{0x4b, 0x6c}...)
485 b = append(b, []byte{0x2e, 0x20}...)
486 b = strconv.AppendInt(b, int64(t.Hour()), 10)
487 b = append(b, []byte{0x2e}...)
488
489 if t.Minute() < 10 {
490 b = append(b, '0')
491 }
492
493 b = strconv.AppendInt(b, int64(t.Minute()), 10)
494
495 return string(b)
496 }
497
498
499 func (nds *nds) FmtTimeMedium(t time.Time) string {
500
501 b := make([]byte, 0, 32)
502
503 b = append(b, []byte{0x4b, 0x6c, 0x6f, 0x63, 0x6b}...)
504 b = append(b, []byte{0x20}...)
505 b = strconv.AppendInt(b, int64(t.Hour()), 10)
506 b = append(b, []byte{0x2e}...)
507
508 if t.Minute() < 10 {
509 b = append(b, '0')
510 }
511
512 b = strconv.AppendInt(b, int64(t.Minute()), 10)
513 b = append(b, nds.timeSeparator...)
514
515 if t.Second() < 10 {
516 b = append(b, '0')
517 }
518
519 b = strconv.AppendInt(b, int64(t.Second()), 10)
520
521 return string(b)
522 }
523
524
525 func (nds *nds) FmtTimeLong(t time.Time) string {
526
527 b := make([]byte, 0, 32)
528
529 b = append(b, []byte{0x4b, 0x6c, 0x6f, 0x63, 0x6b}...)
530 b = append(b, []byte{0x20}...)
531 b = strconv.AppendInt(b, int64(t.Hour()), 10)
532 b = append(b, []byte{0x2e}...)
533
534 if t.Minute() < 10 {
535 b = append(b, '0')
536 }
537
538 b = strconv.AppendInt(b, int64(t.Minute()), 10)
539 b = append(b, nds.timeSeparator...)
540
541 if t.Second() < 10 {
542 b = append(b, '0')
543 }
544
545 b = strconv.AppendInt(b, int64(t.Second()), 10)
546 b = append(b, []byte{0x20, 0x28}...)
547
548 tz, _ := t.Zone()
549 b = append(b, tz...)
550
551 b = append(b, []byte{0x29}...)
552
553 return string(b)
554 }
555
556
557 func (nds *nds) FmtTimeFull(t time.Time) string {
558
559 b := make([]byte, 0, 32)
560
561 b = append(b, []byte{0x4b, 0x6c, 0x6f, 0x63, 0x6b}...)
562 b = append(b, []byte{0x20}...)
563 b = strconv.AppendInt(b, int64(t.Hour()), 10)
564 b = append(b, []byte{0x2e}...)
565
566 if t.Minute() < 10 {
567 b = append(b, '0')
568 }
569
570 b = strconv.AppendInt(b, int64(t.Minute()), 10)
571 b = append(b, nds.timeSeparator...)
572
573 if t.Second() < 10 {
574 b = append(b, '0')
575 }
576
577 b = strconv.AppendInt(b, int64(t.Second()), 10)
578 b = append(b, []byte{0x20, 0x28}...)
579
580 tz, _ := t.Zone()
581
582 if btz, ok := nds.timezones[tz]; ok {
583 b = append(b, btz...)
584 } else {
585 b = append(b, tz...)
586 }
587
588 b = append(b, []byte{0x29}...)
589
590 return string(b)
591 }
592
View as plain text