bm.go 16 KB

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