yi.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package yi
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type yi 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 'yi' locale
  43. func New() locales.Translator {
  44. return &yi{
  45. locale: "yi",
  46. pluralsCardinal: []locales.PluralRule{2, 6},
  47. pluralsOrdinal: nil,
  48. pluralsRange: nil,
  49. decimal: ".",
  50. group: ",",
  51. minus: "-",
  52. percent: "%",
  53. perMille: "‰",
  54. timeSeparator: ":",
  55. inifinity: "∞",
  56. 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 ", "€", "FIM ", "FJD ", "FKP ", "FRF ", "£", "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 ", "₹", "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 ", "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 ", "$", "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 "},
  57. currencyPositivePrefix: " ",
  58. currencyPositiveSuffix: "K",
  59. currencyNegativePrefix: " ",
  60. currencyNegativeSuffix: "K",
  61. monthsAbbreviated: []string{"", "יאַנואַר", "פֿעברואַר", "מערץ", "אַפּריל", "מיי", "יוני", "יולי", "אויגוסט", "סעפּטעמבער", "אקטאבער", "נאוועמבער", "דעצעמבער"},
  62. monthsWide: []string{"", "יאַנואַר", "פֿעברואַר", "מערץ", "אַפּריל", "מיי", "יוני", "יולי", "אויגוסט", "סעפּטעמבער", "אקטאבער", "נאוועמבער", "דעצעמבער"},
  63. daysAbbreviated: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
  64. daysShort: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
  65. daysWide: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
  66. periodsAbbreviated: []string{"פֿאַרמיטאָג", "נאָכמיטאָג"},
  67. periodsWide: []string{"פֿאַרמיטאָג", "נאָכמיטאָג"},
  68. timezones: map[string]string{"ADT": "ADT", "SGT": "SGT", "WAST": "WAST", "HKT": "HKT", "IST": "IST", "OEZ": "OEZ", "WEZ": "WEZ", "WIB": "WIB", "HADT": "HADT", "SAST": "SAST", "AKDT": "AKDT", "PDT": "PDT", "LHST": "LHST", "NZDT": "NZDT", "ChST": "ChST", "GFT": "GFT", "WITA": "WITA", "MDT": "MDT", "GYT": "GYT", "VET": "VET", "AST": "AST", "HAST": "HAST", "HKST": "HKST", "WIT": "WIT", "AWDT": "AWDT", "CHADT": "CHADT", "WART": "WART", "GMT": "GMT", "HNT": "HNT", "TMST": "TMST", "CLST": "CLST", "AEDT": "AEDT", "ARST": "ARST", "JDT": "JDT", "COST": "COST", "ART": "ART", "BOT": "BOT", "UYT": "UYT", "UYST": "UYST", "ECT": "ECT", "∅∅∅": "∅∅∅", "HAT": "HAT", "MST": "MST", "AWST": "AWST", "MYT": "MYT", "TMT": "TMT", "MESZ": "MESZ", "CLT": "CLT", "EST": "EST", "BT": "BT", "AKST": "AKST", "CDT": "CDT", "WARST": "WARST", "OESZ": "OESZ", "ACST": "ACST", "WAT": "WAT", "MEZ": "MEZ", "NZST": "NZST", "ACWDT": "ACWDT", "AEST": "AEST", "CHAST": "CHAST", "EDT": "EDT", "JST": "JST", "WESZ": "WESZ", "CAT": "CAT", "EAT": "EAT", "ACDT": "ACDT", "SRT": "SRT", "PST": "PST", "LHDT": "LHDT", "CST": "CST", "ACWST": "ACWST", "COT": "COT"},
  69. }
  70. }
  71. // Locale returns the current translators string locale
  72. func (yi *yi) Locale() string {
  73. return yi.locale
  74. }
  75. // PluralsCardinal returns the list of cardinal plural rules associated with 'yi'
  76. func (yi *yi) PluralsCardinal() []locales.PluralRule {
  77. return yi.pluralsCardinal
  78. }
  79. // PluralsOrdinal returns the list of ordinal plural rules associated with 'yi'
  80. func (yi *yi) PluralsOrdinal() []locales.PluralRule {
  81. return yi.pluralsOrdinal
  82. }
  83. // PluralsRange returns the list of range plural rules associated with 'yi'
  84. func (yi *yi) PluralsRange() []locales.PluralRule {
  85. return yi.pluralsRange
  86. }
  87. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'yi'
  88. func (yi *yi) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  89. n := math.Abs(num)
  90. i := int64(n)
  91. if i == 1 && v == 0 {
  92. return locales.PluralRuleOne
  93. }
  94. return locales.PluralRuleOther
  95. }
  96. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'yi'
  97. func (yi *yi) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  98. return locales.PluralRuleUnknown
  99. }
  100. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'yi'
  101. func (yi *yi) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  102. return locales.PluralRuleUnknown
  103. }
  104. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  105. func (yi *yi) MonthAbbreviated(month time.Month) string {
  106. return yi.monthsAbbreviated[month]
  107. }
  108. // MonthsAbbreviated returns the locales abbreviated months
  109. func (yi *yi) MonthsAbbreviated() []string {
  110. return yi.monthsAbbreviated[1:]
  111. }
  112. // MonthNarrow returns the locales narrow month given the 'month' provided
  113. func (yi *yi) MonthNarrow(month time.Month) string {
  114. return yi.monthsNarrow[month]
  115. }
  116. // MonthsNarrow returns the locales narrow months
  117. func (yi *yi) MonthsNarrow() []string {
  118. return yi.monthsNarrow[1:]
  119. }
  120. // MonthWide returns the locales wide month given the 'month' provided
  121. func (yi *yi) MonthWide(month time.Month) string {
  122. return yi.monthsWide[month]
  123. }
  124. // MonthsWide returns the locales wide months
  125. func (yi *yi) MonthsWide() []string {
  126. return yi.monthsWide[1:]
  127. }
  128. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  129. func (yi *yi) WeekdayAbbreviated(weekday time.Weekday) string {
  130. return yi.daysAbbreviated[weekday]
  131. }
  132. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  133. func (yi *yi) WeekdaysAbbreviated() []string {
  134. return yi.daysAbbreviated
  135. }
  136. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  137. func (yi *yi) WeekdayNarrow(weekday time.Weekday) string {
  138. return yi.daysNarrow[weekday]
  139. }
  140. // WeekdaysNarrow returns the locales narrow weekdays
  141. func (yi *yi) WeekdaysNarrow() []string {
  142. return yi.daysNarrow
  143. }
  144. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  145. func (yi *yi) WeekdayShort(weekday time.Weekday) string {
  146. return yi.daysShort[weekday]
  147. }
  148. // WeekdaysShort returns the locales short weekdays
  149. func (yi *yi) WeekdaysShort() []string {
  150. return yi.daysShort
  151. }
  152. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  153. func (yi *yi) WeekdayWide(weekday time.Weekday) string {
  154. return yi.daysWide[weekday]
  155. }
  156. // WeekdaysWide returns the locales wide weekdays
  157. func (yi *yi) WeekdaysWide() []string {
  158. return yi.daysWide
  159. }
  160. // FmtNumber returns 'num' with digits/precision of 'v' for 'yi' and handles both Whole and Real numbers based on 'v'
  161. func (yi *yi) FmtNumber(num float64, v uint64) string {
  162. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. }
  164. // FmtPercent returns 'num' with digits/precision of 'v' for 'yi' and handles both Whole and Real numbers based on 'v'
  165. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  166. func (yi *yi) FmtPercent(num float64, v uint64) string {
  167. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  168. }
  169. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'yi'
  170. func (yi *yi) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  171. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  172. symbol := yi.currencies[currency]
  173. l := len(s) + len(symbol) + 5
  174. b := make([]byte, 0, l)
  175. for i := len(s) - 1; i >= 0; i-- {
  176. if s[i] == '.' {
  177. b = append(b, yi.decimal[0])
  178. continue
  179. }
  180. b = append(b, s[i])
  181. }
  182. for j := len(symbol) - 1; j >= 0; j-- {
  183. b = append(b, symbol[j])
  184. }
  185. for j := len(yi.currencyPositivePrefix) - 1; j >= 0; j-- {
  186. b = append(b, yi.currencyPositivePrefix[j])
  187. }
  188. if num < 0 {
  189. b = append(b, yi.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. b = append(b, yi.currencyPositiveSuffix...)
  196. return string(b)
  197. }
  198. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'yi'
  199. // in accounting notation.
  200. func (yi *yi) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  201. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  202. symbol := yi.currencies[currency]
  203. l := len(s) + len(symbol) + 5
  204. b := make([]byte, 0, l)
  205. for i := len(s) - 1; i >= 0; i-- {
  206. if s[i] == '.' {
  207. b = append(b, yi.decimal[0])
  208. continue
  209. }
  210. b = append(b, s[i])
  211. }
  212. if num < 0 {
  213. for j := len(symbol) - 1; j >= 0; j-- {
  214. b = append(b, symbol[j])
  215. }
  216. for j := len(yi.currencyNegativePrefix) - 1; j >= 0; j-- {
  217. b = append(b, yi.currencyNegativePrefix[j])
  218. }
  219. b = append(b, yi.minus[0])
  220. } else {
  221. for j := len(symbol) - 1; j >= 0; j-- {
  222. b = append(b, symbol[j])
  223. }
  224. for j := len(yi.currencyPositivePrefix) - 1; j >= 0; j-- {
  225. b = append(b, yi.currencyPositivePrefix[j])
  226. }
  227. }
  228. // reverse
  229. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  230. b[i], b[j] = b[j], b[i]
  231. }
  232. if num < 0 {
  233. b = append(b, yi.currencyNegativeSuffix...)
  234. } else {
  235. b = append(b, yi.currencyPositiveSuffix...)
  236. }
  237. return string(b)
  238. }
  239. // FmtDateShort returns the short date representation of 't' for 'yi'
  240. func (yi *yi) FmtDateShort(t time.Time) string {
  241. b := make([]byte, 0, 32)
  242. if t.Day() < 10 {
  243. b = append(b, '0')
  244. }
  245. b = strconv.AppendInt(b, int64(t.Day()), 10)
  246. b = append(b, []byte{0x2f}...)
  247. if t.Month() < 10 {
  248. b = append(b, '0')
  249. }
  250. b = strconv.AppendInt(b, int64(t.Month()), 10)
  251. b = append(b, []byte{0x2f}...)
  252. if t.Year() > 9 {
  253. b = append(b, strconv.Itoa(t.Year())[2:]...)
  254. } else {
  255. b = append(b, strconv.Itoa(t.Year())[1:]...)
  256. }
  257. return string(b)
  258. }
  259. // FmtDateMedium returns the medium date representation of 't' for 'yi'
  260. func (yi *yi) FmtDateMedium(t time.Time) string {
  261. b := make([]byte, 0, 32)
  262. b = strconv.AppendInt(b, int64(t.Day()), 10)
  263. b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
  264. b = append(b, yi.monthsAbbreviated[t.Month()]...)
  265. b = append(b, []byte{0x20}...)
  266. b = strconv.AppendInt(b, int64(t.Year()), 10)
  267. return string(b)
  268. }
  269. // FmtDateLong returns the long date representation of 't' for 'yi'
  270. func (yi *yi) FmtDateLong(t time.Time) string {
  271. b := make([]byte, 0, 32)
  272. b = strconv.AppendInt(b, int64(t.Day()), 10)
  273. b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
  274. b = append(b, yi.monthsWide[t.Month()]...)
  275. b = append(b, []byte{0x20}...)
  276. b = strconv.AppendInt(b, int64(t.Year()), 10)
  277. return string(b)
  278. }
  279. // FmtDateFull returns the full date representation of 't' for 'yi'
  280. func (yi *yi) FmtDateFull(t time.Time) string {
  281. b := make([]byte, 0, 32)
  282. b = append(b, yi.daysWide[t.Weekday()]...)
  283. b = append(b, []byte{0x2c, 0x20}...)
  284. b = strconv.AppendInt(b, int64(t.Day()), 10)
  285. b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
  286. b = append(b, yi.monthsWide[t.Month()]...)
  287. b = append(b, []byte{0x20}...)
  288. b = strconv.AppendInt(b, int64(t.Year()), 10)
  289. return string(b)
  290. }
  291. // FmtTimeShort returns the short time representation of 't' for 'yi'
  292. func (yi *yi) FmtTimeShort(t time.Time) string {
  293. b := make([]byte, 0, 32)
  294. if t.Hour() < 10 {
  295. b = append(b, '0')
  296. }
  297. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  298. b = append(b, yi.timeSeparator...)
  299. if t.Minute() < 10 {
  300. b = append(b, '0')
  301. }
  302. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  303. return string(b)
  304. }
  305. // FmtTimeMedium returns the medium time representation of 't' for 'yi'
  306. func (yi *yi) FmtTimeMedium(t time.Time) string {
  307. b := make([]byte, 0, 32)
  308. if t.Hour() < 10 {
  309. b = append(b, '0')
  310. }
  311. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  312. b = append(b, yi.timeSeparator...)
  313. if t.Minute() < 10 {
  314. b = append(b, '0')
  315. }
  316. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  317. b = append(b, yi.timeSeparator...)
  318. if t.Second() < 10 {
  319. b = append(b, '0')
  320. }
  321. b = strconv.AppendInt(b, int64(t.Second()), 10)
  322. return string(b)
  323. }
  324. // FmtTimeLong returns the long time representation of 't' for 'yi'
  325. func (yi *yi) FmtTimeLong(t time.Time) string {
  326. b := make([]byte, 0, 32)
  327. if t.Hour() < 10 {
  328. b = append(b, '0')
  329. }
  330. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  331. b = append(b, yi.timeSeparator...)
  332. if t.Minute() < 10 {
  333. b = append(b, '0')
  334. }
  335. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  336. b = append(b, yi.timeSeparator...)
  337. if t.Second() < 10 {
  338. b = append(b, '0')
  339. }
  340. b = strconv.AppendInt(b, int64(t.Second()), 10)
  341. b = append(b, []byte{0x20}...)
  342. tz, _ := t.Zone()
  343. b = append(b, tz...)
  344. return string(b)
  345. }
  346. // FmtTimeFull returns the full time representation of 't' for 'yi'
  347. func (yi *yi) FmtTimeFull(t time.Time) string {
  348. b := make([]byte, 0, 32)
  349. if t.Hour() < 10 {
  350. b = append(b, '0')
  351. }
  352. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  353. b = append(b, yi.timeSeparator...)
  354. if t.Minute() < 10 {
  355. b = append(b, '0')
  356. }
  357. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  358. b = append(b, yi.timeSeparator...)
  359. if t.Second() < 10 {
  360. b = append(b, '0')
  361. }
  362. b = strconv.AppendInt(b, int64(t.Second()), 10)
  363. b = append(b, []byte{0x20}...)
  364. tz, _ := t.Zone()
  365. if btz, ok := yi.timezones[tz]; ok {
  366. b = append(b, btz...)
  367. } else {
  368. b = append(b, tz...)
  369. }
  370. return string(b)
  371. }