se.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package se
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type se 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. percentSuffix string
  19. perMille string
  20. timeSeparator string
  21. inifinity string
  22. currencies []string // idx = enum of currency code
  23. currencyPositiveSuffix string
  24. currencyNegativeSuffix string
  25. monthsAbbreviated []string
  26. monthsNarrow []string
  27. monthsWide []string
  28. daysAbbreviated []string
  29. daysNarrow []string
  30. daysShort []string
  31. daysWide []string
  32. periodsAbbreviated []string
  33. periodsNarrow []string
  34. periodsShort []string
  35. periodsWide []string
  36. erasAbbreviated []string
  37. erasNarrow []string
  38. erasWide []string
  39. timezones map[string]string
  40. }
  41. // New returns a new instance of translator for the 'se' locale
  42. func New() locales.Translator {
  43. return &se{
  44. locale: "se",
  45. pluralsCardinal: []locales.PluralRule{2, 3, 6},
  46. pluralsOrdinal: nil,
  47. pluralsRange: nil,
  48. decimal: ",",
  49. group: " ",
  50. minus: "−",
  51. percent: "%",
  52. perMille: "‰",
  53. timeSeparator: ":",
  54. inifinity: "∞",
  55. 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 ", "Dkr", "DOP ", "DZD ", "ECS ", "ECV ", "EEK ", "EGP ", "ERN ", "ESA ", "ESB ", "ESP ", "ETB ", "€", "FIM ", "FJD ", "FKP ", "FRF ", "GBP ", "GEK ", "GEL ", "GHC ", "GHS ", "GIP ", "GMD ", "GNF ", "GNS ", "GQE ", "GRD ", "GTQ ", "GWE ", "GWP ", "GYD ", "HK$", "HNL ", "HRD ", "HRK ", "HTG ", "HUF ", "IDR ", "IEP ", "ILP ", "ILR ", "ILS ", "₹", "IQD ", "IRR ", "ISJ ", "ISK ", "ITL ", "JMD ", "JOD ", "JP¥", "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 ", "MX$", "MXP ", "MXV ", "MYR ", "MZE ", "MZM ", "MZN ", "NAD ", "NGN ", "NIC ", "NIO ", "NLG ", "kr", "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 ", "Skr", "SGD ", "SHP ", "SIT ", "SKK ", "SLL ", "SOS ", "SRD ", "SRG ", "SSP ", "STD ", "SUR ", "SVC ", "SYP ", "SZL ", "฿", "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 "},
  56. percentSuffix: " ",
  57. currencyPositiveSuffix: " ",
  58. currencyNegativeSuffix: " ",
  59. monthsAbbreviated: []string{"", "ođđj", "guov", "njuk", "cuo", "mies", "geas", "suoi", "borg", "čakč", "golg", "skáb", "juov"},
  60. monthsNarrow: []string{"", "O", "G", "N", "C", "M", "G", "S", "B", "Č", "G", "S", "J"},
  61. monthsWide: []string{"", "ođđajagemánnu", "guovvamánnu", "njukčamánnu", "cuoŋománnu", "miessemánnu", "geassemánnu", "suoidnemánnu", "borgemánnu", "čakčamánnu", "golggotmánnu", "skábmamánnu", "juovlamánnu"},
  62. daysAbbreviated: []string{"sotn", "vuos", "maŋ", "gask", "duor", "bear", "láv"},
  63. daysNarrow: []string{"S", "V", "M", "G", "D", "B", "L"},
  64. daysWide: []string{"sotnabeaivi", "vuossárga", "maŋŋebárga", "gaskavahkku", "duorasdat", "bearjadat", "lávvardat"},
  65. periodsAbbreviated: []string{"i.b.", "e.b."},
  66. periodsWide: []string{"iđitbeaivet", "eahketbeaivet"},
  67. erasAbbreviated: []string{"o.Kr.", "m.Kr."},
  68. erasNarrow: []string{"", ""},
  69. erasWide: []string{"ovdal Kristtusa", "maŋŋel Kristtusa"},
  70. timezones: map[string]string{"OESZ": "nuorti-Eurohpá geassiáigi", "CDT": "CDT", "WIT": "WIT", "AWST": "AWST", "LHST": "LHST", "NZDT": "NZDT", "CLT": "CLT", "EAT": "EAT", "ARST": "ARST", "PST": "PST", "PDT": "PDT", "VET": "VET", "ACWDT": "ACWDT", "EST": "EST", "COST": "COST", "WART": "WART", "HNT": "HNT", "WITA": "WITA", "TMT": "TMT", "JDT": "JDT", "JST": "JST", "MYT": "MYT", "WEZ": "oarje-Eurohpá dábálašáigi", "HKT": "HKT", "MEZ": "gaska-Eurohpá dábálašáigi", "BT": "BT", "IST": "IST", "ACDT": "ACDT", "CST": "CST", "AEDT": "AEDT", "TMST": "TMST", "NZST": "NZST", "AEST": "AEST", "CAT": "CAT", "EDT": "EDT", "GFT": "GFT", "MDT": "MDT", "WESZ": "oarje-Eurohpá geassiáigi", "CHAST": "CHAST", "GMT": "Greenwich gaskka áigi", "HAT": "HAT", "AKST": "AKST", "ChST": "ChST", "ECT": "ECT", "ADT": "ADT", "WAT": "WAT", "WAST": "WAST", "WARST": "WARST", "AWDT": "AWDT", "MESZ": "gaska-Eurohpá geassiáigi", "OEZ": "nuorti-Eurohpá dábálašáigi", "ART": "ART", "AKDT": "AKDT", "ACWST": "ACWST", "CHADT": "CHADT", "SAST": "SAST", "HKST": "HKST", "∅∅∅": "∅∅∅", "SRT": "SRT", "GYT": "GYT", "COT": "COT", "SGT": "SGT", "MST": "MST", "LHDT": "LHDT", "BOT": "BOT", "HAST": "HAST", "WIB": "WIB", "ACST": "ACST", "AST": "AST", "HADT": "HADT", "UYT": "UYT", "UYST": "UYST", "CLST": "CLST"},
  71. }
  72. }
  73. // Locale returns the current translators string locale
  74. func (se *se) Locale() string {
  75. return se.locale
  76. }
  77. // PluralsCardinal returns the list of cardinal plural rules associated with 'se'
  78. func (se *se) PluralsCardinal() []locales.PluralRule {
  79. return se.pluralsCardinal
  80. }
  81. // PluralsOrdinal returns the list of ordinal plural rules associated with 'se'
  82. func (se *se) PluralsOrdinal() []locales.PluralRule {
  83. return se.pluralsOrdinal
  84. }
  85. // PluralsRange returns the list of range plural rules associated with 'se'
  86. func (se *se) PluralsRange() []locales.PluralRule {
  87. return se.pluralsRange
  88. }
  89. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'se'
  90. func (se *se) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  91. n := math.Abs(num)
  92. if n == 1 {
  93. return locales.PluralRuleOne
  94. } else if n == 2 {
  95. return locales.PluralRuleTwo
  96. }
  97. return locales.PluralRuleOther
  98. }
  99. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'se'
  100. func (se *se) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  101. return locales.PluralRuleUnknown
  102. }
  103. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'se'
  104. func (se *se) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  105. return locales.PluralRuleUnknown
  106. }
  107. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  108. func (se *se) MonthAbbreviated(month time.Month) string {
  109. return se.monthsAbbreviated[month]
  110. }
  111. // MonthsAbbreviated returns the locales abbreviated months
  112. func (se *se) MonthsAbbreviated() []string {
  113. return se.monthsAbbreviated[1:]
  114. }
  115. // MonthNarrow returns the locales narrow month given the 'month' provided
  116. func (se *se) MonthNarrow(month time.Month) string {
  117. return se.monthsNarrow[month]
  118. }
  119. // MonthsNarrow returns the locales narrow months
  120. func (se *se) MonthsNarrow() []string {
  121. return se.monthsNarrow[1:]
  122. }
  123. // MonthWide returns the locales wide month given the 'month' provided
  124. func (se *se) MonthWide(month time.Month) string {
  125. return se.monthsWide[month]
  126. }
  127. // MonthsWide returns the locales wide months
  128. func (se *se) MonthsWide() []string {
  129. return se.monthsWide[1:]
  130. }
  131. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  132. func (se *se) WeekdayAbbreviated(weekday time.Weekday) string {
  133. return se.daysAbbreviated[weekday]
  134. }
  135. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  136. func (se *se) WeekdaysAbbreviated() []string {
  137. return se.daysAbbreviated
  138. }
  139. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  140. func (se *se) WeekdayNarrow(weekday time.Weekday) string {
  141. return se.daysNarrow[weekday]
  142. }
  143. // WeekdaysNarrow returns the locales narrow weekdays
  144. func (se *se) WeekdaysNarrow() []string {
  145. return se.daysNarrow
  146. }
  147. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  148. func (se *se) WeekdayShort(weekday time.Weekday) string {
  149. return se.daysShort[weekday]
  150. }
  151. // WeekdaysShort returns the locales short weekdays
  152. func (se *se) WeekdaysShort() []string {
  153. return se.daysShort
  154. }
  155. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  156. func (se *se) WeekdayWide(weekday time.Weekday) string {
  157. return se.daysWide[weekday]
  158. }
  159. // WeekdaysWide returns the locales wide weekdays
  160. func (se *se) WeekdaysWide() []string {
  161. return se.daysWide
  162. }
  163. // FmtNumber returns 'num' with digits/precision of 'v' for 'se' and handles both Whole and Real numbers based on 'v'
  164. func (se *se) FmtNumber(num float64, v uint64) string {
  165. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  166. l := len(s) + 4 + 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, se.decimal[0])
  173. inWhole = true
  174. continue
  175. }
  176. if inWhole {
  177. if count == 3 {
  178. for j := len(se.group) - 1; j >= 0; j-- {
  179. b = append(b, se.group[j])
  180. }
  181. count = 1
  182. } else {
  183. count++
  184. }
  185. }
  186. b = append(b, s[i])
  187. }
  188. if num < 0 {
  189. for j := len(se.minus) - 1; j >= 0; j-- {
  190. b = append(b, se.minus[j])
  191. }
  192. }
  193. // reverse
  194. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  195. b[i], b[j] = b[j], b[i]
  196. }
  197. return string(b)
  198. }
  199. // FmtPercent returns 'num' with digits/precision of 'v' for 'se' and handles both Whole and Real numbers based on 'v'
  200. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  201. func (se *se) FmtPercent(num float64, v uint64) string {
  202. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  203. l := len(s) + 7
  204. b := make([]byte, 0, l)
  205. for i := len(s) - 1; i >= 0; i-- {
  206. if s[i] == '.' {
  207. b = append(b, se.decimal[0])
  208. continue
  209. }
  210. b = append(b, s[i])
  211. }
  212. if num < 0 {
  213. for j := len(se.minus) - 1; j >= 0; j-- {
  214. b = append(b, se.minus[j])
  215. }
  216. }
  217. // reverse
  218. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  219. b[i], b[j] = b[j], b[i]
  220. }
  221. b = append(b, se.percentSuffix...)
  222. b = append(b, se.percent...)
  223. return string(b)
  224. }
  225. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'se'
  226. func (se *se) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  227. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  228. symbol := se.currencies[currency]
  229. l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
  230. count := 0
  231. inWhole := v == 0
  232. b := make([]byte, 0, l)
  233. for i := len(s) - 1; i >= 0; i-- {
  234. if s[i] == '.' {
  235. b = append(b, se.decimal[0])
  236. inWhole = true
  237. continue
  238. }
  239. if inWhole {
  240. if count == 3 {
  241. for j := len(se.group) - 1; j >= 0; j-- {
  242. b = append(b, se.group[j])
  243. }
  244. count = 1
  245. } else {
  246. count++
  247. }
  248. }
  249. b = append(b, s[i])
  250. }
  251. if num < 0 {
  252. for j := len(se.minus) - 1; j >= 0; j-- {
  253. b = append(b, se.minus[j])
  254. }
  255. }
  256. // reverse
  257. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  258. b[i], b[j] = b[j], b[i]
  259. }
  260. if int(v) < 2 {
  261. if v == 0 {
  262. b = append(b, se.decimal...)
  263. }
  264. for i := 0; i < 2-int(v); i++ {
  265. b = append(b, '0')
  266. }
  267. }
  268. b = append(b, se.currencyPositiveSuffix...)
  269. b = append(b, symbol...)
  270. return string(b)
  271. }
  272. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'se'
  273. // in accounting notation.
  274. func (se *se) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  275. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  276. symbol := se.currencies[currency]
  277. l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
  278. count := 0
  279. inWhole := v == 0
  280. b := make([]byte, 0, l)
  281. for i := len(s) - 1; i >= 0; i-- {
  282. if s[i] == '.' {
  283. b = append(b, se.decimal[0])
  284. inWhole = true
  285. continue
  286. }
  287. if inWhole {
  288. if count == 3 {
  289. for j := len(se.group) - 1; j >= 0; j-- {
  290. b = append(b, se.group[j])
  291. }
  292. count = 1
  293. } else {
  294. count++
  295. }
  296. }
  297. b = append(b, s[i])
  298. }
  299. if num < 0 {
  300. for j := len(se.minus) - 1; j >= 0; j-- {
  301. b = append(b, se.minus[j])
  302. }
  303. }
  304. // reverse
  305. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  306. b[i], b[j] = b[j], b[i]
  307. }
  308. if int(v) < 2 {
  309. if v == 0 {
  310. b = append(b, se.decimal...)
  311. }
  312. for i := 0; i < 2-int(v); i++ {
  313. b = append(b, '0')
  314. }
  315. }
  316. if num < 0 {
  317. b = append(b, se.currencyNegativeSuffix...)
  318. b = append(b, symbol...)
  319. } else {
  320. b = append(b, se.currencyPositiveSuffix...)
  321. b = append(b, symbol...)
  322. }
  323. return string(b)
  324. }
  325. // FmtDateShort returns the short date representation of 't' for 'se'
  326. func (se *se) FmtDateShort(t time.Time) string {
  327. b := make([]byte, 0, 32)
  328. return string(b)
  329. }
  330. // FmtDateMedium returns the medium date representation of 't' for 'se'
  331. func (se *se) FmtDateMedium(t time.Time) string {
  332. b := make([]byte, 0, 32)
  333. return string(b)
  334. }
  335. // FmtDateLong returns the long date representation of 't' for 'se'
  336. func (se *se) FmtDateLong(t time.Time) string {
  337. b := make([]byte, 0, 32)
  338. return string(b)
  339. }
  340. // FmtDateFull returns the full date representation of 't' for 'se'
  341. func (se *se) FmtDateFull(t time.Time) string {
  342. b := make([]byte, 0, 32)
  343. return string(b)
  344. }
  345. // FmtTimeShort returns the short time representation of 't' for 'se'
  346. func (se *se) FmtTimeShort(t time.Time) string {
  347. b := make([]byte, 0, 32)
  348. return string(b)
  349. }
  350. // FmtTimeMedium returns the medium time representation of 't' for 'se'
  351. func (se *se) FmtTimeMedium(t time.Time) string {
  352. b := make([]byte, 0, 32)
  353. return string(b)
  354. }
  355. // FmtTimeLong returns the long time representation of 't' for 'se'
  356. func (se *se) FmtTimeLong(t time.Time) string {
  357. b := make([]byte, 0, 32)
  358. return string(b)
  359. }
  360. // FmtTimeFull returns the full time representation of 't' for 'se'
  361. func (se *se) FmtTimeFull(t time.Time) string {
  362. b := make([]byte, 0, 32)
  363. return string(b)
  364. }