vun.go 16 KB

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