ksb.go 15 KB

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