smn.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package smn
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type smn 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 'smn' locale
  43. func New() locales.Translator {
  44. return &smn{
  45. locale: "smn",
  46. pluralsCardinal: []locales.PluralRule{2, 3, 6},
  47. pluralsOrdinal: nil,
  48. pluralsRange: nil,
  49. timeSeparator: ":",
  50. 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 "},
  51. currencyPositivePrefix: " ",
  52. currencyPositiveSuffix: "K",
  53. currencyNegativePrefix: " ",
  54. currencyNegativeSuffix: "K",
  55. monthsWide: []string{"", "uđđâivemáánu", "kuovâmáánu", "njuhčâmáánu", "cuáŋuimáánu", "vyesimáánu", "kesimáánu", "syeinimáánu", "porgemáánu", "čohčâmáánu", "roovvâdmáánu", "skammâmáánu", "juovlâmáánu"},
  56. daysAbbreviated: []string{"pa", "vu", "ma", "ko", "tu", "vá", "lá"},
  57. daysNarrow: []string{"P", "V", "M", "K", "T", "V", "L"},
  58. daysWide: []string{"pasepeeivi", "vuossaargâ", "majebaargâ", "koskoho", "tuorâstuv", "vástuppeeivi", "lávurduv"},
  59. timezones: map[string]string{"JST": "JST", "CDT": "CDT", "WEZ": "WEZ", "GFT": "GFT", "MESZ": "MESZ", "WART": "WART", "WARST": "WARST", "WIB": "WIB", "PDT": "PDT", "AWDT": "AWDT", "LHDT": "LHDT", "CLST": "CLST", "MDT": "MDT", "HKST": "HKST", "MEZ": "MEZ", "UYST": "UYST", "COT": "COT", "AST": "AST", "GMT": "GMT", "ACST": "ACST", "NZST": "NZST", "ACWDT": "ACWDT", "ChST": "ChST", "ECT": "ECT", "WAT": "WAT", "HKT": "HKT", "VET": "VET", "EST": "EST", "COST": "COST", "TMST": "TMST", "ARST": "ARST", "NZDT": "NZDT", "CAT": "CAT", "TMT": "TMT", "OESZ": "OESZ", "ART": "ART", "HADT": "HADT", "WITA": "WITA", "OEZ": "OEZ", "CLT": "CLT", "MST": "MST", "AKST": "AKST", "LHST": "LHST", "AEST": "AEST", "WAST": "WAST", "BT": "BT", "HAST": "HAST", "WIT": "WIT", "MYT": "MYT", "BOT": "BOT", "CHADT": "CHADT", "WESZ": "WESZ", "JDT": "JDT", "SRT": "SRT", "PST": "PST", "AEDT": "AEDT", "SGT": "SGT", "HNT": "HNT", "UYT": "UYT", "HAT": "HAT", "CST": "CST", "ACDT": "ACDT", "∅∅∅": "∅∅∅", "EDT": "EDT", "IST": "IST", "SAST": "SAST", "EAT": "EAT", "AKDT": "AKDT", "AWST": "AWST", "GYT": "GYT", "CHAST": "CHAST", "ACWST": "ACWST", "ADT": "ADT"},
  60. }
  61. }
  62. // Locale returns the current translators string locale
  63. func (smn *smn) Locale() string {
  64. return smn.locale
  65. }
  66. // PluralsCardinal returns the list of cardinal plural rules associated with 'smn'
  67. func (smn *smn) PluralsCardinal() []locales.PluralRule {
  68. return smn.pluralsCardinal
  69. }
  70. // PluralsOrdinal returns the list of ordinal plural rules associated with 'smn'
  71. func (smn *smn) PluralsOrdinal() []locales.PluralRule {
  72. return smn.pluralsOrdinal
  73. }
  74. // PluralsRange returns the list of range plural rules associated with 'smn'
  75. func (smn *smn) PluralsRange() []locales.PluralRule {
  76. return smn.pluralsRange
  77. }
  78. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'smn'
  79. func (smn *smn) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  80. n := math.Abs(num)
  81. if n == 1 {
  82. return locales.PluralRuleOne
  83. } else if n == 2 {
  84. return locales.PluralRuleTwo
  85. }
  86. return locales.PluralRuleOther
  87. }
  88. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'smn'
  89. func (smn *smn) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  90. return locales.PluralRuleUnknown
  91. }
  92. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'smn'
  93. func (smn *smn) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  94. return locales.PluralRuleUnknown
  95. }
  96. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  97. func (smn *smn) MonthAbbreviated(month time.Month) string {
  98. return smn.monthsAbbreviated[month]
  99. }
  100. // MonthsAbbreviated returns the locales abbreviated months
  101. func (smn *smn) MonthsAbbreviated() []string {
  102. return smn.monthsAbbreviated[1:]
  103. }
  104. // MonthNarrow returns the locales narrow month given the 'month' provided
  105. func (smn *smn) MonthNarrow(month time.Month) string {
  106. return smn.monthsNarrow[month]
  107. }
  108. // MonthsNarrow returns the locales narrow months
  109. func (smn *smn) MonthsNarrow() []string {
  110. return smn.monthsNarrow[1:]
  111. }
  112. // MonthWide returns the locales wide month given the 'month' provided
  113. func (smn *smn) MonthWide(month time.Month) string {
  114. return smn.monthsWide[month]
  115. }
  116. // MonthsWide returns the locales wide months
  117. func (smn *smn) MonthsWide() []string {
  118. return smn.monthsWide[1:]
  119. }
  120. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  121. func (smn *smn) WeekdayAbbreviated(weekday time.Weekday) string {
  122. return smn.daysAbbreviated[weekday]
  123. }
  124. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  125. func (smn *smn) WeekdaysAbbreviated() []string {
  126. return smn.daysAbbreviated
  127. }
  128. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  129. func (smn *smn) WeekdayNarrow(weekday time.Weekday) string {
  130. return smn.daysNarrow[weekday]
  131. }
  132. // WeekdaysNarrow returns the locales narrow weekdays
  133. func (smn *smn) WeekdaysNarrow() []string {
  134. return smn.daysNarrow
  135. }
  136. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  137. func (smn *smn) WeekdayShort(weekday time.Weekday) string {
  138. return smn.daysShort[weekday]
  139. }
  140. // WeekdaysShort returns the locales short weekdays
  141. func (smn *smn) WeekdaysShort() []string {
  142. return smn.daysShort
  143. }
  144. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  145. func (smn *smn) WeekdayWide(weekday time.Weekday) string {
  146. return smn.daysWide[weekday]
  147. }
  148. // WeekdaysWide returns the locales wide weekdays
  149. func (smn *smn) WeekdaysWide() []string {
  150. return smn.daysWide
  151. }
  152. // FmtNumber returns 'num' with digits/precision of 'v' for 'smn' and handles both Whole and Real numbers based on 'v'
  153. func (smn *smn) FmtNumber(num float64, v uint64) string {
  154. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  155. }
  156. // FmtPercent returns 'num' with digits/precision of 'v' for 'smn' and handles both Whole and Real numbers based on 'v'
  157. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  158. func (smn *smn) FmtPercent(num float64, v uint64) string {
  159. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  160. }
  161. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'smn'
  162. func (smn *smn) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  163. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  164. symbol := smn.currencies[currency]
  165. l := len(s) + len(symbol) + 3
  166. b := make([]byte, 0, l)
  167. for i := len(s) - 1; i >= 0; i-- {
  168. if s[i] == '.' {
  169. b = append(b, smn.decimal[0])
  170. continue
  171. }
  172. b = append(b, s[i])
  173. }
  174. for j := len(symbol) - 1; j >= 0; j-- {
  175. b = append(b, symbol[j])
  176. }
  177. for j := len(smn.currencyPositivePrefix) - 1; j >= 0; j-- {
  178. b = append(b, smn.currencyPositivePrefix[j])
  179. }
  180. if num < 0 {
  181. b = append(b, smn.minus[0])
  182. }
  183. // reverse
  184. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  185. b[i], b[j] = b[j], b[i]
  186. }
  187. b = append(b, smn.currencyPositiveSuffix...)
  188. return string(b)
  189. }
  190. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'smn'
  191. // in accounting notation.
  192. func (smn *smn) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  193. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  194. symbol := smn.currencies[currency]
  195. l := len(s) + len(symbol) + 3
  196. b := make([]byte, 0, l)
  197. for i := len(s) - 1; i >= 0; i-- {
  198. if s[i] == '.' {
  199. b = append(b, smn.decimal[0])
  200. continue
  201. }
  202. b = append(b, s[i])
  203. }
  204. if num < 0 {
  205. for j := len(symbol) - 1; j >= 0; j-- {
  206. b = append(b, symbol[j])
  207. }
  208. for j := len(smn.currencyNegativePrefix) - 1; j >= 0; j-- {
  209. b = append(b, smn.currencyNegativePrefix[j])
  210. }
  211. b = append(b, smn.minus[0])
  212. } else {
  213. for j := len(symbol) - 1; j >= 0; j-- {
  214. b = append(b, symbol[j])
  215. }
  216. for j := len(smn.currencyPositivePrefix) - 1; j >= 0; j-- {
  217. b = append(b, smn.currencyPositivePrefix[j])
  218. }
  219. }
  220. // reverse
  221. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  222. b[i], b[j] = b[j], b[i]
  223. }
  224. if num < 0 {
  225. b = append(b, smn.currencyNegativeSuffix...)
  226. } else {
  227. b = append(b, smn.currencyPositiveSuffix...)
  228. }
  229. return string(b)
  230. }
  231. // FmtDateShort returns the short date representation of 't' for 'smn'
  232. func (smn *smn) FmtDateShort(t time.Time) string {
  233. b := make([]byte, 0, 32)
  234. return string(b)
  235. }
  236. // FmtDateMedium returns the medium date representation of 't' for 'smn'
  237. func (smn *smn) FmtDateMedium(t time.Time) string {
  238. b := make([]byte, 0, 32)
  239. return string(b)
  240. }
  241. // FmtDateLong returns the long date representation of 't' for 'smn'
  242. func (smn *smn) FmtDateLong(t time.Time) string {
  243. b := make([]byte, 0, 32)
  244. return string(b)
  245. }
  246. // FmtDateFull returns the full date representation of 't' for 'smn'
  247. func (smn *smn) FmtDateFull(t time.Time) string {
  248. b := make([]byte, 0, 32)
  249. return string(b)
  250. }
  251. // FmtTimeShort returns the short time representation of 't' for 'smn'
  252. func (smn *smn) FmtTimeShort(t time.Time) string {
  253. b := make([]byte, 0, 32)
  254. return string(b)
  255. }
  256. // FmtTimeMedium returns the medium time representation of 't' for 'smn'
  257. func (smn *smn) FmtTimeMedium(t time.Time) string {
  258. b := make([]byte, 0, 32)
  259. return string(b)
  260. }
  261. // FmtTimeLong returns the long time representation of 't' for 'smn'
  262. func (smn *smn) FmtTimeLong(t time.Time) string {
  263. b := make([]byte, 0, 32)
  264. return string(b)
  265. }
  266. // FmtTimeFull returns the full time representation of 't' for 'smn'
  267. func (smn *smn) FmtTimeFull(t time.Time) string {
  268. b := make([]byte, 0, 32)
  269. return string(b)
  270. }