1 package fi
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 fi 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 &fi{
48 locale: "fi",
49 pluralsCardinal: []locales.PluralRule{2, 6},
50 pluralsOrdinal: []locales.PluralRule{6},
51 pluralsRange: []locales.PluralRule{6},
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", "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", "€", "mk", "FJD", "FKP", "FRF", "£", "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", "¥", "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", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "CFA", "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{"", "tammik.", "helmik.", "maalisk.", "huhtik.", "toukok.", "kesäk.", "heinäk.", "elok.", "syysk.", "lokak.", "marrask.", "jouluk."},
64 monthsNarrow: []string{"", "T", "H", "M", "H", "T", "K", "H", "E", "S", "L", "M", "J"},
65 monthsWide: []string{"", "tammikuuta", "helmikuuta", "maaliskuuta", "huhtikuuta", "toukokuuta", "kesäkuuta", "heinäkuuta", "elokuuta", "syyskuuta", "lokakuuta", "marraskuuta", "joulukuuta"},
66 daysAbbreviated: []string{"su", "ma", "ti", "ke", "to", "pe", "la"},
67 daysNarrow: []string{"S", "M", "T", "K", "T", "P", "L"},
68 daysShort: []string{"su", "ma", "ti", "ke", "to", "pe", "la"},
69 daysWide: []string{"sunnuntaina", "maanantaina", "tiistaina", "keskiviikkona", "torstaina", "perjantaina", "lauantaina"},
70 periodsAbbreviated: []string{"ap.", "ip."},
71 periodsNarrow: []string{"ap.", "ip."},
72 periodsWide: []string{"ap.", "ip."},
73 erasAbbreviated: []string{"eKr.", "jKr."},
74 erasNarrow: []string{"eKr", "jKr"},
75 erasWide: []string{"ennen Kristuksen syntymää", "jälkeen Kristuksen syntymän"},
76 timezones: map[string]string{"ACDT": "Keski-Australian kesäaika", "ACST": "Keski-Australian normaaliaika", "ACWDT": "Läntisen Keski-Australian kesäaika", "ACWST": "Läntisen Keski-Australian normaaliaika", "ADT": "Kanadan Atlantin kesäaika", "AEDT": "Itä-Australian kesäaika", "AEST": "Itä-Australian normaaliaika", "AKDT": "Alaskan kesäaika", "AKST": "Alaskan normaaliaika", "ARST": "Argentiinan kesäaika", "ART": "Argentiinan normaaliaika", "AST": "Kanadan Atlantin normaaliaika", "AWDT": "Länsi-Australian kesäaika", "AWST": "Länsi-Australian normaaliaika", "BOT": "Bolivian aika", "BT": "Bhutanin aika", "CAT": "Keski-Afrikan aika", "CDT": "Yhdysvaltain keskinen kesäaika", "CHADT": "Chathamin kesäaika", "CHAST": "Chathamin normaaliaika", "CLST": "Chilen kesäaika", "CLT": "Chilen normaaliaika", "COST": "Kolumbian kesäaika", "COT": "Kolumbian normaaliaika", "CST": "Yhdysvaltain keskinen normaaliaika", "ChST": "Tšamorron aika", "EAT": "Itä-Afrikan aika", "ECT": "Ecuadorin aika", "EDT": "Yhdysvaltain itäinen kesäaika", "EST": "Yhdysvaltain itäinen normaaliaika", "GFT": "Ranskan Guayanan aika", "GMT": "Greenwichin normaaliaika", "GST": "Arabiemiirikuntien normaaliaika", "GYT": "Guyanan aika", "HADT": "Havaijin-Aleuttien kesäaika", "HAST": "Havaijin-Aleuttien normaaliaika", "HAT": "Newfoundlandin kesäaika", "HECU": "Kuuban kesäaika", "HEEG": "Itä-Grönlannin kesäaika", "HENOMX": "Luoteis-Meksikon kesäaika", "HEOG": "Länsi-Grönlannin kesäaika", "HEPM": "Saint-Pierren ja Miquelonin kesäaika", "HEPMX": "Meksikon Tyynenmeren kesäaika", "HKST": "Hongkongin kesäaika", "HKT": "Hongkongin normaaliaika", "HNCU": "Kuuban normaaliaika", "HNEG": "Itä-Grönlannin normaaliaika", "HNNOMX": "Luoteis-Meksikon normaaliaika", "HNOG": "Länsi-Grönlannin normaaliaika", "HNPM": "Saint-Pierren ja Miquelonin normaaliaika", "HNPMX": "Meksikon Tyynenmeren normaaliaika", "HNT": "Newfoundlandin normaaliaika", "IST": "Intian aika", "JDT": "Japanin kesäaika", "JST": "Japanin normaaliaika", "LHDT": "Lord Howen kesäaika", "LHST": "Lord Howen normaaliaika", "MDT": "Kalliovuorten kesäaika", "MESZ": "Keski-Euroopan kesäaika", "MEZ": "Keski-Euroopan normaaliaika", "MST": "Kalliovuorten normaaliaika", "MYT": "Malesian aika", "NZDT": "Uuden-Seelannin kesäaika", "NZST": "Uuden-Seelannin normaaliaika", "OESZ": "Itä-Euroopan kesäaika", "OEZ": "Itä-Euroopan normaaliaika", "PDT": "Yhdysvaltain Tyynenmeren kesäaika", "PST": "Yhdysvaltain Tyynenmeren normaaliaika", "SAST": "Etelä-Afrikan aika", "SGT": "Singaporen aika", "SRT": "Surinamen aika", "TMST": "Turkmenistanin kesäaika", "TMT": "Turkmenistanin normaaliaika", "UYST": "Uruguayn kesäaika", "UYT": "Uruguayn normaaliaika", "VET": "Venezuelan aika", "WARST": "Länsi-Argentiinan kesäaika", "WART": "Länsi-Argentiinan normaaliaika", "WAST": "Länsi-Afrikan kesäaika", "WAT": "Länsi-Afrikan normaaliaika", "WESZ": "Länsi-Euroopan kesäaika", "WEZ": "Länsi-Euroopan normaaliaika", "WIB": "Länsi-Indonesian aika", "WIT": "Itä-Indonesian aika", "WITA": "Keski-Indonesian aika", "∅∅∅": "Brasilian kesäaika"},
77 }
78 }
79
80
81 func (fi *fi) Locale() string {
82 return fi.locale
83 }
84
85
86 func (fi *fi) PluralsCardinal() []locales.PluralRule {
87 return fi.pluralsCardinal
88 }
89
90
91 func (fi *fi) PluralsOrdinal() []locales.PluralRule {
92 return fi.pluralsOrdinal
93 }
94
95
96 func (fi *fi) PluralsRange() []locales.PluralRule {
97 return fi.pluralsRange
98 }
99
100
101 func (fi *fi) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
102
103 n := math.Abs(num)
104 i := int64(n)
105
106 if i == 1 && v == 0 {
107 return locales.PluralRuleOne
108 }
109
110 return locales.PluralRuleOther
111 }
112
113
114 func (fi *fi) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
115 return locales.PluralRuleOther
116 }
117
118
119 func (fi *fi) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
120 return locales.PluralRuleOther
121 }
122
123
124 func (fi *fi) MonthAbbreviated(month time.Month) string {
125 return fi.monthsAbbreviated[month]
126 }
127
128
129 func (fi *fi) MonthsAbbreviated() []string {
130 return fi.monthsAbbreviated[1:]
131 }
132
133
134 func (fi *fi) MonthNarrow(month time.Month) string {
135 return fi.monthsNarrow[month]
136 }
137
138
139 func (fi *fi) MonthsNarrow() []string {
140 return fi.monthsNarrow[1:]
141 }
142
143
144 func (fi *fi) MonthWide(month time.Month) string {
145 return fi.monthsWide[month]
146 }
147
148
149 func (fi *fi) MonthsWide() []string {
150 return fi.monthsWide[1:]
151 }
152
153
154 func (fi *fi) WeekdayAbbreviated(weekday time.Weekday) string {
155 return fi.daysAbbreviated[weekday]
156 }
157
158
159 func (fi *fi) WeekdaysAbbreviated() []string {
160 return fi.daysAbbreviated
161 }
162
163
164 func (fi *fi) WeekdayNarrow(weekday time.Weekday) string {
165 return fi.daysNarrow[weekday]
166 }
167
168
169 func (fi *fi) WeekdaysNarrow() []string {
170 return fi.daysNarrow
171 }
172
173
174 func (fi *fi) WeekdayShort(weekday time.Weekday) string {
175 return fi.daysShort[weekday]
176 }
177
178
179 func (fi *fi) WeekdaysShort() []string {
180 return fi.daysShort
181 }
182
183
184 func (fi *fi) WeekdayWide(weekday time.Weekday) string {
185 return fi.daysWide[weekday]
186 }
187
188
189 func (fi *fi) WeekdaysWide() []string {
190 return fi.daysWide
191 }
192
193
194 func (fi *fi) Decimal() string {
195 return fi.decimal
196 }
197
198
199 func (fi *fi) Group() string {
200 return fi.group
201 }
202
203
204 func (fi *fi) Minus() string {
205 return fi.minus
206 }
207
208
209 func (fi *fi) FmtNumber(num float64, v uint64) string {
210
211 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
212 l := len(s) + 4 + 2*len(s[:len(s)-int(v)-1])/3
213 count := 0
214 inWhole := v == 0
215 b := make([]byte, 0, l)
216
217 for i := len(s) - 1; i >= 0; i-- {
218
219 if s[i] == '.' {
220 b = append(b, fi.decimal[0])
221 inWhole = true
222 continue
223 }
224
225 if inWhole {
226 if count == 3 {
227 for j := len(fi.group) - 1; j >= 0; j-- {
228 b = append(b, fi.group[j])
229 }
230 count = 1
231 } else {
232 count++
233 }
234 }
235
236 b = append(b, s[i])
237 }
238
239 if num < 0 {
240 for j := len(fi.minus) - 1; j >= 0; j-- {
241 b = append(b, fi.minus[j])
242 }
243 }
244
245
246 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
247 b[i], b[j] = b[j], b[i]
248 }
249
250 return string(b)
251 }
252
253
254
255 func (fi *fi) FmtPercent(num float64, v uint64) string {
256 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
257 l := len(s) + 7
258 b := make([]byte, 0, l)
259
260 for i := len(s) - 1; i >= 0; i-- {
261
262 if s[i] == '.' {
263 b = append(b, fi.decimal[0])
264 continue
265 }
266
267 b = append(b, s[i])
268 }
269
270 if num < 0 {
271 for j := len(fi.minus) - 1; j >= 0; j-- {
272 b = append(b, fi.minus[j])
273 }
274 }
275
276
277 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
278 b[i], b[j] = b[j], b[i]
279 }
280
281 b = append(b, fi.percentSuffix...)
282
283 b = append(b, fi.percent...)
284
285 return string(b)
286 }
287
288
289 func (fi *fi) FmtCurrency(num float64, v uint64, currency currency.Type) string {
290
291 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
292 symbol := fi.currencies[currency]
293 l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
294 count := 0
295 inWhole := v == 0
296 b := make([]byte, 0, l)
297
298 for i := len(s) - 1; i >= 0; i-- {
299
300 if s[i] == '.' {
301 b = append(b, fi.decimal[0])
302 inWhole = true
303 continue
304 }
305
306 if inWhole {
307 if count == 3 {
308 for j := len(fi.group) - 1; j >= 0; j-- {
309 b = append(b, fi.group[j])
310 }
311 count = 1
312 } else {
313 count++
314 }
315 }
316
317 b = append(b, s[i])
318 }
319
320 if num < 0 {
321 for j := len(fi.minus) - 1; j >= 0; j-- {
322 b = append(b, fi.minus[j])
323 }
324 }
325
326
327 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
328 b[i], b[j] = b[j], b[i]
329 }
330
331 if int(v) < 2 {
332
333 if v == 0 {
334 b = append(b, fi.decimal...)
335 }
336
337 for i := 0; i < 2-int(v); i++ {
338 b = append(b, '0')
339 }
340 }
341
342 b = append(b, fi.currencyPositiveSuffix...)
343
344 b = append(b, symbol...)
345
346 return string(b)
347 }
348
349
350
351 func (fi *fi) FmtAccounting(num float64, v uint64, currency currency.Type) string {
352
353 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
354 symbol := fi.currencies[currency]
355 l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
356 count := 0
357 inWhole := v == 0
358 b := make([]byte, 0, l)
359
360 for i := len(s) - 1; i >= 0; i-- {
361
362 if s[i] == '.' {
363 b = append(b, fi.decimal[0])
364 inWhole = true
365 continue
366 }
367
368 if inWhole {
369 if count == 3 {
370 for j := len(fi.group) - 1; j >= 0; j-- {
371 b = append(b, fi.group[j])
372 }
373 count = 1
374 } else {
375 count++
376 }
377 }
378
379 b = append(b, s[i])
380 }
381
382 if num < 0 {
383
384 for j := len(fi.minus) - 1; j >= 0; j-- {
385 b = append(b, fi.minus[j])
386 }
387
388 }
389
390
391 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
392 b[i], b[j] = b[j], b[i]
393 }
394
395 if int(v) < 2 {
396
397 if v == 0 {
398 b = append(b, fi.decimal...)
399 }
400
401 for i := 0; i < 2-int(v); i++ {
402 b = append(b, '0')
403 }
404 }
405
406 if num < 0 {
407 b = append(b, fi.currencyNegativeSuffix...)
408 b = append(b, symbol...)
409 } else {
410
411 b = append(b, fi.currencyPositiveSuffix...)
412 b = append(b, symbol...)
413 }
414
415 return string(b)
416 }
417
418
419 func (fi *fi) FmtDateShort(t time.Time) string {
420
421 b := make([]byte, 0, 32)
422
423 b = strconv.AppendInt(b, int64(t.Day()), 10)
424 b = append(b, []byte{0x2e}...)
425 b = strconv.AppendInt(b, int64(t.Month()), 10)
426 b = append(b, []byte{0x2e}...)
427
428 if t.Year() > 0 {
429 b = strconv.AppendInt(b, int64(t.Year()), 10)
430 } else {
431 b = strconv.AppendInt(b, int64(-t.Year()), 10)
432 }
433
434 return string(b)
435 }
436
437
438 func (fi *fi) FmtDateMedium(t time.Time) string {
439
440 b := make([]byte, 0, 32)
441
442 b = strconv.AppendInt(b, int64(t.Day()), 10)
443 b = append(b, []byte{0x2e}...)
444 b = strconv.AppendInt(b, int64(t.Month()), 10)
445 b = append(b, []byte{0x2e}...)
446
447 if t.Year() > 0 {
448 b = strconv.AppendInt(b, int64(t.Year()), 10)
449 } else {
450 b = strconv.AppendInt(b, int64(-t.Year()), 10)
451 }
452
453 return string(b)
454 }
455
456
457 func (fi *fi) FmtDateLong(t time.Time) string {
458
459 b := make([]byte, 0, 32)
460
461 b = strconv.AppendInt(b, int64(t.Day()), 10)
462 b = append(b, []byte{0x2e, 0x20}...)
463 b = append(b, fi.monthsWide[t.Month()]...)
464 b = append(b, []byte{0x20}...)
465
466 if t.Year() > 0 {
467 b = strconv.AppendInt(b, int64(t.Year()), 10)
468 } else {
469 b = strconv.AppendInt(b, int64(-t.Year()), 10)
470 }
471
472 return string(b)
473 }
474
475
476 func (fi *fi) FmtDateFull(t time.Time) string {
477
478 b := make([]byte, 0, 32)
479
480 b = append(b, []byte{0x63, 0x63, 0x63, 0x63, 0x20}...)
481 b = strconv.AppendInt(b, int64(t.Day()), 10)
482 b = append(b, []byte{0x2e, 0x20}...)
483 b = append(b, fi.monthsWide[t.Month()]...)
484 b = append(b, []byte{0x20}...)
485
486 if t.Year() > 0 {
487 b = strconv.AppendInt(b, int64(t.Year()), 10)
488 } else {
489 b = strconv.AppendInt(b, int64(-t.Year()), 10)
490 }
491
492 return string(b)
493 }
494
495
496 func (fi *fi) FmtTimeShort(t time.Time) string {
497
498 b := make([]byte, 0, 32)
499
500 b = strconv.AppendInt(b, int64(t.Hour()), 10)
501 b = append(b, []byte{0x2e}...)
502
503 if t.Minute() < 10 {
504 b = append(b, '0')
505 }
506
507 b = strconv.AppendInt(b, int64(t.Minute()), 10)
508
509 return string(b)
510 }
511
512
513 func (fi *fi) FmtTimeMedium(t time.Time) string {
514
515 b := make([]byte, 0, 32)
516
517 b = strconv.AppendInt(b, int64(t.Hour()), 10)
518 b = append(b, []byte{0x2e}...)
519
520 if t.Minute() < 10 {
521 b = append(b, '0')
522 }
523
524 b = strconv.AppendInt(b, int64(t.Minute()), 10)
525 b = append(b, []byte{0x2e}...)
526
527 if t.Second() < 10 {
528 b = append(b, '0')
529 }
530
531 b = strconv.AppendInt(b, int64(t.Second()), 10)
532
533 return string(b)
534 }
535
536
537 func (fi *fi) FmtTimeLong(t time.Time) string {
538
539 b := make([]byte, 0, 32)
540
541 b = strconv.AppendInt(b, int64(t.Hour()), 10)
542 b = append(b, []byte{0x2e}...)
543
544 if t.Minute() < 10 {
545 b = append(b, '0')
546 }
547
548 b = strconv.AppendInt(b, int64(t.Minute()), 10)
549 b = append(b, []byte{0x2e}...)
550
551 if t.Second() < 10 {
552 b = append(b, '0')
553 }
554
555 b = strconv.AppendInt(b, int64(t.Second()), 10)
556 b = append(b, []byte{0x20}...)
557
558 tz, _ := t.Zone()
559 b = append(b, tz...)
560
561 return string(b)
562 }
563
564
565 func (fi *fi) FmtTimeFull(t time.Time) string {
566
567 b := make([]byte, 0, 32)
568
569 b = strconv.AppendInt(b, int64(t.Hour()), 10)
570 b = append(b, []byte{0x2e}...)
571
572 if t.Minute() < 10 {
573 b = append(b, '0')
574 }
575
576 b = strconv.AppendInt(b, int64(t.Minute()), 10)
577 b = append(b, []byte{0x2e}...)
578
579 if t.Second() < 10 {
580 b = append(b, '0')
581 }
582
583 b = strconv.AppendInt(b, int64(t.Second()), 10)
584 b = append(b, []byte{0x20}...)
585
586 tz, _ := t.Zone()
587
588 if btz, ok := fi.timezones[tz]; ok {
589 b = append(b, btz...)
590 } else {
591 b = append(b, tz...)
592 }
593
594 return string(b)
595 }
596
View as plain text