luy.go 16 KB

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