ff.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package ff
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ff struct {
  10. locale string
  11. pluralsCardinal []locales.PluralRule
  12. pluralsOrdinal []locales.PluralRule
  13. pluralsRange []locales.PluralRule
  14. decimal string
  15. group string
  16. minus string
  17. percent string
  18. perMille string
  19. timeSeparator string
  20. inifinity string
  21. currencies []string // idx = enum of currency code
  22. currencyPositiveSuffix string
  23. currencyNegativeSuffix string
  24. monthsAbbreviated []string
  25. monthsNarrow []string
  26. monthsWide []string
  27. daysAbbreviated []string
  28. daysNarrow []string
  29. daysShort []string
  30. daysWide []string
  31. periodsAbbreviated []string
  32. periodsNarrow []string
  33. periodsShort []string
  34. periodsWide []string
  35. erasAbbreviated []string
  36. erasNarrow []string
  37. erasWide []string
  38. timezones map[string]string
  39. }
  40. // New returns a new instance of translator for the 'ff' locale
  41. func New() locales.Translator {
  42. return &ff{
  43. locale: "ff",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  47. decimal: ",",
  48. group: " ",
  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", "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", "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", "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", "UZS", "VEB", "VEF", "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. currencyPositiveSuffix: " ",
  52. currencyNegativeSuffix: " ",
  53. monthsAbbreviated: []string{"", "sii", "col", "mbo", "see", "duu", "kor", "mor", "juk", "slt", "yar", "jol", "bow"},
  54. monthsNarrow: []string{"", "s", "c", "m", "s", "d", "k", "m", "j", "s", "y", "j", "b"},
  55. monthsWide: []string{"", "siilo", "colte", "mbooy", "seeɗto", "duujal", "korse", "morso", "juko", "siilto", "yarkomaa", "jolal", "bowte"},
  56. daysAbbreviated: []string{"dew", "aaɓ", "maw", "nje", "naa", "mwd", "hbi"},
  57. daysNarrow: []string{"d", "a", "m", "n", "n", "m", "h"},
  58. daysWide: []string{"dewo", "aaɓnde", "mawbaare", "njeslaare", "naasaande", "mawnde", "hoore-biir"},
  59. periodsAbbreviated: []string{"subaka", "kikiiɗe"},
  60. periodsWide: []string{"subaka", "kikiiɗe"},
  61. erasAbbreviated: []string{"H-I", "C-I"},
  62. erasNarrow: []string{"", ""},
  63. erasWide: []string{"Hade Iisa", "Caggal Iisa"},
  64. timezones: map[string]string{"TMT": "TMT", "WARST": "WARST", "HNT": "HNT", "CLT": "CLT", "CLST": "CLST", "AKDT": "AKDT", "MESZ": "MESZ", "HNPM": "HNPM", "ACDT": "ACDT", "HNNOMX": "HNNOMX", "COST": "COST", "AEST": "AEST", "CAT": "CAT", "SRT": "SRT", "JDT": "JDT", "TMST": "TMST", "ChST": "ChST", "BOT": "BOT", "CST": "CST", "UYST": "UYST", "WIT": "WIT", "WESZ": "WESZ", "GMT": "GMT", "SGT": "SGT", "EST": "EST", "AKST": "AKST", "HEPM": "HEPM", "HEOG": "HEOG", "HKT": "HKT", "∅∅∅": "∅∅∅", "EDT": "EDT", "AST": "AST", "UYT": "UYT", "WAST": "WAST", "CHADT": "CHADT", "MEZ": "MEZ", "ACST": "ACST", "HENOMX": "HENOMX", "ACWDT": "ACWDT", "HAT": "HAT", "WEZ": "WEZ", "NZST": "NZST", "HEEG": "HEEG", "EAT": "EAT", "HAST": "HAST", "ARST": "ARST", "WIB": "WIB", "ACWST": "ACWST", "WITA": "WITA", "AEDT": "AEDT", "GFT": "GFT", "ECT": "ECT", "HEPMX": "HEPMX", "ADT": "ADT", "LHST": "LHST", "GYT": "GYT", "VET": "VET", "HKST": "HKST", "LHDT": "LHDT", "HNCU": "HNCU", "MST": "MST", "MYT": "MYT", "AWST": "AWST", "HNOG": "HNOG", "NZDT": "NZDT", "JST": "JST", "HNEG": "HNEG", "OEZ": "OEZ", "HNPMX": "HNPMX", "ART": "ART", "PDT": "PDT", "CHAST": "CHAST", "SAST": "SAST", "IST": "IST", "WART": "WART", "BT": "BT", "AWDT": "AWDT", "WAT": "WAT", "COT": "COT", "CDT": "CDT", "PST": "PST", "OESZ": "OESZ", "HECU": "HECU", "MDT": "MDT", "HADT": "HADT"},
  65. }
  66. }
  67. // Locale returns the current translators string locale
  68. func (ff *ff) Locale() string {
  69. return ff.locale
  70. }
  71. // PluralsCardinal returns the list of cardinal plural rules associated with 'ff'
  72. func (ff *ff) PluralsCardinal() []locales.PluralRule {
  73. return ff.pluralsCardinal
  74. }
  75. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ff'
  76. func (ff *ff) PluralsOrdinal() []locales.PluralRule {
  77. return ff.pluralsOrdinal
  78. }
  79. // PluralsRange returns the list of range plural rules associated with 'ff'
  80. func (ff *ff) PluralsRange() []locales.PluralRule {
  81. return ff.pluralsRange
  82. }
  83. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ff'
  84. func (ff *ff) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. n := math.Abs(num)
  86. i := int64(n)
  87. if i == 0 || i == 1 {
  88. return locales.PluralRuleOne
  89. }
  90. return locales.PluralRuleOther
  91. }
  92. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ff'
  93. func (ff *ff) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  94. return locales.PluralRuleUnknown
  95. }
  96. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ff'
  97. func (ff *ff) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  98. return locales.PluralRuleUnknown
  99. }
  100. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  101. func (ff *ff) MonthAbbreviated(month time.Month) string {
  102. return ff.monthsAbbreviated[month]
  103. }
  104. // MonthsAbbreviated returns the locales abbreviated months
  105. func (ff *ff) MonthsAbbreviated() []string {
  106. return ff.monthsAbbreviated[1:]
  107. }
  108. // MonthNarrow returns the locales narrow month given the 'month' provided
  109. func (ff *ff) MonthNarrow(month time.Month) string {
  110. return ff.monthsNarrow[month]
  111. }
  112. // MonthsNarrow returns the locales narrow months
  113. func (ff *ff) MonthsNarrow() []string {
  114. return ff.monthsNarrow[1:]
  115. }
  116. // MonthWide returns the locales wide month given the 'month' provided
  117. func (ff *ff) MonthWide(month time.Month) string {
  118. return ff.monthsWide[month]
  119. }
  120. // MonthsWide returns the locales wide months
  121. func (ff *ff) MonthsWide() []string {
  122. return ff.monthsWide[1:]
  123. }
  124. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  125. func (ff *ff) WeekdayAbbreviated(weekday time.Weekday) string {
  126. return ff.daysAbbreviated[weekday]
  127. }
  128. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  129. func (ff *ff) WeekdaysAbbreviated() []string {
  130. return ff.daysAbbreviated
  131. }
  132. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  133. func (ff *ff) WeekdayNarrow(weekday time.Weekday) string {
  134. return ff.daysNarrow[weekday]
  135. }
  136. // WeekdaysNarrow returns the locales narrow weekdays
  137. func (ff *ff) WeekdaysNarrow() []string {
  138. return ff.daysNarrow
  139. }
  140. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  141. func (ff *ff) WeekdayShort(weekday time.Weekday) string {
  142. return ff.daysShort[weekday]
  143. }
  144. // WeekdaysShort returns the locales short weekdays
  145. func (ff *ff) WeekdaysShort() []string {
  146. return ff.daysShort
  147. }
  148. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  149. func (ff *ff) WeekdayWide(weekday time.Weekday) string {
  150. return ff.daysWide[weekday]
  151. }
  152. // WeekdaysWide returns the locales wide weekdays
  153. func (ff *ff) WeekdaysWide() []string {
  154. return ff.daysWide
  155. }
  156. // FmtNumber returns 'num' with digits/precision of 'v' for 'ff' and handles both Whole and Real numbers based on 'v'
  157. func (ff *ff) FmtNumber(num float64, v uint64) string {
  158. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  159. }
  160. // FmtPercent returns 'num' with digits/precision of 'v' for 'ff' and handles both Whole and Real numbers based on 'v'
  161. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  162. func (ff *ff) FmtPercent(num float64, v uint64) string {
  163. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  164. }
  165. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ff'
  166. func (ff *ff) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  167. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  168. symbol := ff.currencies[currency]
  169. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  170. count := 0
  171. inWhole := v == 0
  172. b := make([]byte, 0, l)
  173. for i := len(s) - 1; i >= 0; i-- {
  174. if s[i] == '.' {
  175. b = append(b, ff.decimal[0])
  176. inWhole = true
  177. continue
  178. }
  179. if inWhole {
  180. if count == 3 {
  181. for j := len(ff.group) - 1; j >= 0; j-- {
  182. b = append(b, ff.group[j])
  183. }
  184. count = 1
  185. } else {
  186. count++
  187. }
  188. }
  189. b = append(b, s[i])
  190. }
  191. if num < 0 {
  192. b = append(b, ff.minus[0])
  193. }
  194. // reverse
  195. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  196. b[i], b[j] = b[j], b[i]
  197. }
  198. if int(v) < 2 {
  199. if v == 0 {
  200. b = append(b, ff.decimal...)
  201. }
  202. for i := 0; i < 2-int(v); i++ {
  203. b = append(b, '0')
  204. }
  205. }
  206. b = append(b, ff.currencyPositiveSuffix...)
  207. b = append(b, symbol...)
  208. return string(b)
  209. }
  210. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ff'
  211. // in accounting notation.
  212. func (ff *ff) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  213. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  214. symbol := ff.currencies[currency]
  215. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  216. count := 0
  217. inWhole := v == 0
  218. b := make([]byte, 0, l)
  219. for i := len(s) - 1; i >= 0; i-- {
  220. if s[i] == '.' {
  221. b = append(b, ff.decimal[0])
  222. inWhole = true
  223. continue
  224. }
  225. if inWhole {
  226. if count == 3 {
  227. for j := len(ff.group) - 1; j >= 0; j-- {
  228. b = append(b, ff.group[j])
  229. }
  230. count = 1
  231. } else {
  232. count++
  233. }
  234. }
  235. b = append(b, s[i])
  236. }
  237. if num < 0 {
  238. b = append(b, ff.minus[0])
  239. }
  240. // reverse
  241. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  242. b[i], b[j] = b[j], b[i]
  243. }
  244. if int(v) < 2 {
  245. if v == 0 {
  246. b = append(b, ff.decimal...)
  247. }
  248. for i := 0; i < 2-int(v); i++ {
  249. b = append(b, '0')
  250. }
  251. }
  252. if num < 0 {
  253. b = append(b, ff.currencyNegativeSuffix...)
  254. b = append(b, symbol...)
  255. } else {
  256. b = append(b, ff.currencyPositiveSuffix...)
  257. b = append(b, symbol...)
  258. }
  259. return string(b)
  260. }
  261. // FmtDateShort returns the short date representation of 't' for 'ff'
  262. func (ff *ff) FmtDateShort(t time.Time) string {
  263. b := make([]byte, 0, 32)
  264. b = strconv.AppendInt(b, int64(t.Day()), 10)
  265. b = append(b, []byte{0x2f}...)
  266. b = strconv.AppendInt(b, int64(t.Month()), 10)
  267. b = append(b, []byte{0x2f}...)
  268. if t.Year() > 0 {
  269. b = strconv.AppendInt(b, int64(t.Year()), 10)
  270. } else {
  271. b = strconv.AppendInt(b, int64(t.Year()*-1), 10)
  272. }
  273. return string(b)
  274. }
  275. // FmtDateMedium returns the medium date representation of 't' for 'ff'
  276. func (ff *ff) FmtDateMedium(t time.Time) string {
  277. b := make([]byte, 0, 32)
  278. b = strconv.AppendInt(b, int64(t.Day()), 10)
  279. b = append(b, []byte{0x20}...)
  280. b = append(b, ff.monthsAbbreviated[t.Month()]...)
  281. b = append(b, []byte{0x2c, 0x20}...)
  282. if t.Year() > 0 {
  283. b = strconv.AppendInt(b, int64(t.Year()), 10)
  284. } else {
  285. b = strconv.AppendInt(b, int64(t.Year()*-1), 10)
  286. }
  287. return string(b)
  288. }
  289. // FmtDateLong returns the long date representation of 't' for 'ff'
  290. func (ff *ff) FmtDateLong(t time.Time) string {
  291. b := make([]byte, 0, 32)
  292. b = strconv.AppendInt(b, int64(t.Day()), 10)
  293. b = append(b, []byte{0x20}...)
  294. b = append(b, ff.monthsWide[t.Month()]...)
  295. b = append(b, []byte{0x20}...)
  296. if t.Year() > 0 {
  297. b = strconv.AppendInt(b, int64(t.Year()), 10)
  298. } else {
  299. b = strconv.AppendInt(b, int64(t.Year()*-1), 10)
  300. }
  301. return string(b)
  302. }
  303. // FmtDateFull returns the full date representation of 't' for 'ff'
  304. func (ff *ff) FmtDateFull(t time.Time) string {
  305. b := make([]byte, 0, 32)
  306. b = append(b, ff.daysWide[t.Weekday()]...)
  307. b = append(b, []byte{0x20}...)
  308. b = strconv.AppendInt(b, int64(t.Day()), 10)
  309. b = append(b, []byte{0x20}...)
  310. b = append(b, ff.monthsWide[t.Month()]...)
  311. b = append(b, []byte{0x20}...)
  312. if t.Year() > 0 {
  313. b = strconv.AppendInt(b, int64(t.Year()), 10)
  314. } else {
  315. b = strconv.AppendInt(b, int64(t.Year()*-1), 10)
  316. }
  317. return string(b)
  318. }
  319. // FmtTimeShort returns the short time representation of 't' for 'ff'
  320. func (ff *ff) FmtTimeShort(t time.Time) string {
  321. b := make([]byte, 0, 32)
  322. if t.Hour() < 10 {
  323. b = append(b, '0')
  324. }
  325. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  326. b = append(b, ff.timeSeparator...)
  327. if t.Minute() < 10 {
  328. b = append(b, '0')
  329. }
  330. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  331. return string(b)
  332. }
  333. // FmtTimeMedium returns the medium time representation of 't' for 'ff'
  334. func (ff *ff) FmtTimeMedium(t time.Time) string {
  335. b := make([]byte, 0, 32)
  336. if t.Hour() < 10 {
  337. b = append(b, '0')
  338. }
  339. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  340. b = append(b, ff.timeSeparator...)
  341. if t.Minute() < 10 {
  342. b = append(b, '0')
  343. }
  344. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  345. b = append(b, ff.timeSeparator...)
  346. if t.Second() < 10 {
  347. b = append(b, '0')
  348. }
  349. b = strconv.AppendInt(b, int64(t.Second()), 10)
  350. return string(b)
  351. }
  352. // FmtTimeLong returns the long time representation of 't' for 'ff'
  353. func (ff *ff) FmtTimeLong(t time.Time) string {
  354. b := make([]byte, 0, 32)
  355. if t.Hour() < 10 {
  356. b = append(b, '0')
  357. }
  358. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  359. b = append(b, ff.timeSeparator...)
  360. if t.Minute() < 10 {
  361. b = append(b, '0')
  362. }
  363. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  364. b = append(b, ff.timeSeparator...)
  365. if t.Second() < 10 {
  366. b = append(b, '0')
  367. }
  368. b = strconv.AppendInt(b, int64(t.Second()), 10)
  369. b = append(b, []byte{0x20}...)
  370. tz, _ := t.Zone()
  371. b = append(b, tz...)
  372. return string(b)
  373. }
  374. // FmtTimeFull returns the full time representation of 't' for 'ff'
  375. func (ff *ff) FmtTimeFull(t time.Time) string {
  376. b := make([]byte, 0, 32)
  377. if t.Hour() < 10 {
  378. b = append(b, '0')
  379. }
  380. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  381. b = append(b, ff.timeSeparator...)
  382. if t.Minute() < 10 {
  383. b = append(b, '0')
  384. }
  385. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  386. b = append(b, ff.timeSeparator...)
  387. if t.Second() < 10 {
  388. b = append(b, '0')
  389. }
  390. b = strconv.AppendInt(b, int64(t.Second()), 10)
  391. b = append(b, []byte{0x20}...)
  392. tz, _ := t.Zone()
  393. if btz, ok := ff.timezones[tz]; ok {
  394. b = append(b, btz...)
  395. } else {
  396. b = append(b, tz...)
  397. }
  398. return string(b)
  399. }