root.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. timeSeparator: ":",
  46. 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 ", "р.", "$", "CA$", "CDF ", "CHE ", "CHF ", "CHW ", "CLE ", "CLF ", "$", "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 ", "RON ", "RSD ", "₽", "р.", "RF", "SAR ", "$", "SCR ", "SDD ", "SDG ", "SDP ", "kr", "$", "£", "SIT ", "SKK ", "SLL ", "SOS ", "$", "SRG ", "£", "Db", "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 "},
  47. monthsNarrow: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
  48. monthsWide: []string{"", "M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09", "M10", "M11", "M12"},
  49. daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
  50. daysWide: []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
  51. periodsAbbreviated: []string{"AM", "PM"},
  52. periodsNarrow: []string{"", ""},
  53. periodsWide: []string{"", ""},
  54. erasAbbreviated: []string{"BCE", "CE"},
  55. erasNarrow: []string{"", ""},
  56. erasWide: []string{"", ""},
  57. timezones: map[string]string{"GFT": "GFT", "∅∅∅": "∅∅∅", "PDT": "PDT", "LHST": "LHST", "JDT": "JDT", "MEZ": "MEZ", "WAST": "WAST", "ACST": "ACST", "AKDT": "AKDT", "PST": "PST", "LHDT": "LHDT", "GYT": "GYT", "HKT": "HKT", "HNT": "HNT", "MST": "MST", "CAT": "CAT", "SGT": "SGT", "BOT": "BOT", "WAT": "WAT", "HKST": "HKST", "WARST": "WARST", "WITA": "WITA", "AKST": "AKST", "MYT": "MYT", "ART": "ART", "TMST": "TMST", "JST": "JST", "SRT": "SRT", "ACWDT": "ACWDT", "CLST": "CLST", "OEZ": "OEZ", "GMT": "GMT", "NZDT": "NZDT", "ACWST": "ACWST", "AEDT": "AEDT", "WIB": "WIB", "AEST": "AEST", "CLT": "CLT", "EST": "EST", "OESZ": "OESZ", "TMT": "TMT", "HAST": "HAST", "AWST": "AWST", "CST": "CST", "COST": "COST", "AST": "AST", "ADT": "ADT", "IST": "IST", "ACDT": "ACDT", "ARST": "ARST", "AWDT": "AWDT", "UYST": "UYST", "EAT": "EAT", "CDT": "CDT", "WESZ": "WESZ", "CHADT": "CHADT", "MDT": "MDT", "UYT": "UYT", "WART": "WART", "VET": "VET", "HADT": "HADT", "SAST": "SAST", "WIT": "WIT", "EDT": "EDT", "COT": "COT", "BT": "BT", "MESZ": "MESZ", "ChST": "ChST", "CHAST": "CHAST", "HAT": "HAT", "NZST": "NZST", "WEZ": "WEZ", "ECT": "ECT"},
  58. }
  59. }
  60. // Locale returns the current translators string locale
  61. func (root *root) Locale() string {
  62. return root.locale
  63. }
  64. // PluralsCardinal returns the list of cardinal plural rules associated with 'root'
  65. func (root *root) PluralsCardinal() []locales.PluralRule {
  66. return root.pluralsCardinal
  67. }
  68. // PluralsOrdinal returns the list of ordinal plural rules associated with 'root'
  69. func (root *root) PluralsOrdinal() []locales.PluralRule {
  70. return root.pluralsOrdinal
  71. }
  72. // PluralsRange returns the list of range plural rules associated with 'root'
  73. func (root *root) PluralsRange() []locales.PluralRule {
  74. return root.pluralsRange
  75. }
  76. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'root'
  77. func (root *root) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  78. return locales.PluralRuleOther
  79. }
  80. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'root'
  81. func (root *root) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  82. return locales.PluralRuleOther
  83. }
  84. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'root'
  85. func (root *root) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  86. return locales.PluralRuleUnknown
  87. }
  88. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  89. func (root *root) MonthAbbreviated(month time.Month) string {
  90. return root.monthsAbbreviated[month]
  91. }
  92. // MonthsAbbreviated returns the locales abbreviated months
  93. func (root *root) MonthsAbbreviated() []string {
  94. return root.monthsAbbreviated[1:]
  95. }
  96. // MonthNarrow returns the locales narrow month given the 'month' provided
  97. func (root *root) MonthNarrow(month time.Month) string {
  98. return root.monthsNarrow[month]
  99. }
  100. // MonthsNarrow returns the locales narrow months
  101. func (root *root) MonthsNarrow() []string {
  102. return root.monthsNarrow[1:]
  103. }
  104. // MonthWide returns the locales wide month given the 'month' provided
  105. func (root *root) MonthWide(month time.Month) string {
  106. return root.monthsWide[month]
  107. }
  108. // MonthsWide returns the locales wide months
  109. func (root *root) MonthsWide() []string {
  110. return root.monthsWide[1:]
  111. }
  112. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  113. func (root *root) WeekdayAbbreviated(weekday time.Weekday) string {
  114. return root.daysAbbreviated[weekday]
  115. }
  116. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  117. func (root *root) WeekdaysAbbreviated() []string {
  118. return root.daysAbbreviated
  119. }
  120. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  121. func (root *root) WeekdayNarrow(weekday time.Weekday) string {
  122. return root.daysNarrow[weekday]
  123. }
  124. // WeekdaysNarrow returns the locales narrow weekdays
  125. func (root *root) WeekdaysNarrow() []string {
  126. return root.daysNarrow
  127. }
  128. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  129. func (root *root) WeekdayShort(weekday time.Weekday) string {
  130. return root.daysShort[weekday]
  131. }
  132. // WeekdaysShort returns the locales short weekdays
  133. func (root *root) WeekdaysShort() []string {
  134. return root.daysShort
  135. }
  136. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  137. func (root *root) WeekdayWide(weekday time.Weekday) string {
  138. return root.daysWide[weekday]
  139. }
  140. // WeekdaysWide returns the locales wide weekdays
  141. func (root *root) WeekdaysWide() []string {
  142. return root.daysWide
  143. }
  144. // FmtNumber returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
  145. func (root *root) FmtNumber(num float64, v uint64) string {
  146. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  147. }
  148. // FmtPercent returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
  149. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  150. func (root *root) FmtPercent(num float64, v uint64) string {
  151. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  152. }
  153. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'root'
  154. func (root *root) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  155. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  156. symbol := root.currencies[currency]
  157. return string(append(append([]byte{}, symbol...), s...))
  158. }
  159. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'root'
  160. // in accounting notation.
  161. func (root *root) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  162. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. symbol := root.currencies[currency]
  164. return string(append(append([]byte{}, symbol...), s...))
  165. }
  166. // FmtDateShort returns the short date representation of 't' for 'root'
  167. func (root *root) FmtDateShort(t time.Time) string {
  168. b := make([]byte, 0, 32)
  169. b = strconv.AppendInt(b, int64(t.Year()), 10)
  170. b = append(b, []byte{0x2d}...)
  171. if t.Month() < 10 {
  172. b = append(b, '0')
  173. }
  174. b = strconv.AppendInt(b, int64(t.Month()), 10)
  175. b = append(b, []byte{0x2d}...)
  176. if t.Day() < 10 {
  177. b = append(b, '0')
  178. }
  179. b = strconv.AppendInt(b, int64(t.Day()), 10)
  180. return string(b)
  181. }
  182. // FmtDateMedium returns the medium date representation of 't' for 'root'
  183. func (root *root) FmtDateMedium(t time.Time) string {
  184. b := make([]byte, 0, 32)
  185. b = strconv.AppendInt(b, int64(t.Year()), 10)
  186. b = append(b, []byte{0x20}...)
  187. b = append(b, root.monthsAbbreviated[t.Month()]...)
  188. b = append(b, []byte{0x20}...)
  189. b = strconv.AppendInt(b, int64(t.Day()), 10)
  190. return string(b)
  191. }
  192. // FmtDateLong returns the long date representation of 't' for 'root'
  193. func (root *root) FmtDateLong(t time.Time) string {
  194. b := make([]byte, 0, 32)
  195. b = strconv.AppendInt(b, int64(t.Year()), 10)
  196. b = append(b, []byte{0x20}...)
  197. b = append(b, root.monthsWide[t.Month()]...)
  198. b = append(b, []byte{0x20}...)
  199. b = strconv.AppendInt(b, int64(t.Day()), 10)
  200. return string(b)
  201. }
  202. // FmtDateFull returns the full date representation of 't' for 'root'
  203. func (root *root) FmtDateFull(t time.Time) string {
  204. b := make([]byte, 0, 32)
  205. b = strconv.AppendInt(b, int64(t.Year()), 10)
  206. b = append(b, []byte{0x20}...)
  207. b = append(b, root.monthsWide[t.Month()]...)
  208. b = append(b, []byte{0x20}...)
  209. b = strconv.AppendInt(b, int64(t.Day()), 10)
  210. b = append(b, []byte{0x2c, 0x20}...)
  211. b = append(b, root.daysWide[t.Weekday()]...)
  212. return string(b)
  213. }
  214. // FmtTimeShort returns the short time representation of 't' for 'root'
  215. func (root *root) FmtTimeShort(t time.Time) string {
  216. b := make([]byte, 0, 32)
  217. if t.Hour() < 10 {
  218. b = append(b, '0')
  219. }
  220. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  221. b = append(b, root.timeSeparator...)
  222. if t.Minute() < 10 {
  223. b = append(b, '0')
  224. }
  225. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  226. return string(b)
  227. }
  228. // FmtTimeMedium returns the medium time representation of 't' for 'root'
  229. func (root *root) FmtTimeMedium(t time.Time) string {
  230. b := make([]byte, 0, 32)
  231. if t.Hour() < 10 {
  232. b = append(b, '0')
  233. }
  234. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  235. b = append(b, root.timeSeparator...)
  236. if t.Minute() < 10 {
  237. b = append(b, '0')
  238. }
  239. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  240. b = append(b, root.timeSeparator...)
  241. if t.Second() < 10 {
  242. b = append(b, '0')
  243. }
  244. b = strconv.AppendInt(b, int64(t.Second()), 10)
  245. return string(b)
  246. }
  247. // FmtTimeLong returns the long time representation of 't' for 'root'
  248. func (root *root) FmtTimeLong(t time.Time) string {
  249. b := make([]byte, 0, 32)
  250. if t.Hour() < 10 {
  251. b = append(b, '0')
  252. }
  253. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  254. b = append(b, root.timeSeparator...)
  255. if t.Minute() < 10 {
  256. b = append(b, '0')
  257. }
  258. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  259. b = append(b, root.timeSeparator...)
  260. if t.Second() < 10 {
  261. b = append(b, '0')
  262. }
  263. b = strconv.AppendInt(b, int64(t.Second()), 10)
  264. b = append(b, []byte{0x20}...)
  265. tz, _ := t.Zone()
  266. b = append(b, tz...)
  267. return string(b)
  268. }
  269. // FmtTimeFull returns the full time representation of 't' for 'root'
  270. func (root *root) FmtTimeFull(t time.Time) string {
  271. b := make([]byte, 0, 32)
  272. if t.Hour() < 10 {
  273. b = append(b, '0')
  274. }
  275. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  276. b = append(b, root.timeSeparator...)
  277. if t.Minute() < 10 {
  278. b = append(b, '0')
  279. }
  280. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  281. b = append(b, root.timeSeparator...)
  282. if t.Second() < 10 {
  283. b = append(b, '0')
  284. }
  285. b = strconv.AppendInt(b, int64(t.Second()), 10)
  286. b = append(b, []byte{0x20}...)
  287. tz, _ := t.Zone()
  288. if btz, ok := root.timezones[tz]; ok {
  289. b = append(b, btz...)
  290. } else {
  291. b = append(b, tz...)
  292. }
  293. return string(b)
  294. }