ses.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 ", "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 ", "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{"HAT": "HAT", "UYT": "UYT", "MESZ": "MESZ", "VET": "VET", "EAT": "EAT", "AWST": "AWST", "LHST": "LHST", "AKDT": "AKDT", "CLST": "CLST", "ACWST": "ACWST", "WITA": "WITA", "ARST": "ARST", "JDT": "JDT", "ChST": "ChST", "WEZ": "WEZ", "HKT": "HKT", "WART": "WART", "BOT": "BOT", "CHAST": "CHAST", "HAST": "HAST", "HADT": "HADT", "HKST": "HKST", "WARST": "WARST", "MYT": "MYT", "CLT": "CLT", "ADT": "ADT", "BT": "BT", "HNT": "HNT", "CHADT": "CHADT", "CAT": "CAT", "ACST": "ACST", "GYT": "GYT", "AKST": "AKST", "PDT": "PDT", "ECT": "ECT", "OESZ": "OESZ", "ACDT": "ACDT", "AWDT": "AWDT", "AEST": "AEST", "COST": "COST", "EST": "EST", "TMST": "TMST", "MST": "MST", "MEZ": "MEZ", "COT": "COT", "AEDT": "AEDT", "WIT": "WIT", "OEZ": "OEZ", "LHDT": "LHDT", "NZDT": "NZDT", "WAT": "WAT", "GFT": "GFT", "SRT": "SRT", "CST": "CST", "WESZ": "WESZ", "EDT": "EDT", "AST": "AST", "IST": "IST", "ART": "ART", "NZST": "NZST", "UYST": "UYST", "TMT": "TMT", "JST": "JST", "PST": "PST", "MDT": "MDT", "CDT": "CDT", "WAST": "WAST", "GMT": "GMT", "WIB": "WIB", "ACWDT": "ACWDT", "SAST": "SAST", "∅∅∅": "∅∅∅", "SGT": "SGT"},
  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. // FmtNumber returns 'num' with digits/precision of 'v' for 'ses' and handles both Whole and Real numbers based on 'v'
  147. func (ses *ses) FmtNumber(num float64, v uint64) string {
  148. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  149. }
  150. // FmtPercent returns 'num' with digits/precision of 'v' for 'ses' and handles both Whole and Real numbers based on 'v'
  151. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  152. func (ses *ses) FmtPercent(num float64, v uint64) string {
  153. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  154. }
  155. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ses'
  156. func (ses *ses) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  157. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  158. symbol := ses.currencies[currency]
  159. l := len(s) + len(symbol) + 0 + 2*len(s[:len(s)-int(v)-1])/3
  160. count := 0
  161. inWhole := v == 0
  162. b := make([]byte, 0, l)
  163. for i := len(s) - 1; i >= 0; i-- {
  164. if s[i] == '.' {
  165. b = append(b, ses.decimal[0])
  166. inWhole = true
  167. continue
  168. }
  169. if inWhole {
  170. if count == 3 {
  171. for j := len(ses.group) - 1; j >= 0; j-- {
  172. b = append(b, ses.group[j])
  173. }
  174. count = 1
  175. } else {
  176. count++
  177. }
  178. }
  179. b = append(b, s[i])
  180. }
  181. if num < 0 {
  182. b = append(b, ses.minus[0])
  183. }
  184. // reverse
  185. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  186. b[i], b[j] = b[j], b[i]
  187. }
  188. if int(v) < 2 {
  189. if v == 0 {
  190. b = append(b, ses.decimal...)
  191. }
  192. for i := 0; i < 2-int(v); i++ {
  193. b = append(b, '0')
  194. }
  195. }
  196. b = append(b, symbol...)
  197. return string(b)
  198. }
  199. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ses'
  200. // in accounting notation.
  201. func (ses *ses) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  202. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  203. symbol := ses.currencies[currency]
  204. l := len(s) + len(symbol) + 0 + 2*len(s[:len(s)-int(v)-1])/3
  205. count := 0
  206. inWhole := v == 0
  207. b := make([]byte, 0, l)
  208. for i := len(s) - 1; i >= 0; i-- {
  209. if s[i] == '.' {
  210. b = append(b, ses.decimal[0])
  211. inWhole = true
  212. continue
  213. }
  214. if inWhole {
  215. if count == 3 {
  216. for j := len(ses.group) - 1; j >= 0; j-- {
  217. b = append(b, ses.group[j])
  218. }
  219. count = 1
  220. } else {
  221. count++
  222. }
  223. }
  224. b = append(b, s[i])
  225. }
  226. if num < 0 {
  227. b = append(b, ses.minus[0])
  228. }
  229. // reverse
  230. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  231. b[i], b[j] = b[j], b[i]
  232. }
  233. if int(v) < 2 {
  234. if v == 0 {
  235. b = append(b, ses.decimal...)
  236. }
  237. for i := 0; i < 2-int(v); i++ {
  238. b = append(b, '0')
  239. }
  240. }
  241. if num < 0 {
  242. b = append(b, symbol...)
  243. } else {
  244. b = append(b, symbol...)
  245. }
  246. return string(b)
  247. }
  248. // FmtDateShort returns the short date representation of 't' for 'ses'
  249. func (ses *ses) FmtDateShort(t time.Time) string {
  250. b := make([]byte, 0, 32)
  251. b = strconv.AppendInt(b, int64(t.Day()), 10)
  252. b = append(b, []byte{0x2f}...)
  253. b = strconv.AppendInt(b, int64(t.Month()), 10)
  254. b = append(b, []byte{0x2f}...)
  255. b = strconv.AppendInt(b, int64(t.Year()), 10)
  256. return string(b)
  257. }
  258. // FmtDateMedium returns the medium date representation of 't' for 'ses'
  259. func (ses *ses) FmtDateMedium(t time.Time) string {
  260. b := make([]byte, 0, 32)
  261. b = strconv.AppendInt(b, int64(t.Day()), 10)
  262. b = append(b, []byte{0x20}...)
  263. b = append(b, ses.monthsAbbreviated[t.Month()]...)
  264. b = append(b, []byte{0x2c, 0x20}...)
  265. b = strconv.AppendInt(b, int64(t.Year()), 10)
  266. return string(b)
  267. }
  268. // FmtDateLong returns the long date representation of 't' for 'ses'
  269. func (ses *ses) FmtDateLong(t time.Time) string {
  270. b := make([]byte, 0, 32)
  271. b = strconv.AppendInt(b, int64(t.Day()), 10)
  272. b = append(b, []byte{0x20}...)
  273. b = append(b, ses.monthsWide[t.Month()]...)
  274. b = append(b, []byte{0x20}...)
  275. b = strconv.AppendInt(b, int64(t.Year()), 10)
  276. return string(b)
  277. }
  278. // FmtDateFull returns the full date representation of 't' for 'ses'
  279. func (ses *ses) FmtDateFull(t time.Time) string {
  280. b := make([]byte, 0, 32)
  281. b = append(b, ses.daysWide[t.Weekday()]...)
  282. b = append(b, []byte{0x20}...)
  283. b = strconv.AppendInt(b, int64(t.Day()), 10)
  284. b = append(b, []byte{0x20}...)
  285. b = append(b, ses.monthsWide[t.Month()]...)
  286. b = append(b, []byte{0x20}...)
  287. b = strconv.AppendInt(b, int64(t.Year()), 10)
  288. return string(b)
  289. }
  290. // FmtTimeShort returns the short time representation of 't' for 'ses'
  291. func (ses *ses) FmtTimeShort(t time.Time) string {
  292. b := make([]byte, 0, 32)
  293. if t.Hour() < 10 {
  294. b = append(b, '0')
  295. }
  296. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  297. b = append(b, ses.timeSeparator...)
  298. if t.Minute() < 10 {
  299. b = append(b, '0')
  300. }
  301. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  302. return string(b)
  303. }
  304. // FmtTimeMedium returns the medium time representation of 't' for 'ses'
  305. func (ses *ses) FmtTimeMedium(t time.Time) string {
  306. b := make([]byte, 0, 32)
  307. if t.Hour() < 10 {
  308. b = append(b, '0')
  309. }
  310. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  311. b = append(b, ses.timeSeparator...)
  312. if t.Minute() < 10 {
  313. b = append(b, '0')
  314. }
  315. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  316. b = append(b, ses.timeSeparator...)
  317. if t.Second() < 10 {
  318. b = append(b, '0')
  319. }
  320. b = strconv.AppendInt(b, int64(t.Second()), 10)
  321. return string(b)
  322. }
  323. // FmtTimeLong returns the long time representation of 't' for 'ses'
  324. func (ses *ses) FmtTimeLong(t time.Time) string {
  325. b := make([]byte, 0, 32)
  326. if t.Hour() < 10 {
  327. b = append(b, '0')
  328. }
  329. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  330. b = append(b, ses.timeSeparator...)
  331. if t.Minute() < 10 {
  332. b = append(b, '0')
  333. }
  334. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  335. b = append(b, ses.timeSeparator...)
  336. if t.Second() < 10 {
  337. b = append(b, '0')
  338. }
  339. b = strconv.AppendInt(b, int64(t.Second()), 10)
  340. b = append(b, []byte{0x20}...)
  341. tz, _ := t.Zone()
  342. b = append(b, tz...)
  343. return string(b)
  344. }
  345. // FmtTimeFull returns the full time representation of 't' for 'ses'
  346. func (ses *ses) FmtTimeFull(t time.Time) string {
  347. b := make([]byte, 0, 32)
  348. if t.Hour() < 10 {
  349. b = append(b, '0')
  350. }
  351. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  352. b = append(b, ses.timeSeparator...)
  353. if t.Minute() < 10 {
  354. b = append(b, '0')
  355. }
  356. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  357. b = append(b, ses.timeSeparator...)
  358. if t.Second() < 10 {
  359. b = append(b, '0')
  360. }
  361. b = strconv.AppendInt(b, int64(t.Second()), 10)
  362. b = append(b, []byte{0x20}...)
  363. tz, _ := t.Zone()
  364. if btz, ok := ses.timezones[tz]; ok {
  365. b = append(b, btz...)
  366. } else {
  367. b = append(b, tz...)
  368. }
  369. return string(b)
  370. }