ses.go 16 KB

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