ii.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package ii
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ii 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. currencyPositivePrefix string
  23. currencyPositiveSuffix string
  24. currencyNegativePrefix string
  25. currencyNegativeSuffix string
  26. monthsAbbreviated []string
  27. monthsNarrow []string
  28. monthsWide []string
  29. daysAbbreviated []string
  30. daysNarrow []string
  31. daysShort []string
  32. daysWide []string
  33. periodsAbbreviated []string
  34. periodsNarrow []string
  35. periodsShort []string
  36. periodsWide []string
  37. erasAbbreviated []string
  38. erasNarrow []string
  39. erasWide []string
  40. timezones map[string]string
  41. }
  42. // New returns a new instance of translator for the 'ii' locale
  43. func New() locales.Translator {
  44. return &ii{
  45. locale: "ii",
  46. pluralsCardinal: []locales.PluralRule{6},
  47. pluralsOrdinal: nil,
  48. pluralsRange: nil,
  49. decimal: ".",
  50. group: ",",
  51. timeSeparator: ":",
  52. 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 ", "BYR ", "BZD ", "CAD ", "CDF ", "CHE ", "CHF ", "CHW ", "CLE ", "CLF ", "CLP ", "CNX ", "¥", "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 ", "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 "},
  53. currencyPositivePrefix: " ",
  54. currencyPositiveSuffix: "K",
  55. currencyNegativePrefix: " ",
  56. currencyNegativeSuffix: "K",
  57. monthsAbbreviated: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
  58. monthsWide: []string{"", "ꋍꆪ", "ꑍꆪ", "ꌕꆪ", "ꇖꆪ", "ꉬꆪ", "ꃘꆪ", "ꏃꆪ", "ꉆꆪ", "ꈬꆪ", "ꊰꆪ", "ꊰꊪꆪ", "ꊰꑋꆪ"},
  59. daysAbbreviated: []string{"ꑭꆏ", "ꆏꋍ", "ꆏꑍ", "ꆏꌕ", "ꆏꇖ", "ꆏꉬ", "ꆏꃘ"},
  60. daysNarrow: []string{"ꆏ", "ꋍ", "ꑍ", "ꌕ", "ꇖ", "ꉬ", "ꃘ"},
  61. daysWide: []string{"ꑭꆏꑍ", "ꆏꊂꋍ", "ꆏꊂꑍ", "ꆏꊂꌕ", "ꆏꊂꇖ", "ꆏꊂꉬ", "ꆏꊂꃘ"},
  62. periodsAbbreviated: []string{"ꎸꄑ", "ꁯꋒ"},
  63. periodsWide: []string{"ꎸꄑ", "ꁯꋒ"},
  64. erasAbbreviated: []string{"ꃅꋊꂿ", "ꃅꋊꊂ"},
  65. erasNarrow: []string{"", ""},
  66. erasWide: []string{"", ""},
  67. timezones: map[string]string{"ART": "ART", "WEZ": "WEZ", "ACWDT": "ACWDT", "ChST": "ChST", "HKST": "HKST", "WIB": "WIB", "GFT": "GFT", "MESZ": "MESZ", "AEDT": "AEDT", "SGT": "SGT", "TMT": "TMT", "AWDT": "AWDT", "VET": "VET", "HNT": "HNT", "MEZ": "MEZ", "LHST": "LHST", "CHAST": "CHAST", "IST": "IST", "EAT": "EAT", "MDT": "MDT", "EDT": "EDT", "GMT": "GMT", "AEST": "AEST", "CAT": "CAT", "WIT": "WIT", "LHDT": "LHDT", "CLST": "CLST", "NZST": "NZST", "HKT": "HKT", "WITA": "WITA", "∅∅∅": "∅∅∅", "CDT": "CDT", "AKDT": "AKDT", "NZDT": "NZDT", "EST": "EST", "MYT": "MYT", "JST": "JST", "SRT": "SRT", "BOT": "BOT", "TMST": "TMST", "AKST": "AKST", "AWST": "AWST", "ADT": "ADT", "BT": "BT", "ACDT": "ACDT", "ARST": "ARST", "ECT": "ECT", "COT": "COT", "SAST": "SAST", "WAST": "WAST", "CHADT": "CHADT", "CLT": "CLT", "WAT": "WAT", "ACST": "ACST", "JDT": "JDT", "WESZ": "WESZ", "WART": "WART", "OEZ": "OEZ", "OESZ": "OESZ", "HAT": "HAT", "AST": "AST", "PDT": "PDT", "CST": "CST", "UYST": "UYST", "UYT": "UYT", "ACWST": "ACWST", "COST": "COST", "HAST": "HAST", "HADT": "HADT", "MST": "MST", "PST": "PST", "GYT": "GYT", "WARST": "WARST"},
  68. }
  69. }
  70. // Locale returns the current translators string locale
  71. func (ii *ii) Locale() string {
  72. return ii.locale
  73. }
  74. // PluralsCardinal returns the list of cardinal plural rules associated with 'ii'
  75. func (ii *ii) PluralsCardinal() []locales.PluralRule {
  76. return ii.pluralsCardinal
  77. }
  78. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ii'
  79. func (ii *ii) PluralsOrdinal() []locales.PluralRule {
  80. return ii.pluralsOrdinal
  81. }
  82. // PluralsRange returns the list of range plural rules associated with 'ii'
  83. func (ii *ii) PluralsRange() []locales.PluralRule {
  84. return ii.pluralsRange
  85. }
  86. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ii'
  87. func (ii *ii) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  88. return locales.PluralRuleOther
  89. }
  90. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ii'
  91. func (ii *ii) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  92. return locales.PluralRuleUnknown
  93. }
  94. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ii'
  95. func (ii *ii) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  96. return locales.PluralRuleUnknown
  97. }
  98. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  99. func (ii *ii) MonthAbbreviated(month time.Month) string {
  100. return ii.monthsAbbreviated[month]
  101. }
  102. // MonthsAbbreviated returns the locales abbreviated months
  103. func (ii *ii) MonthsAbbreviated() []string {
  104. return ii.monthsAbbreviated[1:]
  105. }
  106. // MonthNarrow returns the locales narrow month given the 'month' provided
  107. func (ii *ii) MonthNarrow(month time.Month) string {
  108. return ii.monthsNarrow[month]
  109. }
  110. // MonthsNarrow returns the locales narrow months
  111. func (ii *ii) MonthsNarrow() []string {
  112. return ii.monthsNarrow[1:]
  113. }
  114. // MonthWide returns the locales wide month given the 'month' provided
  115. func (ii *ii) MonthWide(month time.Month) string {
  116. return ii.monthsWide[month]
  117. }
  118. // MonthsWide returns the locales wide months
  119. func (ii *ii) MonthsWide() []string {
  120. return ii.monthsWide[1:]
  121. }
  122. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  123. func (ii *ii) WeekdayAbbreviated(weekday time.Weekday) string {
  124. return ii.daysAbbreviated[weekday]
  125. }
  126. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  127. func (ii *ii) WeekdaysAbbreviated() []string {
  128. return ii.daysAbbreviated
  129. }
  130. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  131. func (ii *ii) WeekdayNarrow(weekday time.Weekday) string {
  132. return ii.daysNarrow[weekday]
  133. }
  134. // WeekdaysNarrow returns the locales narrow weekdays
  135. func (ii *ii) WeekdaysNarrow() []string {
  136. return ii.daysNarrow
  137. }
  138. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  139. func (ii *ii) WeekdayShort(weekday time.Weekday) string {
  140. return ii.daysShort[weekday]
  141. }
  142. // WeekdaysShort returns the locales short weekdays
  143. func (ii *ii) WeekdaysShort() []string {
  144. return ii.daysShort
  145. }
  146. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  147. func (ii *ii) WeekdayWide(weekday time.Weekday) string {
  148. return ii.daysWide[weekday]
  149. }
  150. // WeekdaysWide returns the locales wide weekdays
  151. func (ii *ii) WeekdaysWide() []string {
  152. return ii.daysWide
  153. }
  154. // FmtNumber returns 'num' with digits/precision of 'v' for 'ii' and handles both Whole and Real numbers based on 'v'
  155. func (ii *ii) FmtNumber(num float64, v uint64) string {
  156. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  157. }
  158. // FmtPercent returns 'num' with digits/precision of 'v' for 'ii' and handles both Whole and Real numbers based on 'v'
  159. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  160. func (ii *ii) FmtPercent(num float64, v uint64) string {
  161. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  162. }
  163. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ii'
  164. func (ii *ii) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  165. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  166. symbol := ii.currencies[currency]
  167. l := len(s) + len(symbol) + 4
  168. b := make([]byte, 0, l)
  169. for i := len(s) - 1; i >= 0; i-- {
  170. if s[i] == '.' {
  171. b = append(b, ii.decimal[0])
  172. continue
  173. }
  174. b = append(b, s[i])
  175. }
  176. for j := len(symbol) - 1; j >= 0; j-- {
  177. b = append(b, symbol[j])
  178. }
  179. for j := len(ii.currencyPositivePrefix) - 1; j >= 0; j-- {
  180. b = append(b, ii.currencyPositivePrefix[j])
  181. }
  182. if num < 0 {
  183. b = append(b, ii.minus[0])
  184. }
  185. // reverse
  186. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  187. b[i], b[j] = b[j], b[i]
  188. }
  189. b = append(b, ii.currencyPositiveSuffix...)
  190. return string(b)
  191. }
  192. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ii'
  193. // in accounting notation.
  194. func (ii *ii) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  195. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  196. symbol := ii.currencies[currency]
  197. l := len(s) + len(symbol) + 4
  198. b := make([]byte, 0, l)
  199. for i := len(s) - 1; i >= 0; i-- {
  200. if s[i] == '.' {
  201. b = append(b, ii.decimal[0])
  202. continue
  203. }
  204. b = append(b, s[i])
  205. }
  206. if num < 0 {
  207. for j := len(symbol) - 1; j >= 0; j-- {
  208. b = append(b, symbol[j])
  209. }
  210. for j := len(ii.currencyNegativePrefix) - 1; j >= 0; j-- {
  211. b = append(b, ii.currencyNegativePrefix[j])
  212. }
  213. b = append(b, ii.minus[0])
  214. } else {
  215. for j := len(symbol) - 1; j >= 0; j-- {
  216. b = append(b, symbol[j])
  217. }
  218. for j := len(ii.currencyPositivePrefix) - 1; j >= 0; j-- {
  219. b = append(b, ii.currencyPositivePrefix[j])
  220. }
  221. }
  222. // reverse
  223. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  224. b[i], b[j] = b[j], b[i]
  225. }
  226. if num < 0 {
  227. b = append(b, ii.currencyNegativeSuffix...)
  228. } else {
  229. b = append(b, ii.currencyPositiveSuffix...)
  230. }
  231. return string(b)
  232. }
  233. // FmtDateShort returns the short date representation of 't' for 'ii'
  234. func (ii *ii) FmtDateShort(t time.Time) string {
  235. b := make([]byte, 0, 32)
  236. return string(b)
  237. }
  238. // FmtDateMedium returns the medium date representation of 't' for 'ii'
  239. func (ii *ii) FmtDateMedium(t time.Time) string {
  240. b := make([]byte, 0, 32)
  241. return string(b)
  242. }
  243. // FmtDateLong returns the long date representation of 't' for 'ii'
  244. func (ii *ii) FmtDateLong(t time.Time) string {
  245. b := make([]byte, 0, 32)
  246. return string(b)
  247. }
  248. // FmtDateFull returns the full date representation of 't' for 'ii'
  249. func (ii *ii) FmtDateFull(t time.Time) string {
  250. b := make([]byte, 0, 32)
  251. return string(b)
  252. }
  253. // FmtTimeShort returns the short time representation of 't' for 'ii'
  254. func (ii *ii) FmtTimeShort(t time.Time) string {
  255. b := make([]byte, 0, 32)
  256. h := t.Hour()
  257. if h > 12 {
  258. h -= 12
  259. }
  260. b = strconv.AppendInt(b, int64(h), 10)
  261. b = append(b, ii.timeSeparator...)
  262. if t.Minute() < 10 {
  263. b = append(b, '0')
  264. }
  265. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  266. b = append(b, []byte{0x20}...)
  267. if t.Hour() < 12 {
  268. b = append(b, ii.periodsAbbreviated[0]...)
  269. } else {
  270. b = append(b, ii.periodsAbbreviated[1]...)
  271. }
  272. return string(b)
  273. }
  274. // FmtTimeMedium returns the medium time representation of 't' for 'ii'
  275. func (ii *ii) FmtTimeMedium(t time.Time) string {
  276. b := make([]byte, 0, 32)
  277. h := t.Hour()
  278. if h > 12 {
  279. h -= 12
  280. }
  281. b = strconv.AppendInt(b, int64(h), 10)
  282. b = append(b, ii.timeSeparator...)
  283. if t.Minute() < 10 {
  284. b = append(b, '0')
  285. }
  286. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  287. b = append(b, ii.timeSeparator...)
  288. if t.Second() < 10 {
  289. b = append(b, '0')
  290. }
  291. b = strconv.AppendInt(b, int64(t.Second()), 10)
  292. b = append(b, []byte{0x20}...)
  293. if t.Hour() < 12 {
  294. b = append(b, ii.periodsAbbreviated[0]...)
  295. } else {
  296. b = append(b, ii.periodsAbbreviated[1]...)
  297. }
  298. return string(b)
  299. }
  300. // FmtTimeLong returns the long time representation of 't' for 'ii'
  301. func (ii *ii) FmtTimeLong(t time.Time) string {
  302. b := make([]byte, 0, 32)
  303. h := t.Hour()
  304. if h > 12 {
  305. h -= 12
  306. }
  307. b = strconv.AppendInt(b, int64(h), 10)
  308. b = append(b, ii.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, ii.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. if t.Hour() < 12 {
  320. b = append(b, ii.periodsAbbreviated[0]...)
  321. } else {
  322. b = append(b, ii.periodsAbbreviated[1]...)
  323. }
  324. b = append(b, []byte{0x20}...)
  325. tz, _ := t.Zone()
  326. b = append(b, tz...)
  327. return string(b)
  328. }
  329. // FmtTimeFull returns the full time representation of 't' for 'ii'
  330. func (ii *ii) FmtTimeFull(t time.Time) string {
  331. b := make([]byte, 0, 32)
  332. h := t.Hour()
  333. if h > 12 {
  334. h -= 12
  335. }
  336. b = strconv.AppendInt(b, int64(h), 10)
  337. b = append(b, ii.timeSeparator...)
  338. if t.Minute() < 10 {
  339. b = append(b, '0')
  340. }
  341. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  342. b = append(b, ii.timeSeparator...)
  343. if t.Second() < 10 {
  344. b = append(b, '0')
  345. }
  346. b = strconv.AppendInt(b, int64(t.Second()), 10)
  347. b = append(b, []byte{0x20}...)
  348. if t.Hour() < 12 {
  349. b = append(b, ii.periodsAbbreviated[0]...)
  350. } else {
  351. b = append(b, ii.periodsAbbreviated[1]...)
  352. }
  353. b = append(b, []byte{0x20}...)
  354. tz, _ := t.Zone()
  355. if btz, ok := ii.timezones[tz]; ok {
  356. b = append(b, btz...)
  357. } else {
  358. b = append(b, tz...)
  359. }
  360. return string(b)
  361. }