root.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package root
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type root 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 'root' locale
  39. func New() locales.Translator {
  40. return &root{
  41. locale: "root",
  42. pluralsCardinal: []locales.PluralRule{6},
  43. pluralsOrdinal: []locales.PluralRule{6},
  44. pluralsRange: nil,
  45. decimal: ".",
  46. group: ",",
  47. minus: "-",
  48. percent: "%",
  49. perMille: "‰",
  50. timeSeparator: ":",
  51. inifinity: "∞",
  52. currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "Kz", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "$", "ATS", "A$", "AWG", "AZM", "AZN", "BAD", "KM", "BAN", "$", "৳", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "$", "$", "Bs", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "R$", "BRN", "BRR", "BRZ", "$", "BTN", "BUK", "P", "BYB", "р.", "BYR", "$", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "$", "CNH", "CNX", "CN¥", "$", "COU", "₡", "CSD", "CSK", "$", "$", "CVE", "CYP", "Kč", "DDM", "DEM", "DJF", "kr", "$", "DZD", "ECS", "ECV", "EEK", "E£", "ERN", "ESA", "ESB", "₧", "ETB", "€", "FIM", "$", "£", "FRF", "£", "GEK", "₾", "GHC", "GHS", "£", "GMD", "FG", "GNS", "GQE", "GRD", "Q", "GWE", "GWP", "$", "HK$", "L", "HRD", "kn", "HTG", "Ft", "Rp", "IEP", "ILP", "ILR", "₪", "₹", "IQD", "IRR", "ISJ", "kr", "ITL", "$", "JOD", "JP¥", "KES", "KGS", "៛", "CF", "₩", "KRH", "KRO", "₩", "KWD", "$", "₸", "₭", "L£", "Rs", "$", "LSL", "Lt", "LTT", "LUC", "LUF", "LUL", "Ls", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "Ar", "MGF", "MKD", "MKN", "MLF", "K", "₮", "MOP", "MRO", "MTL", "MTP", "Rs", "MVP", "MVR", "MWK", "MX$", "MXP", "MXV", "RM", "MZE", "MZM", "MZN", "$", "₦", "NIC", "C$", "NLG", "kr", "Rs", "NZ$", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "₱", "Rs", "zł", "PLZ", "PTE", "₲", "QAR", "RHD", "ROL", "lei", "RSD", "₽", "р.", "RF", "SAR", "$", "SCR", "SDD", "SDG", "SDP", "kr", "$", "£", "SIT", "SKK", "SLL", "SOS", "$", "SRG", "£", "Db", "STN", "SUR", "SVC", "£", "SZL", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "T$", "TPE", "TRL", "₺", "$", "NT$", "TZS", "₴", "UAK", "UGS", "UGX", "US$", "USN", "USS", "UYI", "UYP", "$", "UZS", "VEB", "Bs", "₫", "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", "R", "ZMK", "ZK", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
  53. monthsNarrow: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
  54. monthsWide: []string{"", "M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09", "M10", "M11", "M12"},
  55. daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
  56. daysWide: []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
  57. periodsAbbreviated: []string{"AM", "PM"},
  58. periodsNarrow: []string{"", ""},
  59. periodsWide: []string{"", ""},
  60. erasAbbreviated: []string{"BCE", "CE"},
  61. erasNarrow: []string{"", ""},
  62. erasWide: []string{"", ""},
  63. timezones: map[string]string{"CLT": "CLT", "ChST": "ChST", "AWST": "AWST", "HEPMX": "HEPMX", "CST": "CST", "PDT": "PDT", "WAST": "WAST", "WITA": "WITA", "ACST": "ACST", "ART": "ART", "HADT": "HADT", "CHADT": "CHADT", "AST": "AST", "WESZ": "WESZ", "JDT": "JDT", "EDT": "EDT", "∅∅∅": "∅∅∅", "HNEG": "HNEG", "HENOMX": "HENOMX", "WIB": "WIB", "BT": "BT", "AKST": "AKST", "HAT": "HAT", "PST": "PST", "IST": "IST", "HNPMX": "HNPMX", "AEST": "AEST", "SAST": "SAST", "NZDT": "NZDT", "ACWDT": "ACWDT", "GYT": "GYT", "WIT": "WIT", "TMST": "TMST", "UYST": "UYST", "CHAST": "CHAST", "HNCU": "HNCU", "AEDT": "AEDT", "ACWST": "ACWST", "WARST": "WARST", "MEZ": "MEZ", "EST": "EST", "ECT": "ECT", "HNOG": "HNOG", "MESZ": "MESZ", "WEZ": "WEZ", "HAST": "HAST", "HEOG": "HEOG", "COT": "COT", "HNNOMX": "HNNOMX", "SRT": "SRT", "OESZ": "OESZ", "ADT": "ADT", "AKDT": "AKDT", "HEEG": "HEEG", "HNPM": "HNPM", "BOT": "BOT", "HECU": "HECU", "HNT": "HNT", "MDT": "MDT", "EAT": "EAT", "OEZ": "OEZ", "HEPM": "HEPM", "AWDT": "AWDT", "COST": "COST", "LHST": "LHST", "LHDT": "LHDT", "MST": "MST", "UYT": "UYT", "GMT": "GMT", "GFT": "GFT", "JST": "JST", "WART": "WART", "ACDT": "ACDT", "NZST": "NZST", "ARST": "ARST", "CDT": "CDT", "MYT": "MYT", "SGT": "SGT", "TMT": "TMT", "CLST": "CLST", "CAT": "CAT", "WAT": "WAT", "HKT": "HKT", "HKST": "HKST", "VET": "VET"},
  64. }
  65. }
  66. // Locale returns the current translators string locale
  67. func (root *root) Locale() string {
  68. return root.locale
  69. }
  70. // PluralsCardinal returns the list of cardinal plural rules associated with 'root'
  71. func (root *root) PluralsCardinal() []locales.PluralRule {
  72. return root.pluralsCardinal
  73. }
  74. // PluralsOrdinal returns the list of ordinal plural rules associated with 'root'
  75. func (root *root) PluralsOrdinal() []locales.PluralRule {
  76. return root.pluralsOrdinal
  77. }
  78. // PluralsRange returns the list of range plural rules associated with 'root'
  79. func (root *root) PluralsRange() []locales.PluralRule {
  80. return root.pluralsRange
  81. }
  82. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'root'
  83. func (root *root) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  84. return locales.PluralRuleOther
  85. }
  86. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'root'
  87. func (root *root) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  88. return locales.PluralRuleOther
  89. }
  90. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'root'
  91. func (root *root) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  92. return locales.PluralRuleUnknown
  93. }
  94. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  95. func (root *root) MonthAbbreviated(month time.Month) string {
  96. return root.monthsAbbreviated[month]
  97. }
  98. // MonthsAbbreviated returns the locales abbreviated months
  99. func (root *root) MonthsAbbreviated() []string {
  100. return nil
  101. }
  102. // MonthNarrow returns the locales narrow month given the 'month' provided
  103. func (root *root) MonthNarrow(month time.Month) string {
  104. return root.monthsNarrow[month]
  105. }
  106. // MonthsNarrow returns the locales narrow months
  107. func (root *root) MonthsNarrow() []string {
  108. return root.monthsNarrow[1:]
  109. }
  110. // MonthWide returns the locales wide month given the 'month' provided
  111. func (root *root) MonthWide(month time.Month) string {
  112. return root.monthsWide[month]
  113. }
  114. // MonthsWide returns the locales wide months
  115. func (root *root) MonthsWide() []string {
  116. return root.monthsWide[1:]
  117. }
  118. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  119. func (root *root) WeekdayAbbreviated(weekday time.Weekday) string {
  120. return root.daysAbbreviated[weekday]
  121. }
  122. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  123. func (root *root) WeekdaysAbbreviated() []string {
  124. return root.daysAbbreviated
  125. }
  126. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  127. func (root *root) WeekdayNarrow(weekday time.Weekday) string {
  128. return root.daysNarrow[weekday]
  129. }
  130. // WeekdaysNarrow returns the locales narrow weekdays
  131. func (root *root) WeekdaysNarrow() []string {
  132. return root.daysNarrow
  133. }
  134. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  135. func (root *root) WeekdayShort(weekday time.Weekday) string {
  136. return root.daysShort[weekday]
  137. }
  138. // WeekdaysShort returns the locales short weekdays
  139. func (root *root) WeekdaysShort() []string {
  140. return root.daysShort
  141. }
  142. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  143. func (root *root) WeekdayWide(weekday time.Weekday) string {
  144. return root.daysWide[weekday]
  145. }
  146. // WeekdaysWide returns the locales wide weekdays
  147. func (root *root) WeekdaysWide() []string {
  148. return root.daysWide
  149. }
  150. // Decimal returns the decimal point of number
  151. func (root *root) Decimal() string {
  152. return root.decimal
  153. }
  154. // Group returns the group of number
  155. func (root *root) Group() string {
  156. return root.group
  157. }
  158. // Group returns the minus sign of number
  159. func (root *root) Minus() string {
  160. return root.minus
  161. }
  162. // FmtNumber returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
  163. func (root *root) FmtNumber(num float64, v uint64) string {
  164. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  165. }
  166. // FmtPercent returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
  167. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  168. func (root *root) FmtPercent(num float64, v uint64) string {
  169. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  170. }
  171. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'root'
  172. func (root *root) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  173. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  174. symbol := root.currencies[currency]
  175. return string(append(append([]byte{}, symbol...), s...))
  176. }
  177. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'root'
  178. // in accounting notation.
  179. func (root *root) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  180. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  181. symbol := root.currencies[currency]
  182. return string(append(append([]byte{}, symbol...), s...))
  183. }
  184. // FmtDateShort returns the short date representation of 't' for 'root'
  185. func (root *root) FmtDateShort(t time.Time) string {
  186. b := make([]byte, 0, 32)
  187. if t.Year() > 0 {
  188. b = strconv.AppendInt(b, int64(t.Year()), 10)
  189. } else {
  190. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  191. }
  192. b = append(b, []byte{0x2d}...)
  193. if t.Month() < 10 {
  194. b = append(b, '0')
  195. }
  196. b = strconv.AppendInt(b, int64(t.Month()), 10)
  197. b = append(b, []byte{0x2d}...)
  198. if t.Day() < 10 {
  199. b = append(b, '0')
  200. }
  201. b = strconv.AppendInt(b, int64(t.Day()), 10)
  202. return string(b)
  203. }
  204. // FmtDateMedium returns the medium date representation of 't' for 'root'
  205. func (root *root) FmtDateMedium(t time.Time) string {
  206. b := make([]byte, 0, 32)
  207. if t.Year() > 0 {
  208. b = strconv.AppendInt(b, int64(t.Year()), 10)
  209. } else {
  210. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  211. }
  212. b = append(b, []byte{0x20}...)
  213. b = append(b, root.monthsAbbreviated[t.Month()]...)
  214. b = append(b, []byte{0x20}...)
  215. b = strconv.AppendInt(b, int64(t.Day()), 10)
  216. return string(b)
  217. }
  218. // FmtDateLong returns the long date representation of 't' for 'root'
  219. func (root *root) FmtDateLong(t time.Time) string {
  220. b := make([]byte, 0, 32)
  221. if t.Year() > 0 {
  222. b = strconv.AppendInt(b, int64(t.Year()), 10)
  223. } else {
  224. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  225. }
  226. b = append(b, []byte{0x20}...)
  227. b = append(b, root.monthsWide[t.Month()]...)
  228. b = append(b, []byte{0x20}...)
  229. b = strconv.AppendInt(b, int64(t.Day()), 10)
  230. return string(b)
  231. }
  232. // FmtDateFull returns the full date representation of 't' for 'root'
  233. func (root *root) FmtDateFull(t time.Time) string {
  234. b := make([]byte, 0, 32)
  235. if t.Year() > 0 {
  236. b = strconv.AppendInt(b, int64(t.Year()), 10)
  237. } else {
  238. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  239. }
  240. b = append(b, []byte{0x20}...)
  241. b = append(b, root.monthsWide[t.Month()]...)
  242. b = append(b, []byte{0x20}...)
  243. b = strconv.AppendInt(b, int64(t.Day()), 10)
  244. b = append(b, []byte{0x2c, 0x20}...)
  245. b = append(b, root.daysWide[t.Weekday()]...)
  246. return string(b)
  247. }
  248. // FmtTimeShort returns the short time representation of 't' for 'root'
  249. func (root *root) FmtTimeShort(t time.Time) string {
  250. b := make([]byte, 0, 32)
  251. if t.Hour() < 10 {
  252. b = append(b, '0')
  253. }
  254. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  255. b = append(b, root.timeSeparator...)
  256. if t.Minute() < 10 {
  257. b = append(b, '0')
  258. }
  259. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  260. return string(b)
  261. }
  262. // FmtTimeMedium returns the medium time representation of 't' for 'root'
  263. func (root *root) FmtTimeMedium(t time.Time) string {
  264. b := make([]byte, 0, 32)
  265. if t.Hour() < 10 {
  266. b = append(b, '0')
  267. }
  268. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  269. b = append(b, root.timeSeparator...)
  270. if t.Minute() < 10 {
  271. b = append(b, '0')
  272. }
  273. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  274. b = append(b, root.timeSeparator...)
  275. if t.Second() < 10 {
  276. b = append(b, '0')
  277. }
  278. b = strconv.AppendInt(b, int64(t.Second()), 10)
  279. return string(b)
  280. }
  281. // FmtTimeLong returns the long time representation of 't' for 'root'
  282. func (root *root) FmtTimeLong(t time.Time) string {
  283. b := make([]byte, 0, 32)
  284. if t.Hour() < 10 {
  285. b = append(b, '0')
  286. }
  287. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  288. b = append(b, root.timeSeparator...)
  289. if t.Minute() < 10 {
  290. b = append(b, '0')
  291. }
  292. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  293. b = append(b, root.timeSeparator...)
  294. if t.Second() < 10 {
  295. b = append(b, '0')
  296. }
  297. b = strconv.AppendInt(b, int64(t.Second()), 10)
  298. b = append(b, []byte{0x20}...)
  299. tz, _ := t.Zone()
  300. b = append(b, tz...)
  301. return string(b)
  302. }
  303. // FmtTimeFull returns the full time representation of 't' for 'root'
  304. func (root *root) FmtTimeFull(t time.Time) string {
  305. b := make([]byte, 0, 32)
  306. if t.Hour() < 10 {
  307. b = append(b, '0')
  308. }
  309. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  310. b = append(b, root.timeSeparator...)
  311. if t.Minute() < 10 {
  312. b = append(b, '0')
  313. }
  314. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  315. b = append(b, root.timeSeparator...)
  316. if t.Second() < 10 {
  317. b = append(b, '0')
  318. }
  319. b = strconv.AppendInt(b, int64(t.Second()), 10)
  320. b = append(b, []byte{0x20}...)
  321. tz, _ := t.Zone()
  322. if btz, ok := root.timezones[tz]; ok {
  323. b = append(b, btz...)
  324. } else {
  325. b = append(b, tz...)
  326. }
  327. return string(b)
  328. }