bem.go 16 KB

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