vo.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package vo
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type vo 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 'vo' locale
  39. func New() locales.Translator {
  40. return &vo{
  41. locale: "vo",
  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", "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{"", "yan", "feb", "mäz", "prl", "may", "yun", "yul", "gst", "set", "ton", "nov", "dek"},
  48. monthsNarrow: []string{"", "Y", "F", "M", "P", "M", "Y", "Y", "G", "S", "T", "N", "D"},
  49. monthsWide: []string{"", "yanul", "febul", "mäzul", "prilul", "mayul", "yunul", "yulul", "gustul", "setul", "tobul", "novul", "dekul"},
  50. daysAbbreviated: []string{"su.", "mu.", "tu.", "ve.", "dö.", "fr.", "zä."},
  51. daysNarrow: []string{"S", "M", "T", "V", "D", "F", "Z"},
  52. daysWide: []string{"sudel", "mudel", "tudel", "vedel", "dödel", "fridel", "zädel"},
  53. erasAbbreviated: []string{"b. t. kr.", "p. t. kr."},
  54. erasNarrow: []string{"", ""},
  55. erasWide: []string{"b. t. kr.", "p. t. kr."},
  56. timezones: map[string]string{"ADT": "ADT", "WAST": "WAST", "WESZ": "WESZ", "GFT": "GFT", "CAT": "CAT", "CLT": "CLT", "TMT": "TMT", "UYST": "UYST", "ChST": "ChST", "NZDT": "NZDT", "HEPM": "HEPM", "ARST": "ARST", "CHADT": "CHADT", "HEEG": "HEEG", "MST": "MST", "HAT": "HAT", "VET": "VET", "TMST": "TMST", "GMT": "GMT", "AWST": "AWST", "HEPMX": "HEPMX", "AST": "AST", "LHDT": "LHDT", "HNPM": "HNPM", "HAST": "HAST", "AEST": "AEST", "AEDT": "AEDT", "WAT": "WAT", "HEOG": "HEOG", "MESZ": "MESZ", "∅∅∅": "∅∅∅", "EAT": "EAT", "CHAST": "CHAST", "HECU": "HECU", "PDT": "PDT", "SAST": "SAST", "JDT": "JDT", "AKST": "AKST", "MDT": "MDT", "HNOG": "HNOG", "HNT": "HNT", "HADT": "HADT", "CST": "CST", "EDT": "EDT", "ACST": "ACST", "WARST": "WARST", "HENOMX": "HENOMX", "HNCU": "HNCU", "AWDT": "AWDT", "ACWDT": "ACWDT", "HNNOMX": "HNNOMX", "OESZ": "OESZ", "WART": "WART", "UYT": "UYT", "HNPMX": "HNPMX", "JST": "JST", "COST": "COST", "WIT": "WIT", "GYT": "GYT", "ACWST": "ACWST", "HNEG": "HNEG", "HKT": "HKT", "SRT": "SRT", "NZST": "NZST", "HKST": "HKST", "CLST": "CLST", "WEZ": "WEZ", "SGT": "SGT", "ECT": "ECT", "ACDT": "ACDT", "IST": "IST", "OEZ": "OEZ", "ART": "ART", "CDT": "CDT", "PST": "PST", "WIB": "WIB", "BT": "BT", "BOT": "BOT", "LHST": "LHST", "AKDT": "AKDT", "MYT": "MYT", "MEZ": "MEZ", "COT": "COT", "EST": "EST", "WITA": "WITA"},
  57. }
  58. }
  59. // Locale returns the current translators string locale
  60. func (vo *vo) Locale() string {
  61. return vo.locale
  62. }
  63. // PluralsCardinal returns the list of cardinal plural rules associated with 'vo'
  64. func (vo *vo) PluralsCardinal() []locales.PluralRule {
  65. return vo.pluralsCardinal
  66. }
  67. // PluralsOrdinal returns the list of ordinal plural rules associated with 'vo'
  68. func (vo *vo) PluralsOrdinal() []locales.PluralRule {
  69. return vo.pluralsOrdinal
  70. }
  71. // PluralsRange returns the list of range plural rules associated with 'vo'
  72. func (vo *vo) PluralsRange() []locales.PluralRule {
  73. return vo.pluralsRange
  74. }
  75. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'vo'
  76. func (vo *vo) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  77. n := math.Abs(num)
  78. if n == 1 {
  79. return locales.PluralRuleOne
  80. }
  81. return locales.PluralRuleOther
  82. }
  83. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'vo'
  84. func (vo *vo) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. return locales.PluralRuleUnknown
  86. }
  87. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'vo'
  88. func (vo *vo) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  89. return locales.PluralRuleUnknown
  90. }
  91. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  92. func (vo *vo) MonthAbbreviated(month time.Month) string {
  93. return vo.monthsAbbreviated[month]
  94. }
  95. // MonthsAbbreviated returns the locales abbreviated months
  96. func (vo *vo) MonthsAbbreviated() []string {
  97. return vo.monthsAbbreviated[1:]
  98. }
  99. // MonthNarrow returns the locales narrow month given the 'month' provided
  100. func (vo *vo) MonthNarrow(month time.Month) string {
  101. return vo.monthsNarrow[month]
  102. }
  103. // MonthsNarrow returns the locales narrow months
  104. func (vo *vo) MonthsNarrow() []string {
  105. return vo.monthsNarrow[1:]
  106. }
  107. // MonthWide returns the locales wide month given the 'month' provided
  108. func (vo *vo) MonthWide(month time.Month) string {
  109. return vo.monthsWide[month]
  110. }
  111. // MonthsWide returns the locales wide months
  112. func (vo *vo) MonthsWide() []string {
  113. return vo.monthsWide[1:]
  114. }
  115. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  116. func (vo *vo) WeekdayAbbreviated(weekday time.Weekday) string {
  117. return vo.daysAbbreviated[weekday]
  118. }
  119. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  120. func (vo *vo) WeekdaysAbbreviated() []string {
  121. return vo.daysAbbreviated
  122. }
  123. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  124. func (vo *vo) WeekdayNarrow(weekday time.Weekday) string {
  125. return vo.daysNarrow[weekday]
  126. }
  127. // WeekdaysNarrow returns the locales narrow weekdays
  128. func (vo *vo) WeekdaysNarrow() []string {
  129. return vo.daysNarrow
  130. }
  131. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  132. func (vo *vo) WeekdayShort(weekday time.Weekday) string {
  133. return vo.daysShort[weekday]
  134. }
  135. // WeekdaysShort returns the locales short weekdays
  136. func (vo *vo) WeekdaysShort() []string {
  137. return vo.daysShort
  138. }
  139. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  140. func (vo *vo) WeekdayWide(weekday time.Weekday) string {
  141. return vo.daysWide[weekday]
  142. }
  143. // WeekdaysWide returns the locales wide weekdays
  144. func (vo *vo) WeekdaysWide() []string {
  145. return vo.daysWide
  146. }
  147. // Decimal returns the decimal point of number
  148. func (vo *vo) Decimal() string {
  149. return vo.decimal
  150. }
  151. // Group returns the group of number
  152. func (vo *vo) Group() string {
  153. return vo.group
  154. }
  155. // Group returns the minus sign of number
  156. func (vo *vo) Minus() string {
  157. return vo.minus
  158. }
  159. // FmtNumber returns 'num' with digits/precision of 'v' for 'vo' and handles both Whole and Real numbers based on 'v'
  160. func (vo *vo) FmtNumber(num float64, v uint64) string {
  161. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  162. }
  163. // FmtPercent returns 'num' with digits/precision of 'v' for 'vo' and handles both Whole and Real numbers based on 'v'
  164. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  165. func (vo *vo) FmtPercent(num float64, v uint64) string {
  166. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  167. }
  168. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'vo'
  169. func (vo *vo) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  170. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  171. symbol := vo.currencies[currency]
  172. return string(append(append([]byte{}, symbol...), s...))
  173. }
  174. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'vo'
  175. // in accounting notation.
  176. func (vo *vo) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  177. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  178. symbol := vo.currencies[currency]
  179. return string(append(append([]byte{}, symbol...), s...))
  180. }
  181. // FmtDateShort returns the short date representation of 't' for 'vo'
  182. func (vo *vo) FmtDateShort(t time.Time) string {
  183. b := make([]byte, 0, 32)
  184. if t.Year() > 0 {
  185. b = strconv.AppendInt(b, int64(t.Year()), 10)
  186. } else {
  187. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  188. }
  189. b = append(b, []byte{0x2d}...)
  190. if t.Month() < 10 {
  191. b = append(b, '0')
  192. }
  193. b = strconv.AppendInt(b, int64(t.Month()), 10)
  194. b = append(b, []byte{0x2d}...)
  195. if t.Day() < 10 {
  196. b = append(b, '0')
  197. }
  198. b = strconv.AppendInt(b, int64(t.Day()), 10)
  199. return string(b)
  200. }
  201. // FmtDateMedium returns the medium date representation of 't' for 'vo'
  202. func (vo *vo) FmtDateMedium(t time.Time) string {
  203. b := make([]byte, 0, 32)
  204. if t.Year() > 0 {
  205. b = strconv.AppendInt(b, int64(t.Year()), 10)
  206. } else {
  207. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  208. }
  209. b = append(b, []byte{0x20}...)
  210. b = append(b, vo.monthsAbbreviated[t.Month()]...)
  211. b = append(b, []byte{0x2e, 0x20}...)
  212. b = strconv.AppendInt(b, int64(t.Day()), 10)
  213. return string(b)
  214. }
  215. // FmtDateLong returns the long date representation of 't' for 'vo'
  216. func (vo *vo) FmtDateLong(t time.Time) string {
  217. b := make([]byte, 0, 32)
  218. if t.Year() > 0 {
  219. b = strconv.AppendInt(b, int64(t.Year()), 10)
  220. } else {
  221. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  222. }
  223. b = append(b, []byte{0x20}...)
  224. b = append(b, vo.monthsWide[t.Month()]...)
  225. b = append(b, []byte{0x20}...)
  226. b = strconv.AppendInt(b, int64(t.Day()), 10)
  227. return string(b)
  228. }
  229. // FmtDateFull returns the full date representation of 't' for 'vo'
  230. func (vo *vo) FmtDateFull(t time.Time) string {
  231. b := make([]byte, 0, 32)
  232. if t.Year() > 0 {
  233. b = strconv.AppendInt(b, int64(t.Year()), 10)
  234. } else {
  235. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  236. }
  237. b = append(b, []byte{0x20}...)
  238. b = append(b, vo.monthsWide[t.Month()]...)
  239. b = append(b, []byte{0x61}...)
  240. b = append(b, []byte{0x20, 0x64}...)
  241. b = append(b, []byte{0x2e, 0x20}...)
  242. b = strconv.AppendInt(b, int64(t.Day()), 10)
  243. b = append(b, []byte{0x69, 0x64}...)
  244. return string(b)
  245. }
  246. // FmtTimeShort returns the short time representation of 't' for 'vo'
  247. func (vo *vo) FmtTimeShort(t time.Time) string {
  248. b := make([]byte, 0, 32)
  249. if t.Hour() < 10 {
  250. b = append(b, '0')
  251. }
  252. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  253. b = append(b, vo.timeSeparator...)
  254. if t.Minute() < 10 {
  255. b = append(b, '0')
  256. }
  257. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  258. return string(b)
  259. }
  260. // FmtTimeMedium returns the medium time representation of 't' for 'vo'
  261. func (vo *vo) FmtTimeMedium(t time.Time) string {
  262. b := make([]byte, 0, 32)
  263. if t.Hour() < 10 {
  264. b = append(b, '0')
  265. }
  266. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  267. b = append(b, vo.timeSeparator...)
  268. if t.Minute() < 10 {
  269. b = append(b, '0')
  270. }
  271. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  272. b = append(b, vo.timeSeparator...)
  273. if t.Second() < 10 {
  274. b = append(b, '0')
  275. }
  276. b = strconv.AppendInt(b, int64(t.Second()), 10)
  277. return string(b)
  278. }
  279. // FmtTimeLong returns the long time representation of 't' for 'vo'
  280. func (vo *vo) FmtTimeLong(t time.Time) string {
  281. b := make([]byte, 0, 32)
  282. if t.Hour() < 10 {
  283. b = append(b, '0')
  284. }
  285. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  286. b = append(b, vo.timeSeparator...)
  287. if t.Minute() < 10 {
  288. b = append(b, '0')
  289. }
  290. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  291. b = append(b, vo.timeSeparator...)
  292. if t.Second() < 10 {
  293. b = append(b, '0')
  294. }
  295. b = strconv.AppendInt(b, int64(t.Second()), 10)
  296. b = append(b, []byte{0x20}...)
  297. tz, _ := t.Zone()
  298. b = append(b, tz...)
  299. return string(b)
  300. }
  301. // FmtTimeFull returns the full time representation of 't' for 'vo'
  302. func (vo *vo) FmtTimeFull(t time.Time) string {
  303. b := make([]byte, 0, 32)
  304. if t.Hour() < 10 {
  305. b = append(b, '0')
  306. }
  307. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  308. b = append(b, vo.timeSeparator...)
  309. if t.Minute() < 10 {
  310. b = append(b, '0')
  311. }
  312. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  313. b = append(b, vo.timeSeparator...)
  314. if t.Second() < 10 {
  315. b = append(b, '0')
  316. }
  317. b = strconv.AppendInt(b, int64(t.Second()), 10)
  318. b = append(b, []byte{0x20}...)
  319. tz, _ := t.Zone()
  320. if btz, ok := vo.timezones[tz]; ok {
  321. b = append(b, btz...)
  322. } else {
  323. b = append(b, tz...)
  324. }
  325. return string(b)
  326. }