mfe.go 16 KB

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