khq.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. package khq
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type khq 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 'khq' locale
  39. func New() locales.Translator {
  40. return &khq{
  41. locale: "khq",
  42. pluralsCardinal: nil,
  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", "Alj", "Ass"},
  52. daysNarrow: []string{"H", "T", "T", "L", "L", "L", "S"},
  53. daysWide: []string{"Alhadi", "Atini", "Atalata", "Alarba", "Alhamiisa", "Aljuma", "Assabdu"},
  54. periodsAbbreviated: []string{"Adduha", "Aluula"},
  55. periodsWide: []string{"Adduha", "Aluula"},
  56. erasAbbreviated: []string{"IJ", "IZ"},
  57. erasNarrow: []string{"", ""},
  58. erasWide: []string{"Isaa jine", "Isaa jamanoo"},
  59. timezones: map[string]string{"HEPM": "HEPM", "ChST": "ChST", "JDT": "JDT", "HEOG": "HEOG", "HNPM": "HNPM", "HEPMX": "HEPMX", "AEDT": "AEDT", "HNT": "HNT", "CAT": "CAT", "NZDT": "NZDT", "HENOMX": "HENOMX", "HADT": "HADT", "MEZ": "MEZ", "CLST": "CLST", "CHADT": "CHADT", "HECU": "HECU", "CDT": "CDT", "MST": "MST", "ACWST": "ACWST", "HNOG": "HNOG", "SRT": "SRT", "OEZ": "OEZ", "HNPMX": "HNPMX", "WESZ": "WESZ", "MESZ": "MESZ", "HKST": "HKST", "WAT": "WAT", "ARST": "ARST", "CHAST": "CHAST", "WIB": "WIB", "BT": "BT", "WITA": "WITA", "GMT": "GMT", "GFT": "GFT", "AKST": "AKST", "EDT": "EDT", "HNEG": "HNEG", "LHST": "LHST", "HAT": "HAT", "EAT": "EAT", "COST": "COST", "ADT": "ADT", "AEST": "AEST", "ACST": "ACST", "ECT": "ECT", "ART": "ART", "MYT": "MYT", "BOT": "BOT", "EST": "EST", "IST": "IST", "WARST": "WARST", "AST": "AST", "WAST": "WAST", "NZST": "NZST", "TMST": "TMST", "PDT": "PDT", "LHDT": "LHDT", "UYST": "UYST", "AWDT": "AWDT", "MDT": "MDT", "SAST": "SAST", "HEEG": "HEEG", "PST": "PST", "HKT": "HKT", "VET": "VET", "CST": "CST", "ACWDT": "ACWDT", "HNNOMX": "HNNOMX", "CLT": "CLT", "TMT": "TMT", "HAST": "HAST", "WEZ": "WEZ", "∅∅∅": "∅∅∅", "ACDT": "ACDT", "HNCU": "HNCU", "GYT": "GYT", "UYT": "UYT", "AWST": "AWST", "JST": "JST", "WART": "WART", "OESZ": "OESZ", "COT": "COT", "AKDT": "AKDT", "SGT": "SGT", "WIT": "WIT"},
  60. }
  61. }
  62. // Locale returns the current translators string locale
  63. func (khq *khq) Locale() string {
  64. return khq.locale
  65. }
  66. // PluralsCardinal returns the list of cardinal plural rules associated with 'khq'
  67. func (khq *khq) PluralsCardinal() []locales.PluralRule {
  68. return khq.pluralsCardinal
  69. }
  70. // PluralsOrdinal returns the list of ordinal plural rules associated with 'khq'
  71. func (khq *khq) PluralsOrdinal() []locales.PluralRule {
  72. return khq.pluralsOrdinal
  73. }
  74. // PluralsRange returns the list of range plural rules associated with 'khq'
  75. func (khq *khq) PluralsRange() []locales.PluralRule {
  76. return khq.pluralsRange
  77. }
  78. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'khq'
  79. func (khq *khq) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  80. return locales.PluralRuleUnknown
  81. }
  82. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'khq'
  83. func (khq *khq) 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 'khq'
  87. func (khq *khq) 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 (khq *khq) MonthAbbreviated(month time.Month) string {
  92. return khq.monthsAbbreviated[month]
  93. }
  94. // MonthsAbbreviated returns the locales abbreviated months
  95. func (khq *khq) MonthsAbbreviated() []string {
  96. return khq.monthsAbbreviated[1:]
  97. }
  98. // MonthNarrow returns the locales narrow month given the 'month' provided
  99. func (khq *khq) MonthNarrow(month time.Month) string {
  100. return khq.monthsNarrow[month]
  101. }
  102. // MonthsNarrow returns the locales narrow months
  103. func (khq *khq) MonthsNarrow() []string {
  104. return khq.monthsNarrow[1:]
  105. }
  106. // MonthWide returns the locales wide month given the 'month' provided
  107. func (khq *khq) MonthWide(month time.Month) string {
  108. return khq.monthsWide[month]
  109. }
  110. // MonthsWide returns the locales wide months
  111. func (khq *khq) MonthsWide() []string {
  112. return khq.monthsWide[1:]
  113. }
  114. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  115. func (khq *khq) WeekdayAbbreviated(weekday time.Weekday) string {
  116. return khq.daysAbbreviated[weekday]
  117. }
  118. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  119. func (khq *khq) WeekdaysAbbreviated() []string {
  120. return khq.daysAbbreviated
  121. }
  122. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  123. func (khq *khq) WeekdayNarrow(weekday time.Weekday) string {
  124. return khq.daysNarrow[weekday]
  125. }
  126. // WeekdaysNarrow returns the locales narrow weekdays
  127. func (khq *khq) WeekdaysNarrow() []string {
  128. return khq.daysNarrow
  129. }
  130. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  131. func (khq *khq) WeekdayShort(weekday time.Weekday) string {
  132. return khq.daysShort[weekday]
  133. }
  134. // WeekdaysShort returns the locales short weekdays
  135. func (khq *khq) WeekdaysShort() []string {
  136. return khq.daysShort
  137. }
  138. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  139. func (khq *khq) WeekdayWide(weekday time.Weekday) string {
  140. return khq.daysWide[weekday]
  141. }
  142. // WeekdaysWide returns the locales wide weekdays
  143. func (khq *khq) WeekdaysWide() []string {
  144. return khq.daysWide
  145. }
  146. // Decimal returns the decimal point of number
  147. func (khq *khq) Decimal() string {
  148. return khq.decimal
  149. }
  150. // Group returns the group of number
  151. func (khq *khq) Group() string {
  152. return khq.group
  153. }
  154. // Group returns the minus sign of number
  155. func (khq *khq) Minus() string {
  156. return khq.minus
  157. }
  158. // FmtNumber returns 'num' with digits/precision of 'v' for 'khq' and handles both Whole and Real numbers based on 'v'
  159. func (khq *khq) 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 'khq' 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 (khq *khq) 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 'khq'
  168. func (khq *khq) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  169. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  170. symbol := khq.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, khq.decimal[0])
  178. inWhole = true
  179. continue
  180. }
  181. if inWhole {
  182. if count == 3 {
  183. for j := len(khq.group) - 1; j >= 0; j-- {
  184. b = append(b, khq.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, khq.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, khq.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 'khq'
  212. // in accounting notation.
  213. func (khq *khq) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  214. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  215. symbol := khq.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, khq.decimal[0])
  223. inWhole = true
  224. continue
  225. }
  226. if inWhole {
  227. if count == 3 {
  228. for j := len(khq.group) - 1; j >= 0; j-- {
  229. b = append(b, khq.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, khq.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, khq.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 'khq'
  261. func (khq *khq) 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 'khq'
  275. func (khq *khq) 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, khq.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 'khq'
  289. func (khq *khq) 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, khq.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 'khq'
  303. func (khq *khq) FmtDateFull(t time.Time) string {
  304. b := make([]byte, 0, 32)
  305. b = append(b, khq.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, khq.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 'khq'
  319. func (khq *khq) 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, khq.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 'khq'
  333. func (khq *khq) 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, khq.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, khq.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 'khq'
  352. func (khq *khq) 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, khq.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, khq.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 'khq'
  374. func (khq *khq) 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, khq.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, khq.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 := khq.timezones[tz]; ok {
  393. b = append(b, btz...)
  394. } else {
  395. b = append(b, tz...)
  396. }
  397. return string(b)
  398. }