luy.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package luy
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type luy 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 'luy' locale
  39. func New() locales.Translator {
  40. return &luy{
  41. locale: "luy",
  42. pluralsCardinal: nil,
  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 ", "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 "},
  47. monthsAbbreviated: []string{"", "Jan", "Feb", "Mar", "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{"", "Januari", "Februari", "Machi", "Aprili", "Mei", "Juni", "Julai", "Agosti", "Septemba", "Oktoba", "Novemba", "Desemba"},
  50. daysAbbreviated: []string{"J2", "J3", "J4", "J5", "Al", "Ij", "J1"},
  51. daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
  52. daysWide: []string{"Jumapiri", "Jumatatu", "Jumanne", "Jumatano", "Murwa wa Kanne", "Murwa wa Katano", "Jumamosi"},
  53. periodsAbbreviated: []string{"a.m.", "p.m."},
  54. periodsWide: []string{"a.m.", "p.m."},
  55. erasAbbreviated: []string{"BC", "AD"},
  56. erasNarrow: []string{"", ""},
  57. erasWide: []string{"Imberi ya Kuuza Kwa", "Muhiga Kuvita Kuuza"},
  58. timezones: map[string]string{"SRT": "SRT", "PDT": "PDT", "CHAST": "CHAST", "EST": "EST", "SGT": "SGT", "WEZ": "WEZ", "COST": "COST", "HKST": "HKST", "AWDT": "AWDT", "MESZ": "MESZ", "UYST": "UYST", "WARST": "WARST", "CDT": "CDT", "ACWDT": "ACWDT", "ECT": "ECT", "HAST": "HAST", "WITA": "WITA", "JDT": "JDT", "PST": "PST", "UYT": "UYT", "SAST": "SAST", "GYT": "GYT", "ACWST": "ACWST", "ChST": "ChST", "CHADT": "CHADT", "EDT": "EDT", "HAT": "HAT", "ACDT": "ACDT", "AKST": "AKST", "MEZ": "MEZ", "AEST": "AEST", "MDT": "MDT", "VET": "VET", "AST": "AST", "∅∅∅": "∅∅∅", "JST": "JST", "CST": "CST", "CAT": "CAT", "WART": "WART", "LHST": "LHST", "LHDT": "LHDT", "WESZ": "WESZ", "COT": "COT", "WIT": "WIT", "OEZ": "OEZ", "GMT": "GMT", "HNT": "HNT", "MST": "MST", "ARST": "ARST", "TMST": "TMST", "WAT": "WAT", "HKT": "HKT", "IST": "IST", "ART": "ART", "AWST": "AWST", "CLST": "CLST", "HADT": "HADT", "OESZ": "OESZ", "BT": "BT", "GFT": "GFT", "ACST": "ACST", "TMT": "TMT", "AKDT": "AKDT", "NZST": "NZST", "CLT": "CLT", "AEDT": "AEDT", "WIB": "WIB", "EAT": "EAT", "MYT": "MYT", "NZDT": "NZDT", "BOT": "BOT", "ADT": "ADT", "WAST": "WAST"},
  59. }
  60. }
  61. // Locale returns the current translators string locale
  62. func (luy *luy) Locale() string {
  63. return luy.locale
  64. }
  65. // PluralsCardinal returns the list of cardinal plural rules associated with 'luy'
  66. func (luy *luy) PluralsCardinal() []locales.PluralRule {
  67. return luy.pluralsCardinal
  68. }
  69. // PluralsOrdinal returns the list of ordinal plural rules associated with 'luy'
  70. func (luy *luy) PluralsOrdinal() []locales.PluralRule {
  71. return luy.pluralsOrdinal
  72. }
  73. // PluralsRange returns the list of range plural rules associated with 'luy'
  74. func (luy *luy) PluralsRange() []locales.PluralRule {
  75. return luy.pluralsRange
  76. }
  77. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'luy'
  78. func (luy *luy) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  79. return locales.PluralRuleUnknown
  80. }
  81. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'luy'
  82. func (luy *luy) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  83. return locales.PluralRuleUnknown
  84. }
  85. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'luy'
  86. func (luy *luy) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  87. return locales.PluralRuleUnknown
  88. }
  89. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  90. func (luy *luy) MonthAbbreviated(month time.Month) string {
  91. return luy.monthsAbbreviated[month]
  92. }
  93. // MonthsAbbreviated returns the locales abbreviated months
  94. func (luy *luy) MonthsAbbreviated() []string {
  95. return luy.monthsAbbreviated[1:]
  96. }
  97. // MonthNarrow returns the locales narrow month given the 'month' provided
  98. func (luy *luy) MonthNarrow(month time.Month) string {
  99. return luy.monthsNarrow[month]
  100. }
  101. // MonthsNarrow returns the locales narrow months
  102. func (luy *luy) MonthsNarrow() []string {
  103. return luy.monthsNarrow[1:]
  104. }
  105. // MonthWide returns the locales wide month given the 'month' provided
  106. func (luy *luy) MonthWide(month time.Month) string {
  107. return luy.monthsWide[month]
  108. }
  109. // MonthsWide returns the locales wide months
  110. func (luy *luy) MonthsWide() []string {
  111. return luy.monthsWide[1:]
  112. }
  113. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  114. func (luy *luy) WeekdayAbbreviated(weekday time.Weekday) string {
  115. return luy.daysAbbreviated[weekday]
  116. }
  117. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  118. func (luy *luy) WeekdaysAbbreviated() []string {
  119. return luy.daysAbbreviated
  120. }
  121. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  122. func (luy *luy) WeekdayNarrow(weekday time.Weekday) string {
  123. return luy.daysNarrow[weekday]
  124. }
  125. // WeekdaysNarrow returns the locales narrow weekdays
  126. func (luy *luy) WeekdaysNarrow() []string {
  127. return luy.daysNarrow
  128. }
  129. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  130. func (luy *luy) WeekdayShort(weekday time.Weekday) string {
  131. return luy.daysShort[weekday]
  132. }
  133. // WeekdaysShort returns the locales short weekdays
  134. func (luy *luy) WeekdaysShort() []string {
  135. return luy.daysShort
  136. }
  137. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  138. func (luy *luy) WeekdayWide(weekday time.Weekday) string {
  139. return luy.daysWide[weekday]
  140. }
  141. // WeekdaysWide returns the locales wide weekdays
  142. func (luy *luy) WeekdaysWide() []string {
  143. return luy.daysWide
  144. }
  145. // FmtNumber returns 'num' with digits/precision of 'v' for 'luy' and handles both Whole and Real numbers based on 'v'
  146. func (luy *luy) FmtNumber(num float64, v uint64) string {
  147. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  148. }
  149. // FmtPercent returns 'num' with digits/precision of 'v' for 'luy' and handles both Whole and Real numbers based on 'v'
  150. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  151. func (luy *luy) FmtPercent(num float64, v uint64) string {
  152. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  153. }
  154. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'luy'
  155. func (luy *luy) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  156. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  157. symbol := luy.currencies[currency]
  158. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  159. count := 0
  160. inWhole := v == 0
  161. b := make([]byte, 0, l)
  162. for i := len(s) - 1; i >= 0; i-- {
  163. if s[i] == '.' {
  164. b = append(b, luy.decimal[0])
  165. inWhole = true
  166. continue
  167. }
  168. if inWhole {
  169. if count == 3 {
  170. b = append(b, luy.group[0])
  171. count = 1
  172. } else {
  173. count++
  174. }
  175. }
  176. b = append(b, s[i])
  177. }
  178. for j := len(symbol) - 1; j >= 0; j-- {
  179. b = append(b, symbol[j])
  180. }
  181. if num < 0 {
  182. b = append(b, luy.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, luy.decimal...)
  191. }
  192. for i := 0; i < 2-int(v); i++ {
  193. b = append(b, '0')
  194. }
  195. }
  196. return string(b)
  197. }
  198. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'luy'
  199. // in accounting notation.
  200. func (luy *luy) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  201. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  202. symbol := luy.currencies[currency]
  203. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  204. count := 0
  205. inWhole := v == 0
  206. b := make([]byte, 0, l)
  207. for i := len(s) - 1; i >= 0; i-- {
  208. if s[i] == '.' {
  209. b = append(b, luy.decimal[0])
  210. inWhole = true
  211. continue
  212. }
  213. if inWhole {
  214. if count == 3 {
  215. b = append(b, luy.group[0])
  216. count = 1
  217. } else {
  218. count++
  219. }
  220. }
  221. b = append(b, s[i])
  222. }
  223. if num < 0 {
  224. for j := len(symbol) - 1; j >= 0; j-- {
  225. b = append(b, symbol[j])
  226. }
  227. b = append(b, luy.minus[0])
  228. } else {
  229. for j := len(symbol) - 1; j >= 0; j-- {
  230. b = append(b, symbol[j])
  231. }
  232. }
  233. // reverse
  234. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  235. b[i], b[j] = b[j], b[i]
  236. }
  237. if int(v) < 2 {
  238. if v == 0 {
  239. b = append(b, luy.decimal...)
  240. }
  241. for i := 0; i < 2-int(v); i++ {
  242. b = append(b, '0')
  243. }
  244. }
  245. return string(b)
  246. }
  247. // FmtDateShort returns the short date representation of 't' for 'luy'
  248. func (luy *luy) 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 'luy'
  264. func (luy *luy) 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, luy.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 'luy'
  274. func (luy *luy) 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, luy.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 'luy'
  284. func (luy *luy) FmtDateFull(t time.Time) string {
  285. b := make([]byte, 0, 32)
  286. b = append(b, luy.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, luy.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 'luy'
  296. func (luy *luy) 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, luy.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 'luy'
  310. func (luy *luy) 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, luy.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, luy.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 'luy'
  329. func (luy *luy) 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, luy.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, luy.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 'luy'
  351. func (luy *luy) 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, luy.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, luy.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 := luy.timezones[tz]; ok {
  370. b = append(b, btz...)
  371. } else {
  372. b = append(b, tz...)
  373. }
  374. return string(b)
  375. }