shi.go 15 KB

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