mas.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. package mas
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type mas 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 'mas' locale
  41. func New() locales.Translator {
  42. return &mas{
  43. locale: "mas",
  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 ", "Ksh", "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 "},
  49. currencyNegativePrefix: "(",
  50. currencyNegativeSuffix: ")",
  51. monthsAbbreviated: []string{"", "Dal", "Ará", "Ɔɛn", "Doy", "Lép", "Rok", "Sás", "Bɔ́r", "Kús", "Gís", "Shʉ́", "Ntʉ́"},
  52. monthsWide: []string{"", "Oladalʉ́", "Arát", "Ɔɛnɨ́ɔɨŋɔk", "Olodoyíóríê inkókúâ", "Oloilépūnyīē inkókúâ", "Kújúɔrɔk", "Mórusásin", "Ɔlɔ́ɨ́bɔ́rárɛ", "Kúshîn", "Olgísan", "Pʉshʉ́ka", "Ntʉ́ŋʉ́s"},
  53. daysAbbreviated: []string{"Jpi", "Jtt", "Jnn", "Jtn", "Alh", "Iju", "Jmo"},
  54. daysNarrow: []string{"2", "3", "4", "5", "6", "7", "1"},
  55. daysWide: []string{"Jumapílí", "Jumatátu", "Jumane", "Jumatánɔ", "Alaámisi", "Jumáa", "Jumamósi"},
  56. periodsAbbreviated: []string{"Ɛnkakɛnyá", "Ɛndámâ"},
  57. periodsWide: []string{"Ɛnkakɛnyá", "Ɛndámâ"},
  58. erasAbbreviated: []string{"MY", "EY"},
  59. erasNarrow: []string{"", ""},
  60. erasWide: []string{"Meínō Yɛ́sʉ", "Eínō Yɛ́sʉ"},
  61. timezones: map[string]string{"ChST": "ChST", "ACST": "ACST", "NZST": "NZST", "AEST": "AEST", "BOT": "BOT", "VET": "VET", "WEZ": "WEZ", "AEDT": "AEDT", "MYT": "MYT", "PST": "PST", "AWST": "AWST", "WITA": "WITA", "WIT": "WIT", "ART": "ART", "UYST": "UYST", "CAT": "CAT", "CLT": "CLT", "WAST": "WAST", "HKST": "HKST", "WIB": "WIB", "∅∅∅": "∅∅∅", "MDT": "MDT", "CLST": "CLST", "HAST": "HAST", "AKST": "AKST", "MEZ": "MEZ", "CHADT": "CHADT", "EST": "EST", "COT": "COT", "UYT": "UYT", "CST": "CST", "BT": "BT", "OEZ": "OEZ", "WAT": "WAT", "HAT": "HAT", "ARST": "ARST", "SRT": "SRT", "ACWST": "ACWST", "AWDT": "AWDT", "LHDT": "LHDT", "MST": "MST", "AST": "AST", "SAST": "SAST", "OESZ": "OESZ", "JDT": "JDT", "PDT": "PDT", "GMT": "GMT", "COST": "COST", "TMT": "TMT", "ACWDT": "ACWDT", "ECT": "ECT", "CHAST": "CHAST", "WESZ": "WESZ", "LHST": "LHST", "CDT": "CDT", "MESZ": "MESZ", "ADT": "ADT", "HADT": "HADT", "SGT": "SGT", "GYT": "GYT", "AKDT": "AKDT", "WARST": "WARST", "HKT": "HKT", "WART": "WART", "GFT": "GFT", "JST": "JST", "NZDT": "NZDT", "ACDT": "ACDT", "EDT": "EDT", "IST": "IST", "EAT": "EAT", "HNT": "HNT", "TMST": "TMST"},
  62. }
  63. }
  64. // Locale returns the current translators string locale
  65. func (mas *mas) Locale() string {
  66. return mas.locale
  67. }
  68. // PluralsCardinal returns the list of cardinal plural rules associated with 'mas'
  69. func (mas *mas) PluralsCardinal() []locales.PluralRule {
  70. return mas.pluralsCardinal
  71. }
  72. // PluralsOrdinal returns the list of ordinal plural rules associated with 'mas'
  73. func (mas *mas) PluralsOrdinal() []locales.PluralRule {
  74. return mas.pluralsOrdinal
  75. }
  76. // PluralsRange returns the list of range plural rules associated with 'mas'
  77. func (mas *mas) PluralsRange() []locales.PluralRule {
  78. return mas.pluralsRange
  79. }
  80. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'mas'
  81. func (mas *mas) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  82. n := math.Abs(num)
  83. if n == 1 {
  84. return locales.PluralRuleOne
  85. }
  86. return locales.PluralRuleOther
  87. }
  88. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'mas'
  89. func (mas *mas) 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 'mas'
  93. func (mas *mas) 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 (mas *mas) MonthAbbreviated(month time.Month) string {
  98. return mas.monthsAbbreviated[month]
  99. }
  100. // MonthsAbbreviated returns the locales abbreviated months
  101. func (mas *mas) MonthsAbbreviated() []string {
  102. return mas.monthsAbbreviated[1:]
  103. }
  104. // MonthNarrow returns the locales narrow month given the 'month' provided
  105. func (mas *mas) MonthNarrow(month time.Month) string {
  106. return mas.monthsNarrow[month]
  107. }
  108. // MonthsNarrow returns the locales narrow months
  109. func (mas *mas) MonthsNarrow() []string {
  110. return mas.monthsNarrow[1:]
  111. }
  112. // MonthWide returns the locales wide month given the 'month' provided
  113. func (mas *mas) MonthWide(month time.Month) string {
  114. return mas.monthsWide[month]
  115. }
  116. // MonthsWide returns the locales wide months
  117. func (mas *mas) MonthsWide() []string {
  118. return mas.monthsWide[1:]
  119. }
  120. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  121. func (mas *mas) WeekdayAbbreviated(weekday time.Weekday) string {
  122. return mas.daysAbbreviated[weekday]
  123. }
  124. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  125. func (mas *mas) WeekdaysAbbreviated() []string {
  126. return mas.daysAbbreviated
  127. }
  128. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  129. func (mas *mas) WeekdayNarrow(weekday time.Weekday) string {
  130. return mas.daysNarrow[weekday]
  131. }
  132. // WeekdaysNarrow returns the locales narrow weekdays
  133. func (mas *mas) WeekdaysNarrow() []string {
  134. return mas.daysNarrow
  135. }
  136. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  137. func (mas *mas) WeekdayShort(weekday time.Weekday) string {
  138. return mas.daysShort[weekday]
  139. }
  140. // WeekdaysShort returns the locales short weekdays
  141. func (mas *mas) WeekdaysShort() []string {
  142. return mas.daysShort
  143. }
  144. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  145. func (mas *mas) WeekdayWide(weekday time.Weekday) string {
  146. return mas.daysWide[weekday]
  147. }
  148. // WeekdaysWide returns the locales wide weekdays
  149. func (mas *mas) WeekdaysWide() []string {
  150. return mas.daysWide
  151. }
  152. // FmtNumber returns 'num' with digits/precision of 'v' for 'mas' and handles both Whole and Real numbers based on 'v'
  153. func (mas *mas) FmtNumber(num float64, v uint64) string {
  154. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  155. }
  156. // FmtPercent returns 'num' with digits/precision of 'v' for 'mas' and handles both Whole and Real numbers based on 'v'
  157. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  158. func (mas *mas) FmtPercent(num float64, v uint64) string {
  159. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  160. }
  161. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'mas'
  162. func (mas *mas) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  163. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  164. symbol := mas.currencies[currency]
  165. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  166. count := 0
  167. inWhole := v == 0
  168. b := make([]byte, 0, l)
  169. for i := len(s) - 1; i >= 0; i-- {
  170. if s[i] == '.' {
  171. b = append(b, mas.decimal[0])
  172. inWhole = true
  173. continue
  174. }
  175. if inWhole {
  176. if count == 3 {
  177. b = append(b, mas.group[0])
  178. count = 1
  179. } else {
  180. count++
  181. }
  182. }
  183. b = append(b, s[i])
  184. }
  185. for j := len(symbol) - 1; j >= 0; j-- {
  186. b = append(b, symbol[j])
  187. }
  188. if num < 0 {
  189. b = append(b, mas.minus[0])
  190. }
  191. // reverse
  192. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  193. b[i], b[j] = b[j], b[i]
  194. }
  195. if int(v) < 2 {
  196. if v == 0 {
  197. b = append(b, mas.decimal...)
  198. }
  199. for i := 0; i < 2-int(v); i++ {
  200. b = append(b, '0')
  201. }
  202. }
  203. return string(b)
  204. }
  205. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'mas'
  206. // in accounting notation.
  207. func (mas *mas) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  208. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  209. symbol := mas.currencies[currency]
  210. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  211. count := 0
  212. inWhole := v == 0
  213. b := make([]byte, 0, l)
  214. for i := len(s) - 1; i >= 0; i-- {
  215. if s[i] == '.' {
  216. b = append(b, mas.decimal[0])
  217. inWhole = true
  218. continue
  219. }
  220. if inWhole {
  221. if count == 3 {
  222. b = append(b, mas.group[0])
  223. count = 1
  224. } else {
  225. count++
  226. }
  227. }
  228. b = append(b, s[i])
  229. }
  230. if num < 0 {
  231. for j := len(symbol) - 1; j >= 0; j-- {
  232. b = append(b, symbol[j])
  233. }
  234. b = append(b, mas.currencyNegativePrefix[0])
  235. } else {
  236. for j := len(symbol) - 1; j >= 0; j-- {
  237. b = append(b, symbol[j])
  238. }
  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, mas.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, mas.currencyNegativeSuffix...)
  254. }
  255. return string(b)
  256. }
  257. // FmtDateShort returns the short date representation of 't' for 'mas'
  258. func (mas *mas) FmtDateShort(t time.Time) string {
  259. b := make([]byte, 0, 32)
  260. if t.Day() < 10 {
  261. b = append(b, '0')
  262. }
  263. b = strconv.AppendInt(b, int64(t.Day()), 10)
  264. b = append(b, []byte{0x2f}...)
  265. if t.Month() < 10 {
  266. b = append(b, '0')
  267. }
  268. b = strconv.AppendInt(b, int64(t.Month()), 10)
  269. b = append(b, []byte{0x2f}...)
  270. b = strconv.AppendInt(b, int64(t.Year()), 10)
  271. return string(b)
  272. }
  273. // FmtDateMedium returns the medium date representation of 't' for 'mas'
  274. func (mas *mas) FmtDateMedium(t time.Time) string {
  275. b := make([]byte, 0, 32)
  276. b = strconv.AppendInt(b, int64(t.Day()), 10)
  277. b = append(b, []byte{0x20}...)
  278. b = append(b, mas.monthsAbbreviated[t.Month()]...)
  279. b = append(b, []byte{0x20}...)
  280. b = strconv.AppendInt(b, int64(t.Year()), 10)
  281. return string(b)
  282. }
  283. // FmtDateLong returns the long date representation of 't' for 'mas'
  284. func (mas *mas) FmtDateLong(t time.Time) string {
  285. b := make([]byte, 0, 32)
  286. b = strconv.AppendInt(b, int64(t.Day()), 10)
  287. b = append(b, []byte{0x20}...)
  288. b = append(b, mas.monthsWide[t.Month()]...)
  289. b = append(b, []byte{0x20}...)
  290. b = strconv.AppendInt(b, int64(t.Year()), 10)
  291. return string(b)
  292. }
  293. // FmtDateFull returns the full date representation of 't' for 'mas'
  294. func (mas *mas) FmtDateFull(t time.Time) string {
  295. b := make([]byte, 0, 32)
  296. b = append(b, mas.daysWide[t.Weekday()]...)
  297. b = append(b, []byte{0x2c, 0x20}...)
  298. b = strconv.AppendInt(b, int64(t.Day()), 10)
  299. b = append(b, []byte{0x20}...)
  300. b = append(b, mas.monthsWide[t.Month()]...)
  301. b = append(b, []byte{0x20}...)
  302. b = strconv.AppendInt(b, int64(t.Year()), 10)
  303. return string(b)
  304. }
  305. // FmtTimeShort returns the short time representation of 't' for 'mas'
  306. func (mas *mas) FmtTimeShort(t time.Time) string {
  307. b := make([]byte, 0, 32)
  308. if t.Hour() < 10 {
  309. b = append(b, '0')
  310. }
  311. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  312. b = append(b, mas.timeSeparator...)
  313. if t.Minute() < 10 {
  314. b = append(b, '0')
  315. }
  316. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  317. return string(b)
  318. }
  319. // FmtTimeMedium returns the medium time representation of 't' for 'mas'
  320. func (mas *mas) FmtTimeMedium(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, mas.timeSeparator...)
  327. if t.Minute() < 10 {
  328. b = append(b, '0')
  329. }
  330. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  331. b = append(b, mas.timeSeparator...)
  332. if t.Second() < 10 {
  333. b = append(b, '0')
  334. }
  335. b = strconv.AppendInt(b, int64(t.Second()), 10)
  336. return string(b)
  337. }
  338. // FmtTimeLong returns the long time representation of 't' for 'mas'
  339. func (mas *mas) FmtTimeLong(t time.Time) string {
  340. b := make([]byte, 0, 32)
  341. if t.Hour() < 10 {
  342. b = append(b, '0')
  343. }
  344. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  345. b = append(b, mas.timeSeparator...)
  346. if t.Minute() < 10 {
  347. b = append(b, '0')
  348. }
  349. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  350. b = append(b, mas.timeSeparator...)
  351. if t.Second() < 10 {
  352. b = append(b, '0')
  353. }
  354. b = strconv.AppendInt(b, int64(t.Second()), 10)
  355. b = append(b, []byte{0x20}...)
  356. tz, _ := t.Zone()
  357. b = append(b, tz...)
  358. return string(b)
  359. }
  360. // FmtTimeFull returns the full time representation of 't' for 'mas'
  361. func (mas *mas) FmtTimeFull(t time.Time) string {
  362. b := make([]byte, 0, 32)
  363. if t.Hour() < 10 {
  364. b = append(b, '0')
  365. }
  366. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  367. b = append(b, mas.timeSeparator...)
  368. if t.Minute() < 10 {
  369. b = append(b, '0')
  370. }
  371. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  372. b = append(b, mas.timeSeparator...)
  373. if t.Second() < 10 {
  374. b = append(b, '0')
  375. }
  376. b = strconv.AppendInt(b, int64(t.Second()), 10)
  377. b = append(b, []byte{0x20}...)
  378. tz, _ := t.Zone()
  379. if btz, ok := mas.timezones[tz]; ok {
  380. b = append(b, btz...)
  381. } else {
  382. b = append(b, tz...)
  383. }
  384. return string(b)
  385. }