ce.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package ce
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ce 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. percentSuffix string
  19. perMille string
  20. timeSeparator string
  21. inifinity string
  22. currencies []string // idx = enum of currency code
  23. currencyPositiveSuffix string
  24. currencyNegativeSuffix string
  25. monthsAbbreviated []string
  26. monthsNarrow []string
  27. monthsWide []string
  28. daysAbbreviated []string
  29. daysNarrow []string
  30. daysShort []string
  31. daysWide []string
  32. periodsAbbreviated []string
  33. periodsNarrow []string
  34. periodsShort []string
  35. periodsWide []string
  36. erasAbbreviated []string
  37. erasNarrow []string
  38. erasWide []string
  39. timezones map[string]string
  40. }
  41. // New returns a new instance of translator for the 'ce' locale
  42. func New() locales.Translator {
  43. return &ce{
  44. locale: "ce",
  45. pluralsCardinal: []locales.PluralRule{2, 6},
  46. pluralsOrdinal: []locales.PluralRule{6},
  47. pluralsRange: nil,
  48. decimal: ".",
  49. group: ",",
  50. minus: "-",
  51. percent: "%",
  52. perMille: "‰",
  53. timeSeparator: ":",
  54. currencies: []string{"ADP ", "AED", "AFA ", "AFN", "ALK ", "ALL", "AMD", "ANG", "AOA", "AOK ", "AON ", "AOR ", "ARA ", "ARL ", "ARM ", "ARP ", "ARS", "ATS ", "A$", "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 ", "R$", "BRN ", "BRR ", "BRZ ", "BSD", "BTN", "BUK ", "BWP", "BYB ", "BYR", "BZD", "CA$", "CDF", "CHE ", "CHF", "CHW ", "CLE ", "CLF ", "CLP", "CNX ", "CN¥", "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", "€", "FIM ", "FJD", "FKP", "FRF ", "£", "GEK ", "GEL", "GHC ", "GHS", "GIP", "GMD", "GNF", "GNS ", "GQE ", "GRD ", "GTQ", "GWE ", "GWP ", "GYD", "HK$", "HNL", "HRD ", "HRK", "HTG", "HUF", "IDR", "IEP ", "ILP ", "ILR ", "₪", "₹", "IQD", "IRR", "ISJ ", "ISK", "ITL ", "JMD", "JOD", "JP¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH ", "KRO ", "₩", "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", "MX$", "MXP ", "MXV ", "MYR", "MZE ", "MZM ", "MZN", "NAD", "NGN", "NIC ", "NIO", "NLG ", "NOK", "NPR", "NZ$", "OMR", "PAB", "PEI ", "PEN", "PES ", "PGK", "PHP", "PKR", "PLN", "PLZ ", "PTE ", "PYG", "QAR", "RHD ", "ROL ", "RON", "RSD", "₽", "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", "NT$", "TZS", "UAH", "UAK ", "UGS ", "UGX", "US$", "USN ", "USS ", "UYI ", "UYP ", "UYU", "UZS", "VEB ", "VEF", "₫", "VNN ", "VUV", "WST", "FCFA", "XAG ", "XAU ", "XBA ", "XBB ", "XBC ", "XBD ", "EC$", "XDR ", "XEU ", "XFO ", "XFU ", "CFA", "XPD ", "CFPF", "XPT ", "XRE ", "XSU ", "XTS ", "XUA ", "XXX ", "YDD ", "YER", "YUD ", "YUM ", "YUN ", "YUR ", "ZAL ", "ZAR", "ZMK ", "ZMW", "ZRN ", "ZRZ ", "ZWD ", "ZWL ", "ZWR "},
  55. percentSuffix: " ",
  56. currencyPositiveSuffix: " ",
  57. currencyNegativeSuffix: " ",
  58. monthsAbbreviated: []string{"", "янв", "фев", "мар", "апр", "май", "июн", "июл", "авг", "сен", "окт", "ноя", "дек"},
  59. monthsWide: []string{"", "январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"},
  60. daysWide: []string{"кӀиранан де", "оршотан де", "шинарин де", "кхаарин де", "еарин де", "пӀераскан де", "шот де"},
  61. timezones: map[string]string{"AKDT": "Аляска, аьхкенан хан", "EST": "Малхбален Америка, стандартан хан", "HAST": "Гавайн-алеутийн стандартан хан", "HADT": "Гавайн-алеутийн аьхкенан хан", "BT": "Бутан", "HKT": "Гонконг, стандартан хан", "OEZ": "Малхбален Европа, стандартан хан", "GFT": "Французийн Гвиана", "MEZ": "Юккъера Европа, стандартан хан", "MESZ": "Юккъера Европа, аьхкенан хан", "AEDT": "Малхбален Австрали, аьхкенан хан", "EDT": "Малхбален Америка, аьхкенан хан", "ACST": "Юккъера Австрали, стандартан хан", "UYST": "Уругвай, аьхкенан хан", "COT": "Колумби, стандартан хан", "∅∅∅": "Бразили, аьхкенан хан", "MST": "MST", "LHST": "Лорд-Хау, стандартан хан", "LHDT": "Лорд-Хау, аьхкенан хан", "UYT": "Уругвай, стандартан хан", "CST": "Юккъера Америка, стандартан хан", "WITA": "Юккъера Индонези", "ACWDT": "Юккъера Австрали, малхбузен аьхкенан хан", "PDT": "Тийна океанан аьхкенан хан", "GYT": "Гайана", "CDT": "Юккъера Америка, аьхкенан хан", "CAT": "Юккъера Африка", "PST": "Тийна океанан стандартан хан", "AKST": "Аляска, стандартан хан", "WART": "Малхбузен Аргентина, стандартан хан", "SRT": "Суринам", "CHAST": "Чатем, стандартан хан", "CHADT": "Чатем, аьхкенан хан", "IST": "Инди", "MDT": "MDT", "ARST": "Аргентина, аьхкенан хан", "WARST": "Малхбузен Аргентина, аьхкенан хан", "WIT": "Малхбален Индонези", "NZST": "Керла Зеланди, стандартан хан", "BOT": "Боливи", "SAST": "Къилба Африка", "HAT": "Ньюфаундленд, аьхкенан хан", "ACDT": "Юккъера Австрали, аьхкенан хан", "MYT": "Малайзи", "NZDT": "Керла Зеланди, аьхкенан хан", "ACWST": "Юккъера Австрали, малхбузен стандартан хан", "WESZ": "Малхбузен Европа, аьхкенан хан", "COST": "Колумби, аьхкенан хан", "ECT": "Эквадор", "ADT": "Атлантикан аьхкенан хан", "WAT": "Малхбузен Африка, стандартан хан", "WAST": "Малхбузен Африка, аьхкенан хан", "OESZ": "Малхбален Европа, аьхкенан хан", "AEST": "Малхбален Австрали, стандартан хан", "GMT": "Гринвичица юкъара хан", "AWST": "Малхбузен Австрали, стандартан хан", "JDT": "Япони, аьхкенан хан", "HKST": "Гонконг, аьхкенан хан", "WIB": "Малхбузен Индонези", "ART": "Аргентина, стандартан хан", "ChST": "Чаморро", "TMT": "Туркменин стандартан хан", "WEZ": "Малхбузен Европа, стандартан хан", "AST": "Атлантикан стандартан хан", "HNT": "Ньюфаундленд, стандартан хан", "TMST": "Туркменин аьхкенан хан", "EAT": "Малхбален Африка", "SGT": "Сингапур", "AWDT": "Малхбузен Австрали, аьхкенан хан", "JST": "Япони, стандартан хан", "CLT": "Чили, стандартан хан", "CLST": "Чили, аьхкенан хан", "VET": "Венесуэла"},
  62. }
  63. }
  64. // Locale returns the current translators string locale
  65. func (ce *ce) Locale() string {
  66. return ce.locale
  67. }
  68. // PluralsCardinal returns the list of cardinal plural rules associated with 'ce'
  69. func (ce *ce) PluralsCardinal() []locales.PluralRule {
  70. return ce.pluralsCardinal
  71. }
  72. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ce'
  73. func (ce *ce) PluralsOrdinal() []locales.PluralRule {
  74. return ce.pluralsOrdinal
  75. }
  76. // PluralsRange returns the list of range plural rules associated with 'ce'
  77. func (ce *ce) PluralsRange() []locales.PluralRule {
  78. return ce.pluralsRange
  79. }
  80. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ce'
  81. func (ce *ce) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  82. n := math.Abs(num)
  83. if n == 1 {
  84. return locales.PluralRuleOne
  85. }
  86. return locales.PluralRuleOther
  87. }
  88. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ce'
  89. func (ce *ce) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  90. return locales.PluralRuleOther
  91. }
  92. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ce'
  93. func (ce *ce) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  94. return locales.PluralRuleUnknown
  95. }
  96. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  97. func (ce *ce) MonthAbbreviated(month time.Month) string {
  98. return ce.monthsAbbreviated[month]
  99. }
  100. // MonthsAbbreviated returns the locales abbreviated months
  101. func (ce *ce) MonthsAbbreviated() []string {
  102. return ce.monthsAbbreviated[1:]
  103. }
  104. // MonthNarrow returns the locales narrow month given the 'month' provided
  105. func (ce *ce) MonthNarrow(month time.Month) string {
  106. return ce.monthsNarrow[month]
  107. }
  108. // MonthsNarrow returns the locales narrow months
  109. func (ce *ce) MonthsNarrow() []string {
  110. return ce.monthsNarrow[1:]
  111. }
  112. // MonthWide returns the locales wide month given the 'month' provided
  113. func (ce *ce) MonthWide(month time.Month) string {
  114. return ce.monthsWide[month]
  115. }
  116. // MonthsWide returns the locales wide months
  117. func (ce *ce) MonthsWide() []string {
  118. return ce.monthsWide[1:]
  119. }
  120. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  121. func (ce *ce) WeekdayAbbreviated(weekday time.Weekday) string {
  122. return ce.daysAbbreviated[weekday]
  123. }
  124. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  125. func (ce *ce) WeekdaysAbbreviated() []string {
  126. return ce.daysAbbreviated
  127. }
  128. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  129. func (ce *ce) WeekdayNarrow(weekday time.Weekday) string {
  130. return ce.daysNarrow[weekday]
  131. }
  132. // WeekdaysNarrow returns the locales narrow weekdays
  133. func (ce *ce) WeekdaysNarrow() []string {
  134. return ce.daysNarrow
  135. }
  136. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  137. func (ce *ce) WeekdayShort(weekday time.Weekday) string {
  138. return ce.daysShort[weekday]
  139. }
  140. // WeekdaysShort returns the locales short weekdays
  141. func (ce *ce) WeekdaysShort() []string {
  142. return ce.daysShort
  143. }
  144. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  145. func (ce *ce) WeekdayWide(weekday time.Weekday) string {
  146. return ce.daysWide[weekday]
  147. }
  148. // WeekdaysWide returns the locales wide weekdays
  149. func (ce *ce) WeekdaysWide() []string {
  150. return ce.daysWide
  151. }
  152. // FmtNumber returns 'num' with digits/precision of 'v' for 'ce' and handles both Whole and Real numbers based on 'v'
  153. func (ce *ce) FmtNumber(num float64, v uint64) string {
  154. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  155. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  156. count := 0
  157. inWhole := v == 0
  158. b := make([]byte, 0, l)
  159. for i := len(s) - 1; i >= 0; i-- {
  160. if s[i] == '.' {
  161. b = append(b, ce.decimal[0])
  162. inWhole = true
  163. continue
  164. }
  165. if inWhole {
  166. if count == 3 {
  167. b = append(b, ce.group[0])
  168. count = 1
  169. } else {
  170. count++
  171. }
  172. }
  173. b = append(b, s[i])
  174. }
  175. if num < 0 {
  176. b = append(b, ce.minus[0])
  177. }
  178. // reverse
  179. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  180. b[i], b[j] = b[j], b[i]
  181. }
  182. return string(b)
  183. }
  184. // FmtPercent returns 'num' with digits/precision of 'v' for 'ce' and handles both Whole and Real numbers based on 'v'
  185. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  186. func (ce *ce) FmtPercent(num float64, v uint64) string {
  187. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  188. l := len(s) + 5
  189. b := make([]byte, 0, l)
  190. for i := len(s) - 1; i >= 0; i-- {
  191. if s[i] == '.' {
  192. b = append(b, ce.decimal[0])
  193. continue
  194. }
  195. b = append(b, s[i])
  196. }
  197. if num < 0 {
  198. b = append(b, ce.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. b = append(b, ce.percentSuffix...)
  205. b = append(b, ce.percent...)
  206. return string(b)
  207. }
  208. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ce'
  209. func (ce *ce) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  210. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  211. symbol := ce.currencies[currency]
  212. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  213. count := 0
  214. inWhole := v == 0
  215. b := make([]byte, 0, l)
  216. for i := len(s) - 1; i >= 0; i-- {
  217. if s[i] == '.' {
  218. b = append(b, ce.decimal[0])
  219. inWhole = true
  220. continue
  221. }
  222. if inWhole {
  223. if count == 3 {
  224. b = append(b, ce.group[0])
  225. count = 1
  226. } else {
  227. count++
  228. }
  229. }
  230. b = append(b, s[i])
  231. }
  232. if num < 0 {
  233. b = append(b, ce.minus[0])
  234. }
  235. // reverse
  236. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  237. b[i], b[j] = b[j], b[i]
  238. }
  239. if int(v) < 2 {
  240. if v == 0 {
  241. b = append(b, ce.decimal...)
  242. }
  243. for i := 0; i < 2-int(v); i++ {
  244. b = append(b, '0')
  245. }
  246. }
  247. b = append(b, ce.currencyPositiveSuffix...)
  248. b = append(b, symbol...)
  249. return string(b)
  250. }
  251. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ce'
  252. // in accounting notation.
  253. func (ce *ce) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  254. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  255. symbol := ce.currencies[currency]
  256. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  257. count := 0
  258. inWhole := v == 0
  259. b := make([]byte, 0, l)
  260. for i := len(s) - 1; i >= 0; i-- {
  261. if s[i] == '.' {
  262. b = append(b, ce.decimal[0])
  263. inWhole = true
  264. continue
  265. }
  266. if inWhole {
  267. if count == 3 {
  268. b = append(b, ce.group[0])
  269. count = 1
  270. } else {
  271. count++
  272. }
  273. }
  274. b = append(b, s[i])
  275. }
  276. if num < 0 {
  277. b = append(b, ce.minus[0])
  278. }
  279. // reverse
  280. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  281. b[i], b[j] = b[j], b[i]
  282. }
  283. if int(v) < 2 {
  284. if v == 0 {
  285. b = append(b, ce.decimal...)
  286. }
  287. for i := 0; i < 2-int(v); i++ {
  288. b = append(b, '0')
  289. }
  290. }
  291. if num < 0 {
  292. b = append(b, ce.currencyNegativeSuffix...)
  293. b = append(b, symbol...)
  294. } else {
  295. b = append(b, ce.currencyPositiveSuffix...)
  296. b = append(b, symbol...)
  297. }
  298. return string(b)
  299. }
  300. // FmtDateShort returns the short date representation of 't' for 'ce'
  301. func (ce *ce) FmtDateShort(t time.Time) string {
  302. b := make([]byte, 0, 32)
  303. return string(b)
  304. }
  305. // FmtDateMedium returns the medium date representation of 't' for 'ce'
  306. func (ce *ce) FmtDateMedium(t time.Time) string {
  307. b := make([]byte, 0, 32)
  308. return string(b)
  309. }
  310. // FmtDateLong returns the long date representation of 't' for 'ce'
  311. func (ce *ce) FmtDateLong(t time.Time) string {
  312. b := make([]byte, 0, 32)
  313. return string(b)
  314. }
  315. // FmtDateFull returns the full date representation of 't' for 'ce'
  316. func (ce *ce) FmtDateFull(t time.Time) string {
  317. b := make([]byte, 0, 32)
  318. return string(b)
  319. }
  320. // FmtTimeShort returns the short time representation of 't' for 'ce'
  321. func (ce *ce) FmtTimeShort(t time.Time) string {
  322. b := make([]byte, 0, 32)
  323. return string(b)
  324. }
  325. // FmtTimeMedium returns the medium time representation of 't' for 'ce'
  326. func (ce *ce) FmtTimeMedium(t time.Time) string {
  327. b := make([]byte, 0, 32)
  328. return string(b)
  329. }
  330. // FmtTimeLong returns the long time representation of 't' for 'ce'
  331. func (ce *ce) FmtTimeLong(t time.Time) string {
  332. b := make([]byte, 0, 32)
  333. return string(b)
  334. }
  335. // FmtTimeFull returns the full time representation of 't' for 'ce'
  336. func (ce *ce) FmtTimeFull(t time.Time) string {
  337. b := make([]byte, 0, 32)
  338. return string(b)
  339. }