shi.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 ", "BYR ", "BZD ", "CAD ", "CDF ", "CHE ", "CHF ", "CHW ", "CLE ", "CLF ", "CLP ", "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 ", "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{"ACDT": "ACDT", "ART": "ART", "EAT": "EAT", "MYT": "MYT", "CLT": "CLT", "WEZ": "WEZ", "MEZ": "MEZ", "PST": "PST", "WITA": "WITA", "WIB": "WIB", "ECT": "ECT", "WAST": "WAST", "WIT": "WIT", "ARST": "ARST", "GYT": "GYT", "JST": "JST", "WESZ": "WESZ", "MST": "MST", "ACWST": "ACWST", "ADT": "ADT", "OEZ": "OEZ", "CAT": "CAT", "COT": "COT", "HKT": "HKT", "IST": "IST", "GFT": "GFT", "OESZ": "OESZ", "LHST": "LHST", "CHADT": "CHADT", "AEST": "AEST", "HADT": "HADT", "AKDT": "AKDT", "LHDT": "LHDT", "WAT": "WAT", "MDT": "MDT", "SAST": "SAST", "HKST": "HKST", "AKST": "AKST", "AWST": "AWST", "CDT": "CDT", "EDT": "EDT", "VET": "VET", "AEDT": "AEDT", "EST": "EST", "AST": "AST", "∅∅∅": "∅∅∅", "TMST": "TMST", "PDT": "PDT", "CLST": "CLST", "HAT": "HAT", "ACWDT": "ACWDT", "SGT": "SGT", "WARST": "WARST", "ACST": "ACST", "SRT": "SRT", "UYT": "UYT", "CST": "CST", "WART": "WART", "BT": "BT", "GMT": "GMT", "HNT": "HNT", "AWDT": "AWDT", "NZDT": "NZDT", "BOT": "BOT", "ChST": "ChST", "NZST": "NZST", "CHAST": "CHAST", "COST": "COST", "HAST": "HAST", "TMT": "TMT", "JDT": "JDT", "UYST": "UYST", "MESZ": "MESZ"},
  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. // FmtNumber returns 'num' with digits/precision of 'v' for 'shi' and handles both Whole and Real numbers based on 'v'
  154. func (shi *shi) FmtNumber(num float64, v uint64) string {
  155. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  156. }
  157. // FmtPercent returns 'num' with digits/precision of 'v' for 'shi' and handles both Whole and Real numbers based on 'v'
  158. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  159. func (shi *shi) FmtPercent(num float64, v uint64) string {
  160. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  161. }
  162. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'shi'
  163. func (shi *shi) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  164. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  165. symbol := shi.currencies[currency]
  166. l := len(s) + len(symbol) + 1 + 2*len(s[:len(s)-int(v)-1])/3
  167. count := 0
  168. inWhole := v == 0
  169. b := make([]byte, 0, l)
  170. for i := len(s) - 1; i >= 0; i-- {
  171. if s[i] == '.' {
  172. b = append(b, shi.decimal[0])
  173. inWhole = true
  174. continue
  175. }
  176. if inWhole {
  177. if count == 3 {
  178. for j := len(shi.group) - 1; j >= 0; j-- {
  179. b = append(b, shi.group[j])
  180. }
  181. count = 1
  182. } else {
  183. count++
  184. }
  185. }
  186. b = append(b, s[i])
  187. }
  188. if num < 0 {
  189. b = append(b, shi.minus[0])
  190. }
  191. // reverse
  192. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  193. b[i], b[j] = b[j], b[i]
  194. }
  195. if int(v) < 2 {
  196. if v == 0 {
  197. b = append(b, shi.decimal...)
  198. }
  199. for i := 0; i < 2-int(v); i++ {
  200. b = append(b, '0')
  201. }
  202. }
  203. b = append(b, symbol...)
  204. return string(b)
  205. }
  206. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'shi'
  207. // in accounting notation.
  208. func (shi *shi) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  209. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  210. symbol := shi.currencies[currency]
  211. l := len(s) + len(symbol) + 1 + 2*len(s[:len(s)-int(v)-1])/3
  212. count := 0
  213. inWhole := v == 0
  214. b := make([]byte, 0, l)
  215. for i := len(s) - 1; i >= 0; i-- {
  216. if s[i] == '.' {
  217. b = append(b, shi.decimal[0])
  218. inWhole = true
  219. continue
  220. }
  221. if inWhole {
  222. if count == 3 {
  223. for j := len(shi.group) - 1; j >= 0; j-- {
  224. b = append(b, shi.group[j])
  225. }
  226. count = 1
  227. } else {
  228. count++
  229. }
  230. }
  231. b = append(b, s[i])
  232. }
  233. if num < 0 {
  234. b = append(b, shi.minus[0])
  235. }
  236. // reverse
  237. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  238. b[i], b[j] = b[j], b[i]
  239. }
  240. if int(v) < 2 {
  241. if v == 0 {
  242. b = append(b, shi.decimal...)
  243. }
  244. for i := 0; i < 2-int(v); i++ {
  245. b = append(b, '0')
  246. }
  247. }
  248. if num < 0 {
  249. b = append(b, symbol...)
  250. } else {
  251. b = append(b, symbol...)
  252. }
  253. return string(b)
  254. }
  255. // FmtDateShort returns the short date representation of 't' for 'shi'
  256. func (shi *shi) FmtDateShort(t time.Time) string {
  257. b := make([]byte, 0, 32)
  258. b = strconv.AppendInt(b, int64(t.Day()), 10)
  259. b = append(b, []byte{0x2f}...)
  260. b = strconv.AppendInt(b, int64(t.Month()), 10)
  261. b = append(b, []byte{0x2f}...)
  262. b = strconv.AppendInt(b, int64(t.Year()), 10)
  263. return string(b)
  264. }
  265. // FmtDateMedium returns the medium date representation of 't' for 'shi'
  266. func (shi *shi) FmtDateMedium(t time.Time) string {
  267. b := make([]byte, 0, 32)
  268. b = strconv.AppendInt(b, int64(t.Day()), 10)
  269. b = append(b, []byte{0x20}...)
  270. b = append(b, shi.monthsAbbreviated[t.Month()]...)
  271. b = append(b, []byte{0x2c, 0x20}...)
  272. b = strconv.AppendInt(b, int64(t.Year()), 10)
  273. return string(b)
  274. }
  275. // FmtDateLong returns the long date representation of 't' for 'shi'
  276. func (shi *shi) FmtDateLong(t time.Time) string {
  277. b := make([]byte, 0, 32)
  278. b = strconv.AppendInt(b, int64(t.Day()), 10)
  279. b = append(b, []byte{0x20}...)
  280. b = append(b, shi.monthsWide[t.Month()]...)
  281. b = append(b, []byte{0x20}...)
  282. b = strconv.AppendInt(b, int64(t.Year()), 10)
  283. return string(b)
  284. }
  285. // FmtDateFull returns the full date representation of 't' for 'shi'
  286. func (shi *shi) FmtDateFull(t time.Time) string {
  287. b := make([]byte, 0, 32)
  288. b = append(b, shi.daysWide[t.Weekday()]...)
  289. b = append(b, []byte{0x20}...)
  290. b = strconv.AppendInt(b, int64(t.Day()), 10)
  291. b = append(b, []byte{0x20}...)
  292. b = append(b, shi.monthsWide[t.Month()]...)
  293. b = append(b, []byte{0x20}...)
  294. b = strconv.AppendInt(b, int64(t.Year()), 10)
  295. return string(b)
  296. }
  297. // FmtTimeShort returns the short time representation of 't' for 'shi'
  298. func (shi *shi) FmtTimeShort(t time.Time) string {
  299. b := make([]byte, 0, 32)
  300. return string(b)
  301. }
  302. // FmtTimeMedium returns the medium time representation of 't' for 'shi'
  303. func (shi *shi) FmtTimeMedium(t time.Time) string {
  304. b := make([]byte, 0, 32)
  305. return string(b)
  306. }
  307. // FmtTimeLong returns the long time representation of 't' for 'shi'
  308. func (shi *shi) FmtTimeLong(t time.Time) string {
  309. b := make([]byte, 0, 32)
  310. return string(b)
  311. }
  312. // FmtTimeFull returns the full time representation of 't' for 'shi'
  313. func (shi *shi) FmtTimeFull(t time.Time) string {
  314. b := make([]byte, 0, 32)
  315. return string(b)
  316. }