8 "github.com/go-playground/locales"
9 "github.com/go-playground/locales/currency"
14 pluralsCardinal []locales.PluralRule
15 pluralsOrdinal []locales.PluralRule
16 pluralsRange []locales.PluralRule
24 currencies []string // idx = enum of currency code
25 currencyPositiveSuffix string
26 currencyNegativeSuffix string
27 monthsAbbreviated []string
30 daysAbbreviated []string
34 periodsAbbreviated []string
35 periodsNarrow []string
38 erasAbbreviated []string
41 timezones map[string]string
44 // New returns a new instance of translator for the 'prg' locale
45 func New() locales.Translator {
48 pluralsCardinal: []locales.PluralRule{1, 2, 6},
49 pluralsOrdinal: []locales.PluralRule{6},
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", "BYN", "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"},
57 currencyPositiveSuffix: " ",
58 currencyNegativeSuffix: " ",
59 monthsAbbreviated: []string{"", "rag", "was", "pūl", "sak", "zal", "sīm", "līp", "dag", "sil", "spa", "lap", "sal"},
60 monthsNarrow: []string{"", "R", "W", "P", "S", "Z", "S", "L", "D", "S", "S", "L", "S"},
61 monthsWide: []string{"", "rags", "wassarins", "pūlis", "sakkis", "zallaws", "sīmenis", "līpa", "daggis", "sillins", "spallins", "lapkrūtis", "sallaws"},
62 daysAbbreviated: []string{"nad", "pan", "wis", "pus", "ket", "pēn", "sab"},
63 daysNarrow: []string{"N", "P", "W", "P", "K", "P", "S"},
64 daysWide: []string{"nadīli", "panadīli", "wisasīdis", "pussisawaiti", "ketwirtiks", "pēntniks", "sabattika"},
65 periodsAbbreviated: []string{"AM", "PM"},
66 periodsWide: []string{"ankstāinan", "pa pussideinan"},
67 erasAbbreviated: []string{"BC", "AD"},
68 erasNarrow: []string{"", ""},
69 erasWide: []string{"", ""},
70 timezones: map[string]string{"HNT": "HNT", "SRT": "SRT", "ACWST": "ACWST", "HAST": "HAST", "IST": "IST", "CHADT": "CHADT", "HNPM": "HNPM", "PST": "Pacīfiskas Amērikas zēimas kerdā", "PDT": "Pacīfiskas Amērikas daggas kerdā", "HECU": "HECU", "MYT": "MYT", "WITA": "WITA", "HNOG": "HNOG", "ChST": "ChST", "SGT": "SGT", "ARST": "ARST", "BT": "BT", "ACWDT": "ACWDT", "COT": "COT", "WEZ": "Wakkariskas Eurōpas zēimas kerdā", "HEPMX": "HEPMX", "CST": "Centrālas Amērikas zēimas kerdā", "MDT": "MDT", "HADT": "HADT", "NZST": "NZST", "LHST": "LHST", "AEST": "AEST", "CLST": "CLST", "HEPM": "HEPM", "AKDT": "AKDT", "AKST": "AKST", "UYT": "UYT", "UYST": "UYST", "VET": "VET", "HEOG": "HEOG", "ADT": "Atlāntiska daggas kerdā", "HEEG": "HEEG", "EAT": "EAT", "GMT": "Greenwich kerdā", "WAST": "WAST", "LHDT": "LHDT", "ART": "ART", "GYT": "GYT", "CAT": "CAT", "TMST": "TMST", "WIT": "WIT", "MEZ": "Centrālas Eurōpas zēimas kerdā", "JST": "JST", "HKT": "HKT", "∅∅∅": "∅∅∅", "AST": "Atlāntiska zēimas kerdā", "MST": "MST", "WART": "WART", "OESZ": "Dēiniskas Eurōpas daggas kerdā", "HKST": "HKST", "WESZ": "Wakkariskas Eurōpas daggas kerdā", "HNCU": "HNCU", "SAST": "SAST", "ACST": "ACST", "AEDT": "AEDT", "CDT": "Centrālas Amērikas daggas kerdā", "COST": "COST", "CLT": "CLT", "ECT": "ECT", "HNPMX": "HNPMX", "WARST": "WARST", "WIB": "WIB", "NZDT": "NZDT", "JDT": "JDT", "WAT": "WAT", "EST": "Dēiniskas Amērikas zēimas kerdā", "TMT": "TMT", "MESZ": "Centrālas Eurōpas daggas kerdā", "OEZ": "Dēiniskas Eurōpas zēimas kerdā", "ACDT": "ACDT", "BOT": "BOT", "AWST": "AWST", "AWDT": "AWDT", "HNNOMX": "HNNOMX", "HENOMX": "HENOMX", "HNEG": "HNEG", "GFT": "GFT", "HAT": "HAT", "CHAST": "CHAST", "EDT": "Dēiniskas Amērikas daggas kerdā"},
74 // Locale returns the current translators string locale
75 func (prg *prg) Locale() string {
79 // PluralsCardinal returns the list of cardinal plural rules associated with 'prg'
80 func (prg *prg) PluralsCardinal() []locales.PluralRule {
81 return prg.pluralsCardinal
84 // PluralsOrdinal returns the list of ordinal plural rules associated with 'prg'
85 func (prg *prg) PluralsOrdinal() []locales.PluralRule {
86 return prg.pluralsOrdinal
89 // PluralsRange returns the list of range plural rules associated with 'prg'
90 func (prg *prg) PluralsRange() []locales.PluralRule {
91 return prg.pluralsRange
94 // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'prg'
95 func (prg *prg) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
99 nMod10 := math.Mod(n, 10)
100 nMod100 := math.Mod(n, 100)
104 if (nMod10 == 0) || (nMod100 >= 11 && nMod100 <= 19) || (v == 2 && fMod100 >= 11 && fMod100 <= 19) {
105 return locales.PluralRuleZero
106 } else if (nMod10 == 1 && nMod100 != 11) || (v == 2 && fMod10 == 1 && fMod100 != 11) || (v != 2 && fMod10 == 1) {
107 return locales.PluralRuleOne
110 return locales.PluralRuleOther
113 // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'prg'
114 func (prg *prg) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
115 return locales.PluralRuleOther
118 // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'prg'
119 func (prg *prg) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
120 return locales.PluralRuleUnknown
123 // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
124 func (prg *prg) MonthAbbreviated(month time.Month) string {
125 return prg.monthsAbbreviated[month]
128 // MonthsAbbreviated returns the locales abbreviated months
129 func (prg *prg) MonthsAbbreviated() []string {
130 return prg.monthsAbbreviated[1:]
133 // MonthNarrow returns the locales narrow month given the 'month' provided
134 func (prg *prg) MonthNarrow(month time.Month) string {
135 return prg.monthsNarrow[month]
138 // MonthsNarrow returns the locales narrow months
139 func (prg *prg) MonthsNarrow() []string {
140 return prg.monthsNarrow[1:]
143 // MonthWide returns the locales wide month given the 'month' provided
144 func (prg *prg) MonthWide(month time.Month) string {
145 return prg.monthsWide[month]
148 // MonthsWide returns the locales wide months
149 func (prg *prg) MonthsWide() []string {
150 return prg.monthsWide[1:]
153 // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
154 func (prg *prg) WeekdayAbbreviated(weekday time.Weekday) string {
155 return prg.daysAbbreviated[weekday]
158 // WeekdaysAbbreviated returns the locales abbreviated weekdays
159 func (prg *prg) WeekdaysAbbreviated() []string {
160 return prg.daysAbbreviated
163 // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
164 func (prg *prg) WeekdayNarrow(weekday time.Weekday) string {
165 return prg.daysNarrow[weekday]
168 // WeekdaysNarrow returns the locales narrow weekdays
169 func (prg *prg) WeekdaysNarrow() []string {
170 return prg.daysNarrow
173 // WeekdayShort returns the locales short weekday given the 'weekday' provided
174 func (prg *prg) WeekdayShort(weekday time.Weekday) string {
175 return prg.daysShort[weekday]
178 // WeekdaysShort returns the locales short weekdays
179 func (prg *prg) WeekdaysShort() []string {
183 // WeekdayWide returns the locales wide weekday given the 'weekday' provided
184 func (prg *prg) WeekdayWide(weekday time.Weekday) string {
185 return prg.daysWide[weekday]
188 // WeekdaysWide returns the locales wide weekdays
189 func (prg *prg) WeekdaysWide() []string {
193 // Decimal returns the decimal point of number
194 func (prg *prg) Decimal() string {
198 // Group returns the group of number
199 func (prg *prg) Group() string {
203 // Group returns the minus sign of number
204 func (prg *prg) Minus() string {
208 // FmtNumber returns 'num' with digits/precision of 'v' for 'prg' and handles both Whole and Real numbers based on 'v'
209 func (prg *prg) FmtNumber(num float64, v uint64) string {
211 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
212 l := len(s) + 2 + 2*len(s[:len(s)-int(v)-1])/3
215 b := make([]byte, 0, l)
217 for i := len(s) - 1; i >= 0; i-- {
220 b = append(b, prg.decimal[0])
227 for j := len(prg.group) - 1; j >= 0; j-- {
228 b = append(b, prg.group[j])
240 b = append(b, prg.minus[0])
244 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
245 b[i], b[j] = b[j], b[i]
251 // FmtPercent returns 'num' with digits/precision of 'v' for 'prg' and handles both Whole and Real numbers based on 'v'
252 // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
253 func (prg *prg) FmtPercent(num float64, v uint64) string {
254 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
256 b := make([]byte, 0, l)
258 for i := len(s) - 1; i >= 0; i-- {
261 b = append(b, prg.decimal[0])
269 b = append(b, prg.minus[0])
273 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
274 b[i], b[j] = b[j], b[i]
277 b = append(b, prg.percent...)
282 // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'prg'
283 func (prg *prg) FmtCurrency(num float64, v uint64, currency currency.Type) string {
285 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
286 symbol := prg.currencies[currency]
287 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
290 b := make([]byte, 0, l)
292 for i := len(s) - 1; i >= 0; i-- {
295 b = append(b, prg.decimal[0])
302 for j := len(prg.group) - 1; j >= 0; j-- {
303 b = append(b, prg.group[j])
315 b = append(b, prg.minus[0])
319 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
320 b[i], b[j] = b[j], b[i]
326 b = append(b, prg.decimal...)
329 for i := 0; i < 2-int(v); i++ {
334 b = append(b, prg.currencyPositiveSuffix...)
336 b = append(b, symbol...)
341 // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'prg'
342 // in accounting notation.
343 func (prg *prg) FmtAccounting(num float64, v uint64, currency currency.Type) string {
345 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
346 symbol := prg.currencies[currency]
347 l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
350 b := make([]byte, 0, l)
352 for i := len(s) - 1; i >= 0; i-- {
355 b = append(b, prg.decimal[0])
362 for j := len(prg.group) - 1; j >= 0; j-- {
363 b = append(b, prg.group[j])
376 b = append(b, prg.minus[0])
381 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
382 b[i], b[j] = b[j], b[i]
388 b = append(b, prg.decimal...)
391 for i := 0; i < 2-int(v); i++ {
397 b = append(b, prg.currencyNegativeSuffix...)
398 b = append(b, symbol...)
401 b = append(b, prg.currencyPositiveSuffix...)
402 b = append(b, symbol...)
408 // FmtDateShort returns the short date representation of 't' for 'prg'
409 func (prg *prg) FmtDateShort(t time.Time) string {
411 b := make([]byte, 0, 32)
417 b = strconv.AppendInt(b, int64(t.Day()), 10)
418 b = append(b, []byte{0x2e}...)
424 b = strconv.AppendInt(b, int64(t.Month()), 10)
426 b = append(b, []byte{0x2e}...)
429 b = append(b, strconv.Itoa(t.Year())[2:]...)
431 b = append(b, strconv.Itoa(t.Year())[1:]...)
437 // FmtDateMedium returns the medium date representation of 't' for 'prg'
438 func (prg *prg) FmtDateMedium(t time.Time) string {
440 b := make([]byte, 0, 32)
446 b = strconv.AppendInt(b, int64(t.Day()), 10)
447 b = append(b, []byte{0x2e}...)
453 b = strconv.AppendInt(b, int64(t.Month()), 10)
455 b = append(b, []byte{0x20, 0x73, 0x74}...)
456 b = append(b, []byte{0x2e, 0x20}...)
459 b = strconv.AppendInt(b, int64(t.Year()), 10)
461 b = strconv.AppendInt(b, int64(-t.Year()), 10)
467 // FmtDateLong returns the long date representation of 't' for 'prg'
468 func (prg *prg) FmtDateLong(t time.Time) string {
470 b := make([]byte, 0, 32)
473 b = strconv.AppendInt(b, int64(t.Year()), 10)
475 b = strconv.AppendInt(b, int64(-t.Year()), 10)
478 b = append(b, []byte{0x20, 0x6d, 0x65, 0x74, 0x74, 0x61, 0x73}...)
479 b = append(b, []byte{0x20}...)
480 b = strconv.AppendInt(b, int64(t.Day()), 10)
481 b = append(b, []byte{0x2e, 0x20}...)
482 b = append(b, prg.monthsWide[t.Month()]...)
487 // FmtDateFull returns the full date representation of 't' for 'prg'
488 func (prg *prg) FmtDateFull(t time.Time) string {
490 b := make([]byte, 0, 32)
492 b = append(b, prg.daysWide[t.Weekday()]...)
493 b = append(b, []byte{0x2c, 0x20}...)
496 b = strconv.AppendInt(b, int64(t.Year()), 10)
498 b = strconv.AppendInt(b, int64(-t.Year()), 10)
501 b = append(b, []byte{0x20, 0x6d, 0x65, 0x74, 0x74, 0x61, 0x73}...)
502 b = append(b, []byte{0x20}...)
503 b = strconv.AppendInt(b, int64(t.Day()), 10)
504 b = append(b, []byte{0x2e, 0x20}...)
505 b = append(b, prg.monthsWide[t.Month()]...)
510 // FmtTimeShort returns the short time representation of 't' for 'prg'
511 func (prg *prg) FmtTimeShort(t time.Time) string {
513 b := make([]byte, 0, 32)
519 b = strconv.AppendInt(b, int64(t.Hour()), 10)
520 b = append(b, prg.timeSeparator...)
526 b = strconv.AppendInt(b, int64(t.Minute()), 10)
531 // FmtTimeMedium returns the medium time representation of 't' for 'prg'
532 func (prg *prg) FmtTimeMedium(t time.Time) string {
534 b := make([]byte, 0, 32)
540 b = strconv.AppendInt(b, int64(t.Hour()), 10)
541 b = append(b, prg.timeSeparator...)
547 b = strconv.AppendInt(b, int64(t.Minute()), 10)
548 b = append(b, prg.timeSeparator...)
554 b = strconv.AppendInt(b, int64(t.Second()), 10)
559 // FmtTimeLong returns the long time representation of 't' for 'prg'
560 func (prg *prg) FmtTimeLong(t time.Time) string {
562 b := make([]byte, 0, 32)
568 b = strconv.AppendInt(b, int64(t.Hour()), 10)
569 b = append(b, prg.timeSeparator...)
575 b = strconv.AppendInt(b, int64(t.Minute()), 10)
576 b = append(b, prg.timeSeparator...)
582 b = strconv.AppendInt(b, int64(t.Second()), 10)
583 b = append(b, []byte{0x20}...)
591 // FmtTimeFull returns the full time representation of 't' for 'prg'
592 func (prg *prg) FmtTimeFull(t time.Time) string {
594 b := make([]byte, 0, 32)
600 b = strconv.AppendInt(b, int64(t.Hour()), 10)
601 b = append(b, prg.timeSeparator...)
607 b = strconv.AppendInt(b, int64(t.Minute()), 10)
608 b = append(b, prg.timeSeparator...)
614 b = strconv.AppendInt(b, int64(t.Second()), 10)
615 b = append(b, []byte{0x20}...)
619 if btz, ok := prg.timezones[tz]; ok {
620 b = append(b, btz...)