1package ff 2 3import ( 4 "math" 5 "strconv" 6 "time" 7 8 "github.com/gohugoio/locales" 9 "github.com/gohugoio/locales/currency" 10) 11 12type ff 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 // idx = enum of currency code 25 currencyPositiveSuffix string 26 currencyNegativeSuffix string 27 monthsAbbreviated []string 28 monthsNarrow []string 29 monthsWide []string 30 daysAbbreviated []string 31 daysNarrow []string 32 daysShort []string 33 daysWide []string 34 periodsAbbreviated []string 35 periodsNarrow []string 36 periodsShort []string 37 periodsWide []string 38 erasAbbreviated []string 39 erasNarrow []string 40 erasWide []string 41 timezones map[string]string 42} 43 44// New returns a new instance of translator for the 'ff' locale 45func New() locales.Translator { 46 return &ff{ 47 locale: "ff", 48 pluralsCardinal: []locales.PluralRule{2, 6}, 49 pluralsOrdinal: nil, 50 pluralsRange: nil, 51 decimal: ",", 52 group: " ", 53 timeSeparator: ":", 54 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"}, 55 currencyPositiveSuffix: " ", 56 currencyNegativeSuffix: " ", 57 monthsAbbreviated: []string{"", "sii", "col", "mbo", "see", "duu", "kor", "mor", "juk", "slt", "yar", "jol", "bow"}, 58 monthsNarrow: []string{"", "s", "c", "m", "s", "d", "k", "m", "j", "s", "y", "j", "b"}, 59 monthsWide: []string{"", "siilo", "colte", "mbooy", "seeɗto", "duujal", "korse", "morso", "juko", "siilto", "yarkomaa", "jolal", "bowte"}, 60 daysAbbreviated: []string{"dew", "aaɓ", "maw", "nje", "naa", "mwd", "hbi"}, 61 daysNarrow: []string{"d", "a", "m", "n", "n", "m", "h"}, 62 daysWide: []string{"dewo", "aaɓnde", "mawbaare", "njeslaare", "naasaande", "mawnde", "hoore-biir"}, 63 periodsAbbreviated: []string{"subaka", "kikiiɗe"}, 64 periodsWide: []string{"subaka", "kikiiɗe"}, 65 erasAbbreviated: []string{"H-I", "C-I"}, 66 erasNarrow: []string{"", ""}, 67 erasWide: []string{"Hade Iisa", "Caggal Iisa"}, 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// Locale returns the current translators string locale 73func (ff *ff) Locale() string { 74 return ff.locale 75} 76 77// PluralsCardinal returns the list of cardinal plural rules associated with 'ff' 78func (ff *ff) PluralsCardinal() []locales.PluralRule { 79 return ff.pluralsCardinal 80} 81 82// PluralsOrdinal returns the list of ordinal plural rules associated with 'ff' 83func (ff *ff) PluralsOrdinal() []locales.PluralRule { 84 return ff.pluralsOrdinal 85} 86 87// PluralsRange returns the list of range plural rules associated with 'ff' 88func (ff *ff) PluralsRange() []locales.PluralRule { 89 return ff.pluralsRange 90} 91 92// CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ff' 93func (ff *ff) CardinalPluralRule(num float64, v uint64) locales.PluralRule { 94 95 n := math.Abs(num) 96 i := int64(n) 97 98 if i == 0 || i == 1 { 99 return locales.PluralRuleOne 100 } 101 102 return locales.PluralRuleOther 103} 104 105// OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ff' 106func (ff *ff) OrdinalPluralRule(num float64, v uint64) locales.PluralRule { 107 return locales.PluralRuleUnknown 108} 109 110// RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ff' 111func (ff *ff) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule { 112 return locales.PluralRuleUnknown 113} 114 115// MonthAbbreviated returns the locales abbreviated month given the 'month' provided 116func (ff *ff) MonthAbbreviated(month time.Month) string { 117 return ff.monthsAbbreviated[month] 118} 119 120// MonthsAbbreviated returns the locales abbreviated months 121func (ff *ff) MonthsAbbreviated() []string { 122 return ff.monthsAbbreviated[1:] 123} 124 125// MonthNarrow returns the locales narrow month given the 'month' provided 126func (ff *ff) MonthNarrow(month time.Month) string { 127 return ff.monthsNarrow[month] 128} 129 130// MonthsNarrow returns the locales narrow months 131func (ff *ff) MonthsNarrow() []string { 132 return ff.monthsNarrow[1:] 133} 134 135// MonthWide returns the locales wide month given the 'month' provided 136func (ff *ff) MonthWide(month time.Month) string { 137 return ff.monthsWide[month] 138} 139 140// MonthsWide returns the locales wide months 141func (ff *ff) MonthsWide() []string { 142 return ff.monthsWide[1:] 143} 144 145// WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided 146func (ff *ff) WeekdayAbbreviated(weekday time.Weekday) string { 147 return ff.daysAbbreviated[weekday] 148} 149 150// WeekdaysAbbreviated returns the locales abbreviated weekdays 151func (ff *ff) WeekdaysAbbreviated() []string { 152 return ff.daysAbbreviated 153} 154 155// WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided 156func (ff *ff) WeekdayNarrow(weekday time.Weekday) string { 157 return ff.daysNarrow[weekday] 158} 159 160// WeekdaysNarrow returns the locales narrow weekdays 161func (ff *ff) WeekdaysNarrow() []string { 162 return ff.daysNarrow 163} 164 165// WeekdayShort returns the locales short weekday given the 'weekday' provided 166func (ff *ff) WeekdayShort(weekday time.Weekday) string { 167 return ff.daysShort[weekday] 168} 169 170// WeekdaysShort returns the locales short weekdays 171func (ff *ff) WeekdaysShort() []string { 172 return ff.daysShort 173} 174 175// WeekdayWide returns the locales wide weekday given the 'weekday' provided 176func (ff *ff) WeekdayWide(weekday time.Weekday) string { 177 return ff.daysWide[weekday] 178} 179 180// WeekdaysWide returns the locales wide weekdays 181func (ff *ff) WeekdaysWide() []string { 182 return ff.daysWide 183} 184 185// Decimal returns the decimal point of number 186func (ff *ff) Decimal() string { 187 return ff.decimal 188} 189 190// Group returns the group of number 191func (ff *ff) Group() string { 192 return ff.group 193} 194 195// Group returns the minus sign of number 196func (ff *ff) Minus() string { 197 return ff.minus 198} 199 200// FmtNumber returns 'num' with digits/precision of 'v' for 'ff' and handles both Whole and Real numbers based on 'v' 201func (ff *ff) FmtNumber(num float64, v uint64) string { 202 203 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 204} 205 206// FmtPercent returns 'num' with digits/precision of 'v' for 'ff' and handles both Whole and Real numbers based on 'v' 207// NOTE: 'num' passed into FmtPercent is assumed to be in percent already 208func (ff *ff) FmtPercent(num float64, v uint64) string { 209 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 210} 211 212// FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ff' 213func (ff *ff) FmtCurrency(num float64, v uint64, currency currency.Type) string { 214 215 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 216 symbol := ff.currencies[currency] 217 l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3 218 count := 0 219 inWhole := v == 0 220 b := make([]byte, 0, l) 221 222 for i := len(s) - 1; i >= 0; i-- { 223 224 if s[i] == '.' { 225 b = append(b, ff.decimal[0]) 226 inWhole = true 227 continue 228 } 229 230 if inWhole { 231 if count == 3 { 232 for j := len(ff.group) - 1; j >= 0; j-- { 233 b = append(b, ff.group[j]) 234 } 235 count = 1 236 } else { 237 count++ 238 } 239 } 240 241 b = append(b, s[i]) 242 } 243 244 if num < 0 { 245 b = append(b, ff.minus[0]) 246 } 247 248 // reverse 249 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { 250 b[i], b[j] = b[j], b[i] 251 } 252 253 if int(v) < 2 { 254 255 if v == 0 { 256 b = append(b, ff.decimal...) 257 } 258 259 for i := 0; i < 2-int(v); i++ { 260 b = append(b, '0') 261 } 262 } 263 264 b = append(b, ff.currencyPositiveSuffix...) 265 266 b = append(b, symbol...) 267 268 return string(b) 269} 270 271// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ff' 272// in accounting notation. 273func (ff *ff) FmtAccounting(num float64, v uint64, currency currency.Type) string { 274 275 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 276 symbol := ff.currencies[currency] 277 l := len(s) + len(symbol) + 3 + 2*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, ff.decimal[0]) 286 inWhole = true 287 continue 288 } 289 290 if inWhole { 291 if count == 3 { 292 for j := len(ff.group) - 1; j >= 0; j-- { 293 b = append(b, ff.group[j]) 294 } 295 count = 1 296 } else { 297 count++ 298 } 299 } 300 301 b = append(b, s[i]) 302 } 303 304 if num < 0 { 305 306 b = append(b, ff.minus[0]) 307 308 } 309 310 // reverse 311 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { 312 b[i], b[j] = b[j], b[i] 313 } 314 315 if int(v) < 2 { 316 317 if v == 0 { 318 b = append(b, ff.decimal...) 319 } 320 321 for i := 0; i < 2-int(v); i++ { 322 b = append(b, '0') 323 } 324 } 325 326 if num < 0 { 327 b = append(b, ff.currencyNegativeSuffix...) 328 b = append(b, symbol...) 329 } else { 330 331 b = append(b, ff.currencyPositiveSuffix...) 332 b = append(b, symbol...) 333 } 334 335 return string(b) 336} 337 338// FmtDateShort returns the short date representation of 't' for 'ff' 339func (ff *ff) FmtDateShort(t time.Time) string { 340 341 b := make([]byte, 0, 32) 342 343 b = strconv.AppendInt(b, int64(t.Day()), 10) 344 b = append(b, []byte{0x2f}...) 345 b = strconv.AppendInt(b, int64(t.Month()), 10) 346 b = append(b, []byte{0x2f}...) 347 348 if t.Year() > 0 { 349 b = strconv.AppendInt(b, int64(t.Year()), 10) 350 } else { 351 b = strconv.AppendInt(b, int64(-t.Year()), 10) 352 } 353 354 return string(b) 355} 356 357// FmtDateMedium returns the medium date representation of 't' for 'ff' 358func (ff *ff) FmtDateMedium(t time.Time) string { 359 360 b := make([]byte, 0, 32) 361 362 b = strconv.AppendInt(b, int64(t.Day()), 10) 363 b = append(b, []byte{0x20}...) 364 b = append(b, ff.monthsAbbreviated[t.Month()]...) 365 b = append(b, []byte{0x2c, 0x20}...) 366 367 if t.Year() > 0 { 368 b = strconv.AppendInt(b, int64(t.Year()), 10) 369 } else { 370 b = strconv.AppendInt(b, int64(-t.Year()), 10) 371 } 372 373 return string(b) 374} 375 376// FmtDateLong returns the long date representation of 't' for 'ff' 377func (ff *ff) FmtDateLong(t time.Time) string { 378 379 b := make([]byte, 0, 32) 380 381 b = strconv.AppendInt(b, int64(t.Day()), 10) 382 b = append(b, []byte{0x20}...) 383 b = append(b, ff.monthsWide[t.Month()]...) 384 b = append(b, []byte{0x20}...) 385 386 if t.Year() > 0 { 387 b = strconv.AppendInt(b, int64(t.Year()), 10) 388 } else { 389 b = strconv.AppendInt(b, int64(-t.Year()), 10) 390 } 391 392 return string(b) 393} 394 395// FmtDateFull returns the full date representation of 't' for 'ff' 396func (ff *ff) FmtDateFull(t time.Time) string { 397 398 b := make([]byte, 0, 32) 399 400 b = append(b, ff.daysWide[t.Weekday()]...) 401 b = append(b, []byte{0x20}...) 402 b = strconv.AppendInt(b, int64(t.Day()), 10) 403 b = append(b, []byte{0x20}...) 404 b = append(b, ff.monthsWide[t.Month()]...) 405 b = append(b, []byte{0x20}...) 406 407 if t.Year() > 0 { 408 b = strconv.AppendInt(b, int64(t.Year()), 10) 409 } else { 410 b = strconv.AppendInt(b, int64(-t.Year()), 10) 411 } 412 413 return string(b) 414} 415 416// FmtTimeShort returns the short time representation of 't' for 'ff' 417func (ff *ff) FmtTimeShort(t time.Time) string { 418 419 b := make([]byte, 0, 32) 420 421 if t.Hour() < 10 { 422 b = append(b, '0') 423 } 424 425 b = strconv.AppendInt(b, int64(t.Hour()), 10) 426 b = append(b, ff.timeSeparator...) 427 428 if t.Minute() < 10 { 429 b = append(b, '0') 430 } 431 432 b = strconv.AppendInt(b, int64(t.Minute()), 10) 433 434 return string(b) 435} 436 437// FmtTimeMedium returns the medium time representation of 't' for 'ff' 438func (ff *ff) FmtTimeMedium(t time.Time) string { 439 440 b := make([]byte, 0, 32) 441 442 if t.Hour() < 10 { 443 b = append(b, '0') 444 } 445 446 b = strconv.AppendInt(b, int64(t.Hour()), 10) 447 b = append(b, ff.timeSeparator...) 448 449 if t.Minute() < 10 { 450 b = append(b, '0') 451 } 452 453 b = strconv.AppendInt(b, int64(t.Minute()), 10) 454 b = append(b, ff.timeSeparator...) 455 456 if t.Second() < 10 { 457 b = append(b, '0') 458 } 459 460 b = strconv.AppendInt(b, int64(t.Second()), 10) 461 462 return string(b) 463} 464 465// FmtTimeLong returns the long time representation of 't' for 'ff' 466func (ff *ff) FmtTimeLong(t time.Time) string { 467 468 b := make([]byte, 0, 32) 469 470 if t.Hour() < 10 { 471 b = append(b, '0') 472 } 473 474 b = strconv.AppendInt(b, int64(t.Hour()), 10) 475 b = append(b, ff.timeSeparator...) 476 477 if t.Minute() < 10 { 478 b = append(b, '0') 479 } 480 481 b = strconv.AppendInt(b, int64(t.Minute()), 10) 482 b = append(b, ff.timeSeparator...) 483 484 if t.Second() < 10 { 485 b = append(b, '0') 486 } 487 488 b = strconv.AppendInt(b, int64(t.Second()), 10) 489 b = append(b, []byte{0x20}...) 490 491 tz, _ := t.Zone() 492 b = append(b, tz...) 493 494 return string(b) 495} 496 497// FmtTimeFull returns the full time representation of 't' for 'ff' 498func (ff *ff) FmtTimeFull(t time.Time) string { 499 500 b := make([]byte, 0, 32) 501 502 if t.Hour() < 10 { 503 b = append(b, '0') 504 } 505 506 b = strconv.AppendInt(b, int64(t.Hour()), 10) 507 b = append(b, ff.timeSeparator...) 508 509 if t.Minute() < 10 { 510 b = append(b, '0') 511 } 512 513 b = strconv.AppendInt(b, int64(t.Minute()), 10) 514 b = append(b, ff.timeSeparator...) 515 516 if t.Second() < 10 { 517 b = append(b, '0') 518 } 519 520 b = strconv.AppendInt(b, int64(t.Second()), 10) 521 b = append(b, []byte{0x20}...) 522 523 tz, _ := t.Zone() 524 525 if btz, ok := ff.timezones[tz]; ok { 526 b = append(b, btz...) 527 } else { 528 b = append(b, tz...) 529 } 530 531 return string(b) 532} 533