1 package vo
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 vo 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 &vo{
45 locale: "vo",
46 pluralsCardinal: []locales.PluralRule{2, 6},
47 pluralsOrdinal: nil,
48 pluralsRange: nil,
49 timeSeparator: ":",
50 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"},
51 monthsAbbreviated: []string{"", "yan", "feb", "mäz", "prl", "may", "yun", "yul", "gst", "set", "ton", "nov", "dek"},
52 monthsNarrow: []string{"", "Y", "F", "M", "P", "M", "Y", "Y", "G", "S", "T", "N", "D"},
53 monthsWide: []string{"", "yanul", "febul", "mäzul", "prilul", "mayul", "yunul", "yulul", "gustul", "setul", "tobul", "novul", "dekul"},
54 daysAbbreviated: []string{"su.", "mu.", "tu.", "ve.", "dö.", "fr.", "zä."},
55 daysNarrow: []string{"S", "M", "T", "V", "D", "F", "Z"},
56 daysWide: []string{"sudel", "mudel", "tudel", "vedel", "dödel", "fridel", "zädel"},
57 erasAbbreviated: []string{"b. t. kr.", "p. t. kr."},
58 erasNarrow: []string{"", ""},
59 erasWide: []string{"b. t. kr.", "p. t. kr."},
60 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", "∅∅∅": "∅∅∅"},
61 }
62 }
63
64
65 func (vo *vo) Locale() string {
66 return vo.locale
67 }
68
69
70 func (vo *vo) PluralsCardinal() []locales.PluralRule {
71 return vo.pluralsCardinal
72 }
73
74
75 func (vo *vo) PluralsOrdinal() []locales.PluralRule {
76 return vo.pluralsOrdinal
77 }
78
79
80 func (vo *vo) PluralsRange() []locales.PluralRule {
81 return vo.pluralsRange
82 }
83
84
85 func (vo *vo) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
86
87 n := math.Abs(num)
88
89 if n == 1 {
90 return locales.PluralRuleOne
91 }
92
93 return locales.PluralRuleOther
94 }
95
96
97 func (vo *vo) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
98 return locales.PluralRuleUnknown
99 }
100
101
102 func (vo *vo) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
103 return locales.PluralRuleUnknown
104 }
105
106
107 func (vo *vo) MonthAbbreviated(month time.Month) string {
108 return vo.monthsAbbreviated[month]
109 }
110
111
112 func (vo *vo) MonthsAbbreviated() []string {
113 return vo.monthsAbbreviated[1:]
114 }
115
116
117 func (vo *vo) MonthNarrow(month time.Month) string {
118 return vo.monthsNarrow[month]
119 }
120
121
122 func (vo *vo) MonthsNarrow() []string {
123 return vo.monthsNarrow[1:]
124 }
125
126
127 func (vo *vo) MonthWide(month time.Month) string {
128 return vo.monthsWide[month]
129 }
130
131
132 func (vo *vo) MonthsWide() []string {
133 return vo.monthsWide[1:]
134 }
135
136
137 func (vo *vo) WeekdayAbbreviated(weekday time.Weekday) string {
138 return vo.daysAbbreviated[weekday]
139 }
140
141
142 func (vo *vo) WeekdaysAbbreviated() []string {
143 return vo.daysAbbreviated
144 }
145
146
147 func (vo *vo) WeekdayNarrow(weekday time.Weekday) string {
148 return vo.daysNarrow[weekday]
149 }
150
151
152 func (vo *vo) WeekdaysNarrow() []string {
153 return vo.daysNarrow
154 }
155
156
157 func (vo *vo) WeekdayShort(weekday time.Weekday) string {
158 return vo.daysShort[weekday]
159 }
160
161
162 func (vo *vo) WeekdaysShort() []string {
163 return vo.daysShort
164 }
165
166
167 func (vo *vo) WeekdayWide(weekday time.Weekday) string {
168 return vo.daysWide[weekday]
169 }
170
171
172 func (vo *vo) WeekdaysWide() []string {
173 return vo.daysWide
174 }
175
176
177 func (vo *vo) Decimal() string {
178 return vo.decimal
179 }
180
181
182 func (vo *vo) Group() string {
183 return vo.group
184 }
185
186
187 func (vo *vo) Minus() string {
188 return vo.minus
189 }
190
191
192 func (vo *vo) FmtNumber(num float64, v uint64) string {
193
194 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
195 }
196
197
198
199 func (vo *vo) FmtPercent(num float64, v uint64) string {
200 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
201 }
202
203
204 func (vo *vo) FmtCurrency(num float64, v uint64, currency currency.Type) string {
205
206 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
207 symbol := vo.currencies[currency]
208 return string(append(append([]byte{}, symbol...), s...))
209 }
210
211
212
213 func (vo *vo) FmtAccounting(num float64, v uint64, currency currency.Type) string {
214
215 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
216 symbol := vo.currencies[currency]
217 return string(append(append([]byte{}, symbol...), s...))
218 }
219
220
221 func (vo *vo) FmtDateShort(t time.Time) string {
222
223 b := make([]byte, 0, 32)
224
225 if t.Year() > 0 {
226 b = strconv.AppendInt(b, int64(t.Year()), 10)
227 } else {
228 b = strconv.AppendInt(b, int64(-t.Year()), 10)
229 }
230
231 b = append(b, []byte{0x2d}...)
232
233 if t.Month() < 10 {
234 b = append(b, '0')
235 }
236
237 b = strconv.AppendInt(b, int64(t.Month()), 10)
238
239 b = append(b, []byte{0x2d}...)
240
241 if t.Day() < 10 {
242 b = append(b, '0')
243 }
244
245 b = strconv.AppendInt(b, int64(t.Day()), 10)
246
247 return string(b)
248 }
249
250
251 func (vo *vo) FmtDateMedium(t time.Time) string {
252
253 b := make([]byte, 0, 32)
254
255 if t.Year() > 0 {
256 b = strconv.AppendInt(b, int64(t.Year()), 10)
257 } else {
258 b = strconv.AppendInt(b, int64(-t.Year()), 10)
259 }
260
261 b = append(b, []byte{0x20}...)
262 b = append(b, vo.monthsAbbreviated[t.Month()]...)
263 b = append(b, []byte{0x2e, 0x20}...)
264 b = strconv.AppendInt(b, int64(t.Day()), 10)
265
266 return string(b)
267 }
268
269
270 func (vo *vo) FmtDateLong(t time.Time) string {
271
272 b := make([]byte, 0, 32)
273
274 if t.Year() > 0 {
275 b = strconv.AppendInt(b, int64(t.Year()), 10)
276 } else {
277 b = strconv.AppendInt(b, int64(-t.Year()), 10)
278 }
279
280 b = append(b, []byte{0x20}...)
281 b = append(b, vo.monthsWide[t.Month()]...)
282 b = append(b, []byte{0x20}...)
283 b = strconv.AppendInt(b, int64(t.Day()), 10)
284
285 return string(b)
286 }
287
288
289 func (vo *vo) FmtDateFull(t time.Time) string {
290
291 b := make([]byte, 0, 32)
292
293 if t.Year() > 0 {
294 b = strconv.AppendInt(b, int64(t.Year()), 10)
295 } else {
296 b = strconv.AppendInt(b, int64(-t.Year()), 10)
297 }
298
299 b = append(b, []byte{0x20}...)
300 b = append(b, vo.monthsWide[t.Month()]...)
301 b = append(b, []byte{0x61}...)
302 b = append(b, []byte{0x20, 0x64}...)
303 b = append(b, []byte{0x2e, 0x20}...)
304 b = strconv.AppendInt(b, int64(t.Day()), 10)
305 b = append(b, []byte{0x69, 0x64}...)
306
307 return string(b)
308 }
309
310
311 func (vo *vo) FmtTimeShort(t time.Time) string {
312
313 b := make([]byte, 0, 32)
314
315 if t.Hour() < 10 {
316 b = append(b, '0')
317 }
318
319 b = strconv.AppendInt(b, int64(t.Hour()), 10)
320 b = append(b, vo.timeSeparator...)
321
322 if t.Minute() < 10 {
323 b = append(b, '0')
324 }
325
326 b = strconv.AppendInt(b, int64(t.Minute()), 10)
327
328 return string(b)
329 }
330
331
332 func (vo *vo) FmtTimeMedium(t time.Time) string {
333
334 b := make([]byte, 0, 32)
335
336 if t.Hour() < 10 {
337 b = append(b, '0')
338 }
339
340 b = strconv.AppendInt(b, int64(t.Hour()), 10)
341 b = append(b, vo.timeSeparator...)
342
343 if t.Minute() < 10 {
344 b = append(b, '0')
345 }
346
347 b = strconv.AppendInt(b, int64(t.Minute()), 10)
348 b = append(b, vo.timeSeparator...)
349
350 if t.Second() < 10 {
351 b = append(b, '0')
352 }
353
354 b = strconv.AppendInt(b, int64(t.Second()), 10)
355
356 return string(b)
357 }
358
359
360 func (vo *vo) FmtTimeLong(t time.Time) string {
361
362 b := make([]byte, 0, 32)
363
364 if t.Hour() < 10 {
365 b = append(b, '0')
366 }
367
368 b = strconv.AppendInt(b, int64(t.Hour()), 10)
369 b = append(b, vo.timeSeparator...)
370
371 if t.Minute() < 10 {
372 b = append(b, '0')
373 }
374
375 b = strconv.AppendInt(b, int64(t.Minute()), 10)
376 b = append(b, vo.timeSeparator...)
377
378 if t.Second() < 10 {
379 b = append(b, '0')
380 }
381
382 b = strconv.AppendInt(b, int64(t.Second()), 10)
383 b = append(b, []byte{0x20}...)
384
385 tz, _ := t.Zone()
386 b = append(b, tz...)
387
388 return string(b)
389 }
390
391
392 func (vo *vo) FmtTimeFull(t time.Time) string {
393
394 b := make([]byte, 0, 32)
395
396 if t.Hour() < 10 {
397 b = append(b, '0')
398 }
399
400 b = strconv.AppendInt(b, int64(t.Hour()), 10)
401 b = append(b, vo.timeSeparator...)
402
403 if t.Minute() < 10 {
404 b = append(b, '0')
405 }
406
407 b = strconv.AppendInt(b, int64(t.Minute()), 10)
408 b = append(b, vo.timeSeparator...)
409
410 if t.Second() < 10 {
411 b = append(b, '0')
412 }
413
414 b = strconv.AppendInt(b, int64(t.Second()), 10)
415 b = append(b, []byte{0x20}...)
416
417 tz, _ := t.Zone()
418
419 if btz, ok := vo.timezones[tz]; ok {
420 b = append(b, btz...)
421 } else {
422 b = append(b, tz...)
423 }
424
425 return string(b)
426 }
427
View as plain text