sah.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package sah
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type sah 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 'sah' locale
  43. func New() locales.Translator {
  44. return &sah{
  45. locale: "sah",
  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 ", "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 ", "₽", "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{"", "Тохс", "Олун", "Клн_ттр", "Мус_уст", "Ыам_йн", "Бэс_йн", "От_йн", "Атрдь_йн", "Блҕн_йн", "Алт", "Сэт", "Ахс"},
  58. monthsNarrow: []string{"", "Т", "О", "К", "М", "Ы", "Б", "О", "А", "Б", "А", "С", "А"},
  59. monthsWide: []string{"", "Тохсунньу", "Олунньу", "Кулун тутар", "Муус устар", "Ыам ыйын", "Бэс ыйын", "От ыйын", "Атырдьых ыйын", "Балаҕан ыйын", "Алтынньы", "Сэтинньи", "Ахсынньы"},
  60. daysAbbreviated: []string{"Бс", "Бн", "Оп", "Сэ", "Чп", "Бэ", "Сб"},
  61. daysNarrow: []string{"Б", "Б", "О", "С", "Ч", "Б", "С"},
  62. daysWide: []string{"Баскыһыанньа", "Бэнидиэлинньик", "Оптуорунньук", "Сэрэдэ", "Чэппиэр", "Бээтиҥсэ", "Субуота"},
  63. periodsAbbreviated: []string{"ЭИ", "ЭК"},
  64. periodsWide: []string{"ЭИ", "ЭК"},
  65. erasAbbreviated: []string{"б. э. и.", "б. э"},
  66. erasNarrow: []string{"", ""},
  67. erasWide: []string{"", ""},
  68. timezones: map[string]string{"ACDT": "ACDT", "TMT": "TMT", "MESZ": "MESZ", "VET": "VET", "ADT": "ADT", "GMT": "GMT", "ACST": "ACST", "NZST": "NZST", "AEDT": "AEDT", "ECT": "ECT", "HKT": "HKT", "OEZ": "OEZ", "HNT": "HNT", "GFT": "GFT", "GYT": "GYT", "EST": "EST", "EDT": "EDT", "COT": "COT", "COST": "COST", "BT": "BT", "ARST": "ARST", "SGT": "SGT", "TMST": "TMST", "PST": "PST", "MST": "MST", "MEZ": "MEZ", "HKST": "HKST", "HAT": "HAT", "SRT": "SRT", "AEST": "AEST", "CLT": "CLT", "HAST": "HAST", "CDT": "CDT", "WAST": "WAST", "ACWDT": "ACWDT", "WIT": "WIT", "IST": "IST", "ChST": "ChST", "SAST": "SAST", "CST": "CST", "NZDT": "NZDT", "BOT": "BOT", "CHAST": "CHAST", "CLST": "CLST", "WAT": "WAT", "WITA": "WITA", "AKDT": "AKDT", "LHDT": "LHDT", "JDT": "JDT", "UYT": "UYT", "WEZ": "WEZ", "ACWST": "ACWST", "HADT": "HADT", "WIB": "WIB", "∅∅∅": "∅∅∅", "CAT": "CAT", "AST": "AST", "CHADT": "CHADT", "AKST": "AKST", "LHST": "LHST", "OESZ": "OESZ", "ART": "ART", "PDT": "PDT", "AWDT": "AWDT", "MYT": "MYT", "UYST": "UYST", "WESZ": "WESZ", "WART": "WART", "MDT": "MDT", "AWST": "AWST", "JST": "JST", "WARST": "WARST", "EAT": "EAT"},
  69. }
  70. }
  71. // Locale returns the current translators string locale
  72. func (sah *sah) Locale() string {
  73. return sah.locale
  74. }
  75. // PluralsCardinal returns the list of cardinal plural rules associated with 'sah'
  76. func (sah *sah) PluralsCardinal() []locales.PluralRule {
  77. return sah.pluralsCardinal
  78. }
  79. // PluralsOrdinal returns the list of ordinal plural rules associated with 'sah'
  80. func (sah *sah) PluralsOrdinal() []locales.PluralRule {
  81. return sah.pluralsOrdinal
  82. }
  83. // PluralsRange returns the list of range plural rules associated with 'sah'
  84. func (sah *sah) PluralsRange() []locales.PluralRule {
  85. return sah.pluralsRange
  86. }
  87. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'sah'
  88. func (sah *sah) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  89. return locales.PluralRuleOther
  90. }
  91. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'sah'
  92. func (sah *sah) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  93. return locales.PluralRuleUnknown
  94. }
  95. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'sah'
  96. func (sah *sah) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  97. return locales.PluralRuleUnknown
  98. }
  99. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  100. func (sah *sah) MonthAbbreviated(month time.Month) string {
  101. return sah.monthsAbbreviated[month]
  102. }
  103. // MonthsAbbreviated returns the locales abbreviated months
  104. func (sah *sah) MonthsAbbreviated() []string {
  105. return sah.monthsAbbreviated[1:]
  106. }
  107. // MonthNarrow returns the locales narrow month given the 'month' provided
  108. func (sah *sah) MonthNarrow(month time.Month) string {
  109. return sah.monthsNarrow[month]
  110. }
  111. // MonthsNarrow returns the locales narrow months
  112. func (sah *sah) MonthsNarrow() []string {
  113. return sah.monthsNarrow[1:]
  114. }
  115. // MonthWide returns the locales wide month given the 'month' provided
  116. func (sah *sah) MonthWide(month time.Month) string {
  117. return sah.monthsWide[month]
  118. }
  119. // MonthsWide returns the locales wide months
  120. func (sah *sah) MonthsWide() []string {
  121. return sah.monthsWide[1:]
  122. }
  123. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  124. func (sah *sah) WeekdayAbbreviated(weekday time.Weekday) string {
  125. return sah.daysAbbreviated[weekday]
  126. }
  127. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  128. func (sah *sah) WeekdaysAbbreviated() []string {
  129. return sah.daysAbbreviated
  130. }
  131. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  132. func (sah *sah) WeekdayNarrow(weekday time.Weekday) string {
  133. return sah.daysNarrow[weekday]
  134. }
  135. // WeekdaysNarrow returns the locales narrow weekdays
  136. func (sah *sah) WeekdaysNarrow() []string {
  137. return sah.daysNarrow
  138. }
  139. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  140. func (sah *sah) WeekdayShort(weekday time.Weekday) string {
  141. return sah.daysShort[weekday]
  142. }
  143. // WeekdaysShort returns the locales short weekdays
  144. func (sah *sah) WeekdaysShort() []string {
  145. return sah.daysShort
  146. }
  147. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  148. func (sah *sah) WeekdayWide(weekday time.Weekday) string {
  149. return sah.daysWide[weekday]
  150. }
  151. // WeekdaysWide returns the locales wide weekdays
  152. func (sah *sah) WeekdaysWide() []string {
  153. return sah.daysWide
  154. }
  155. // FmtNumber returns 'num' with digits/precision of 'v' for 'sah' and handles both Whole and Real numbers based on 'v'
  156. func (sah *sah) FmtNumber(num float64, v uint64) string {
  157. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  158. }
  159. // FmtPercent returns 'num' with digits/precision of 'v' for 'sah' and handles both Whole and Real numbers based on 'v'
  160. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  161. func (sah *sah) FmtPercent(num float64, v uint64) string {
  162. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. }
  164. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'sah'
  165. func (sah *sah) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  166. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  167. symbol := sah.currencies[currency]
  168. l := len(s) + len(symbol) + 4
  169. b := make([]byte, 0, l)
  170. for i := len(s) - 1; i >= 0; i-- {
  171. if s[i] == '.' {
  172. b = append(b, sah.decimal[0])
  173. continue
  174. }
  175. b = append(b, s[i])
  176. }
  177. for j := len(symbol) - 1; j >= 0; j-- {
  178. b = append(b, symbol[j])
  179. }
  180. for j := len(sah.currencyPositivePrefix) - 1; j >= 0; j-- {
  181. b = append(b, sah.currencyPositivePrefix[j])
  182. }
  183. if num < 0 {
  184. b = append(b, sah.minus[0])
  185. }
  186. // reverse
  187. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  188. b[i], b[j] = b[j], b[i]
  189. }
  190. b = append(b, sah.currencyPositiveSuffix...)
  191. return string(b)
  192. }
  193. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'sah'
  194. // in accounting notation.
  195. func (sah *sah) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  196. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  197. symbol := sah.currencies[currency]
  198. l := len(s) + len(symbol) + 4
  199. b := make([]byte, 0, l)
  200. for i := len(s) - 1; i >= 0; i-- {
  201. if s[i] == '.' {
  202. b = append(b, sah.decimal[0])
  203. continue
  204. }
  205. b = append(b, s[i])
  206. }
  207. if num < 0 {
  208. for j := len(symbol) - 1; j >= 0; j-- {
  209. b = append(b, symbol[j])
  210. }
  211. for j := len(sah.currencyNegativePrefix) - 1; j >= 0; j-- {
  212. b = append(b, sah.currencyNegativePrefix[j])
  213. }
  214. b = append(b, sah.minus[0])
  215. } else {
  216. for j := len(symbol) - 1; j >= 0; j-- {
  217. b = append(b, symbol[j])
  218. }
  219. for j := len(sah.currencyPositivePrefix) - 1; j >= 0; j-- {
  220. b = append(b, sah.currencyPositivePrefix[j])
  221. }
  222. }
  223. // reverse
  224. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  225. b[i], b[j] = b[j], b[i]
  226. }
  227. if num < 0 {
  228. b = append(b, sah.currencyNegativeSuffix...)
  229. } else {
  230. b = append(b, sah.currencyPositiveSuffix...)
  231. }
  232. return string(b)
  233. }
  234. // FmtDateShort returns the short date representation of 't' for 'sah'
  235. func (sah *sah) FmtDateShort(t time.Time) string {
  236. b := make([]byte, 0, 32)
  237. if t.Year() > 9 {
  238. b = append(b, strconv.Itoa(t.Year())[2:]...)
  239. } else {
  240. b = append(b, strconv.Itoa(t.Year())[1:]...)
  241. }
  242. b = append(b, []byte{0x2f}...)
  243. b = strconv.AppendInt(b, int64(t.Month()), 10)
  244. b = append(b, []byte{0x2f}...)
  245. b = strconv.AppendInt(b, int64(t.Day()), 10)
  246. return string(b)
  247. }
  248. // FmtDateMedium returns the medium date representation of 't' for 'sah'
  249. func (sah *sah) FmtDateMedium(t time.Time) string {
  250. b := make([]byte, 0, 32)
  251. b = strconv.AppendInt(b, int64(t.Year()), 10)
  252. b = append(b, []byte{0x2c, 0x20}...)
  253. b = append(b, sah.monthsAbbreviated[t.Month()]...)
  254. b = append(b, []byte{0x20}...)
  255. b = strconv.AppendInt(b, int64(t.Day()), 10)
  256. return string(b)
  257. }
  258. // FmtDateLong returns the long date representation of 't' for 'sah'
  259. func (sah *sah) FmtDateLong(t time.Time) string {
  260. b := make([]byte, 0, 32)
  261. b = strconv.AppendInt(b, int64(t.Year()), 10)
  262. b = append(b, []byte{0x2c, 0x20}...)
  263. b = append(b, sah.monthsWide[t.Month()]...)
  264. b = append(b, []byte{0x20}...)
  265. b = strconv.AppendInt(b, int64(t.Day()), 10)
  266. return string(b)
  267. }
  268. // FmtDateFull returns the full date representation of 't' for 'sah'
  269. func (sah *sah) FmtDateFull(t time.Time) string {
  270. b := make([]byte, 0, 32)
  271. b = strconv.AppendInt(b, int64(t.Year()), 10)
  272. b = append(b, []byte{}...)
  273. b = append(b, []byte{0x27, 0xd1, 0x81, 0xd1, 0x8b, 0xd0, 0xbb, 0x27, 0x20}...)
  274. b = append(b, sah.monthsWide[t.Month()]...)
  275. b = append(b, []byte{0x20}...)
  276. b = strconv.AppendInt(b, int64(t.Day()), 10)
  277. b = append(b, []byte{}...)
  278. b = append(b, []byte{0x27, 0xd0, 0xba, 0xd2, 0xaf, 0xd0, 0xbd, 0xd1, 0x8d, 0x27, 0x2c, 0x20}...)
  279. b = append(b, sah.daysWide[t.Weekday()]...)
  280. return string(b)
  281. }
  282. // FmtTimeShort returns the short time representation of 't' for 'sah'
  283. func (sah *sah) FmtTimeShort(t time.Time) string {
  284. b := make([]byte, 0, 32)
  285. return string(b)
  286. }
  287. // FmtTimeMedium returns the medium time representation of 't' for 'sah'
  288. func (sah *sah) FmtTimeMedium(t time.Time) string {
  289. b := make([]byte, 0, 32)
  290. if t.Hour() < 10 {
  291. b = append(b, '0')
  292. }
  293. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  294. b = append(b, sah.timeSeparator...)
  295. if t.Minute() < 10 {
  296. b = append(b, '0')
  297. }
  298. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  299. b = append(b, sah.timeSeparator...)
  300. if t.Second() < 10 {
  301. b = append(b, '0')
  302. }
  303. b = strconv.AppendInt(b, int64(t.Second()), 10)
  304. return string(b)
  305. }
  306. // FmtTimeLong returns the long time representation of 't' for 'sah'
  307. func (sah *sah) FmtTimeLong(t time.Time) string {
  308. b := make([]byte, 0, 32)
  309. if t.Hour() < 10 {
  310. b = append(b, '0')
  311. }
  312. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  313. b = append(b, sah.timeSeparator...)
  314. if t.Minute() < 10 {
  315. b = append(b, '0')
  316. }
  317. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  318. b = append(b, sah.timeSeparator...)
  319. if t.Second() < 10 {
  320. b = append(b, '0')
  321. }
  322. b = strconv.AppendInt(b, int64(t.Second()), 10)
  323. b = append(b, []byte{0x20}...)
  324. tz, _ := t.Zone()
  325. b = append(b, tz...)
  326. return string(b)
  327. }
  328. // FmtTimeFull returns the full time representation of 't' for 'sah'
  329. func (sah *sah) FmtTimeFull(t time.Time) string {
  330. b := make([]byte, 0, 32)
  331. return string(b)
  332. }