1 package yi
2
3 import (
4 "math"
5 "strconv"
6 "time"
7
8 "github.com/go-playground/locales"
9 "github.com/go-playground/locales/currency"
10 )
11
12 type yi struct {
13 locale string
14 pluralsCardinal []locales.PluralRule
15 pluralsOrdinal []locales.PluralRule
16 pluralsRange []locales.PluralRule
17 decimal string
18 group string
19 minus string
20 percent string
21 perMille string
22 timeSeparator string
23 inifinity string
24 currencies []string
25 currencyPositivePrefix string
26 currencyPositiveSuffix string
27 currencyNegativePrefix string
28 currencyNegativeSuffix string
29 monthsAbbreviated []string
30 monthsNarrow []string
31 monthsWide []string
32 daysAbbreviated []string
33 daysNarrow []string
34 daysShort []string
35 daysWide []string
36 periodsAbbreviated []string
37 periodsNarrow []string
38 periodsShort []string
39 periodsWide []string
40 erasAbbreviated []string
41 erasNarrow []string
42 erasWide []string
43 timezones map[string]string
44 }
45
46
47 func New() locales.Translator {
48 return &yi{
49 locale: "yi",
50 pluralsCardinal: []locales.PluralRule{2, 6},
51 pluralsOrdinal: nil,
52 pluralsRange: nil,
53 decimal: ".",
54 group: ",",
55 minus: "-",
56 percent: "%",
57 perMille: "‰",
58 timeSeparator: ":",
59 inifinity: "∞",
60 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", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "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", "MRU", "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", "STN", "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", "UYW", "UZS", "VEB", "VEF", "VES", "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"},
61 currencyPositivePrefix: " ",
62 currencyPositiveSuffix: "K",
63 currencyNegativePrefix: " ",
64 currencyNegativeSuffix: "K",
65 monthsAbbreviated: []string{"", "יאַנואַר", "פֿעברואַר", "מערץ", "אַפּריל", "מיי", "יוני", "יולי", "אויגוסט", "סעפּטעמבער", "אקטאבער", "נאוועמבער", "דעצעמבער"},
66 monthsWide: []string{"", "יאַנואַר", "פֿעברואַר", "מערץ", "אַפּריל", "מיי", "יוני", "יולי", "אויגוסט", "סעפּטעמבער", "אקטאבער", "נאוועמבער", "דעצעמבער"},
67 daysAbbreviated: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
68 daysShort: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
69 daysWide: []string{"זונטיק", "מאָנטיק", "דינסטיק", "מיטוואך", "דאנערשטיק", "פֿרײַטיק", "שבת"},
70 periodsAbbreviated: []string{"פֿאַרמיטאָג", "נאָכמיטאָג"},
71 periodsWide: []string{"פֿאַרמיטאָג", "נאָכמיטאָג"},
72 timezones: map[string]string{"ACDT": "ACDT", "ACST": "ACST", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "ADT", "AEDT": "AEDT", "AEST": "AEST", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "AST", "AWDT": "AWDT", "AWST": "AWST", "BOT": "BOT", "BT": "BT", "CAT": "CAT", "CDT": "CDT", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "CST": "CST", "ChST": "ChST", "EAT": "EAT", "ECT": "ECT", "EDT": "EDT", "EST": "EST", "GFT": "GFT", "GMT": "GMT", "GST": "GST", "GYT": "GYT", "HADT": "HADT", "HAST": "HAST", "HAT": "HAT", "HECU": "HECU", "HEEG": "HEEG", "HENOMX": "HENOMX", "HEOG": "HEOG", "HEPM": "HEPM", "HEPMX": "HEPMX", "HKST": "HKST", "HKT": "HKT", "HNCU": "HNCU", "HNEG": "HNEG", "HNNOMX": "HNNOMX", "HNOG": "HNOG", "HNPM": "HNPM", "HNPMX": "HNPMX", "HNT": "HNT", "IST": "IST", "JDT": "JDT", "JST": "JST", "LHDT": "LHDT", "LHST": "LHST", "MDT": "MDT", "MESZ": "MESZ", "MEZ": "MEZ", "MST": "MST", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "OESZ", "OEZ": "OEZ", "PDT": "PDT", "PST": "PST", "SAST": "SAST", "SGT": "SGT", "SRT": "SRT", "TMST": "TMST", "TMT": "TMT", "UYST": "UYST", "UYT": "UYT", "VET": "VET", "WARST": "WARST", "WART": "WART", "WAST": "WAST", "WAT": "WAT", "WESZ": "WESZ", "WEZ": "WEZ", "WIB": "WIB", "WIT": "WIT", "WITA": "WITA", "∅∅∅": "∅∅∅"},
73 }
74 }
75
76
77 func (yi *yi) Locale() string {
78 return yi.locale
79 }
80
81
82 func (yi *yi) PluralsCardinal() []locales.PluralRule {
83 return yi.pluralsCardinal
84 }
85
86
87 func (yi *yi) PluralsOrdinal() []locales.PluralRule {
88 return yi.pluralsOrdinal
89 }
90
91
92 func (yi *yi) PluralsRange() []locales.PluralRule {
93 return yi.pluralsRange
94 }
95
96
97 func (yi *yi) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
98
99 n := math.Abs(num)
100 i := int64(n)
101
102 if i == 1 && v == 0 {
103 return locales.PluralRuleOne
104 }
105
106 return locales.PluralRuleOther
107 }
108
109
110 func (yi *yi) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
111 return locales.PluralRuleUnknown
112 }
113
114
115 func (yi *yi) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
116 return locales.PluralRuleUnknown
117 }
118
119
120 func (yi *yi) MonthAbbreviated(month time.Month) string {
121 return yi.monthsAbbreviated[month]
122 }
123
124
125 func (yi *yi) MonthsAbbreviated() []string {
126 return yi.monthsAbbreviated[1:]
127 }
128
129
130 func (yi *yi) MonthNarrow(month time.Month) string {
131 return yi.monthsNarrow[month]
132 }
133
134
135 func (yi *yi) MonthsNarrow() []string {
136 return nil
137 }
138
139
140 func (yi *yi) MonthWide(month time.Month) string {
141 return yi.monthsWide[month]
142 }
143
144
145 func (yi *yi) MonthsWide() []string {
146 return yi.monthsWide[1:]
147 }
148
149
150 func (yi *yi) WeekdayAbbreviated(weekday time.Weekday) string {
151 return yi.daysAbbreviated[weekday]
152 }
153
154
155 func (yi *yi) WeekdaysAbbreviated() []string {
156 return yi.daysAbbreviated
157 }
158
159
160 func (yi *yi) WeekdayNarrow(weekday time.Weekday) string {
161 return yi.daysNarrow[weekday]
162 }
163
164
165 func (yi *yi) WeekdaysNarrow() []string {
166 return yi.daysNarrow
167 }
168
169
170 func (yi *yi) WeekdayShort(weekday time.Weekday) string {
171 return yi.daysShort[weekday]
172 }
173
174
175 func (yi *yi) WeekdaysShort() []string {
176 return yi.daysShort
177 }
178
179
180 func (yi *yi) WeekdayWide(weekday time.Weekday) string {
181 return yi.daysWide[weekday]
182 }
183
184
185 func (yi *yi) WeekdaysWide() []string {
186 return yi.daysWide
187 }
188
189
190 func (yi *yi) Decimal() string {
191 return yi.decimal
192 }
193
194
195 func (yi *yi) Group() string {
196 return yi.group
197 }
198
199
200 func (yi *yi) Minus() string {
201 return yi.minus
202 }
203
204
205 func (yi *yi) FmtNumber(num float64, v uint64) string {
206
207 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
208 }
209
210
211
212 func (yi *yi) FmtPercent(num float64, v uint64) string {
213 return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
214 }
215
216
217 func (yi *yi) FmtCurrency(num float64, v uint64, currency currency.Type) string {
218
219 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
220 symbol := yi.currencies[currency]
221 l := len(s) + len(symbol) + 5
222
223 b := make([]byte, 0, l)
224
225 for i := len(s) - 1; i >= 0; i-- {
226
227 if s[i] == '.' {
228 b = append(b, yi.decimal[0])
229 continue
230 }
231
232 b = append(b, s[i])
233 }
234
235 for j := len(symbol) - 1; j >= 0; j-- {
236 b = append(b, symbol[j])
237 }
238
239 for j := len(yi.currencyPositivePrefix) - 1; j >= 0; j-- {
240 b = append(b, yi.currencyPositivePrefix[j])
241 }
242
243 if num < 0 {
244 b = append(b, yi.minus[0])
245 }
246
247
248 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
249 b[i], b[j] = b[j], b[i]
250 }
251
252 b = append(b, yi.currencyPositiveSuffix...)
253
254 return string(b)
255 }
256
257
258
259 func (yi *yi) FmtAccounting(num float64, v uint64, currency currency.Type) string {
260
261 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
262 symbol := yi.currencies[currency]
263 l := len(s) + len(symbol) + 5
264
265 b := make([]byte, 0, l)
266
267 for i := len(s) - 1; i >= 0; i-- {
268
269 if s[i] == '.' {
270 b = append(b, yi.decimal[0])
271 continue
272 }
273
274 b = append(b, s[i])
275 }
276
277 if num < 0 {
278
279 for j := len(symbol) - 1; j >= 0; j-- {
280 b = append(b, symbol[j])
281 }
282
283 for j := len(yi.currencyNegativePrefix) - 1; j >= 0; j-- {
284 b = append(b, yi.currencyNegativePrefix[j])
285 }
286
287 b = append(b, yi.minus[0])
288
289 } else {
290
291 for j := len(symbol) - 1; j >= 0; j-- {
292 b = append(b, symbol[j])
293 }
294
295 for j := len(yi.currencyPositivePrefix) - 1; j >= 0; j-- {
296 b = append(b, yi.currencyPositivePrefix[j])
297 }
298
299 }
300
301
302 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
303 b[i], b[j] = b[j], b[i]
304 }
305
306 if num < 0 {
307 b = append(b, yi.currencyNegativeSuffix...)
308 } else {
309
310 b = append(b, yi.currencyPositiveSuffix...)
311 }
312
313 return string(b)
314 }
315
316
317 func (yi *yi) FmtDateShort(t time.Time) string {
318
319 b := make([]byte, 0, 32)
320
321 if t.Day() < 10 {
322 b = append(b, '0')
323 }
324
325 b = strconv.AppendInt(b, int64(t.Day()), 10)
326 b = append(b, []byte{0x2f}...)
327
328 if t.Month() < 10 {
329 b = append(b, '0')
330 }
331
332 b = strconv.AppendInt(b, int64(t.Month()), 10)
333
334 b = append(b, []byte{0x2f}...)
335
336 if t.Year() > 9 {
337 b = append(b, strconv.Itoa(t.Year())[2:]...)
338 } else {
339 b = append(b, strconv.Itoa(t.Year())[1:]...)
340 }
341
342 return string(b)
343 }
344
345
346 func (yi *yi) FmtDateMedium(t time.Time) string {
347
348 b := make([]byte, 0, 32)
349
350 b = strconv.AppendInt(b, int64(t.Day()), 10)
351 b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
352 b = append(b, yi.monthsAbbreviated[t.Month()]...)
353 b = append(b, []byte{0x20}...)
354
355 if t.Year() > 0 {
356 b = strconv.AppendInt(b, int64(t.Year()), 10)
357 } else {
358 b = strconv.AppendInt(b, int64(-t.Year()), 10)
359 }
360
361 return string(b)
362 }
363
364
365 func (yi *yi) FmtDateLong(t time.Time) string {
366
367 b := make([]byte, 0, 32)
368
369 b = strconv.AppendInt(b, int64(t.Day()), 10)
370 b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
371 b = append(b, yi.monthsWide[t.Month()]...)
372 b = append(b, []byte{0x20}...)
373
374 if t.Year() > 0 {
375 b = strconv.AppendInt(b, int64(t.Year()), 10)
376 } else {
377 b = strconv.AppendInt(b, int64(-t.Year()), 10)
378 }
379
380 return string(b)
381 }
382
383
384 func (yi *yi) FmtDateFull(t time.Time) string {
385
386 b := make([]byte, 0, 32)
387
388 b = append(b, yi.daysWide[t.Weekday()]...)
389 b = append(b, []byte{0x2c, 0x20}...)
390 b = strconv.AppendInt(b, int64(t.Day()), 10)
391 b = append(b, []byte{0xd7, 0x98, 0xd7, 0x9f, 0x20}...)
392 b = append(b, yi.monthsWide[t.Month()]...)
393 b = append(b, []byte{0x20}...)
394
395 if t.Year() > 0 {
396 b = strconv.AppendInt(b, int64(t.Year()), 10)
397 } else {
398 b = strconv.AppendInt(b, int64(-t.Year()), 10)
399 }
400
401 return string(b)
402 }
403
404
405 func (yi *yi) FmtTimeShort(t time.Time) string {
406
407 b := make([]byte, 0, 32)
408
409 if t.Hour() < 10 {
410 b = append(b, '0')
411 }
412
413 b = strconv.AppendInt(b, int64(t.Hour()), 10)
414 b = append(b, yi.timeSeparator...)
415
416 if t.Minute() < 10 {
417 b = append(b, '0')
418 }
419
420 b = strconv.AppendInt(b, int64(t.Minute()), 10)
421
422 return string(b)
423 }
424
425
426 func (yi *yi) FmtTimeMedium(t time.Time) string {
427
428 b := make([]byte, 0, 32)
429
430 if t.Hour() < 10 {
431 b = append(b, '0')
432 }
433
434 b = strconv.AppendInt(b, int64(t.Hour()), 10)
435 b = append(b, yi.timeSeparator...)
436
437 if t.Minute() < 10 {
438 b = append(b, '0')
439 }
440
441 b = strconv.AppendInt(b, int64(t.Minute()), 10)
442 b = append(b, yi.timeSeparator...)
443
444 if t.Second() < 10 {
445 b = append(b, '0')
446 }
447
448 b = strconv.AppendInt(b, int64(t.Second()), 10)
449
450 return string(b)
451 }
452
453
454 func (yi *yi) FmtTimeLong(t time.Time) string {
455
456 b := make([]byte, 0, 32)
457
458 if t.Hour() < 10 {
459 b = append(b, '0')
460 }
461
462 b = strconv.AppendInt(b, int64(t.Hour()), 10)
463 b = append(b, yi.timeSeparator...)
464
465 if t.Minute() < 10 {
466 b = append(b, '0')
467 }
468
469 b = strconv.AppendInt(b, int64(t.Minute()), 10)
470 b = append(b, yi.timeSeparator...)
471
472 if t.Second() < 10 {
473 b = append(b, '0')
474 }
475
476 b = strconv.AppendInt(b, int64(t.Second()), 10)
477 b = append(b, []byte{0x20}...)
478
479 tz, _ := t.Zone()
480 b = append(b, tz...)
481
482 return string(b)
483 }
484
485
486 func (yi *yi) FmtTimeFull(t time.Time) string {
487
488 b := make([]byte, 0, 32)
489
490 if t.Hour() < 10 {
491 b = append(b, '0')
492 }
493
494 b = strconv.AppendInt(b, int64(t.Hour()), 10)
495 b = append(b, yi.timeSeparator...)
496
497 if t.Minute() < 10 {
498 b = append(b, '0')
499 }
500
501 b = strconv.AppendInt(b, int64(t.Minute()), 10)
502 b = append(b, yi.timeSeparator...)
503
504 if t.Second() < 10 {
505 b = append(b, '0')
506 }
507
508 b = strconv.AppendInt(b, int64(t.Second()), 10)
509 b = append(b, []byte{0x20}...)
510
511 tz, _ := t.Zone()
512
513 if btz, ok := yi.timezones[tz]; ok {
514 b = append(b, btz...)
515 } else {
516 b = append(b, tz...)
517 }
518
519 return string(b)
520 }
521
View as plain text