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
25 currencies []string // idx = enum of currency code
26 currencyPositiveSuffix string
27 currencyNegativeSuffix string
28 monthsAbbreviated []string
31 daysAbbreviated []string
35 periodsAbbreviated []string
36 periodsNarrow []string
39 erasAbbreviated []string
42 timezones map[string]string
45 // New returns a new instance of translator for the 'nn' locale
46 func New() locales.Translator {
49 pluralsCardinal: []locales.PluralRule{2, 6},
59 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", "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", "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"},
61 currencyPositiveSuffix: " ",
62 currencyNegativeSuffix: " ",
63 monthsAbbreviated: []string{"", "jan.", "feb.", "mars", "apr.", "mai", "juni", "juli", "aug.", "sep.", "okt.", "nov.", "des."},
64 monthsNarrow: []string{"", "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
65 monthsWide: []string{"", "januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"},
66 daysAbbreviated: []string{"sø.", "må.", "ty.", "on.", "to.", "fr.", "la."},
67 daysNarrow: []string{"S", "M", "T", "O", "T", "F", "L"},
68 daysShort: []string{"sø.", "må.", "ty.", "on.", "to.", "fr.", "la."},
69 daysWide: []string{"søndag", "måndag", "tysdag", "onsdag", "torsdag", "fredag", "laurdag"},
70 periodsAbbreviated: []string{"f.m.", "e.m."},
71 periodsNarrow: []string{"f.m.", "e.m."},
72 periodsWide: []string{"formiddag", "ettermiddag"},
73 erasAbbreviated: []string{"f.Kr.", "e.Kr."},
74 erasNarrow: []string{"f.Kr.", "e.Kr."},
75 erasWide: []string{"f.Kr.", "e.Kr."},
76 timezones: map[string]string{"COST": "COST", "ACST": "sentralaustralsk standardtid", "CAT": "sentralafrikansk tid", "ChST": "ChST", "MST": "MST", "AWST": "vestaustralsk standardtid", "OESZ": "austeuropeisk sommartid", "CHADT": "CHADT", "WAST": "vestafrikansk sommartid", "EST": "EST", "CST": "CST", "JST": "JST", "PST": "PST", "BT": "BT", "WITA": "WITA", "IST": "IST", "∅∅∅": "∅∅∅", "PDT": "PDT", "AWDT": "vestaustralsk sommartid", "LHST": "LHST", "AST": "AST", "AEST": "austaustralsk standardtid", "EDT": "EDT", "WIT": "WIT", "ART": "ART", "SAST": "sørafrikansk tid", "WAT": "vestafrikansk standardtid", "AKST": "AKST", "ACDT": "sentralaustralsk sommartid", "WESZ": "vesteuropeisk sommartid", "SGT": "SGT", "GMT": "greenwich middeltid", "CDT": "CDT", "MDT": "MDT", "ACWDT": "vest-sentralaustralsk sommartid", "MESZ": "sentraleuropeisk sommartid", "NZDT": "NZDT", "OEZ": "austeuropeisk standardtid", "HAT": "HAT", "WEZ": "vesteuropeisk standardtid", "TMT": "TMT", "MEZ": "sentraleuropeisk standardtid", "WARST": "WARST", "HENOMX": "HENOMX", "LHDT": "LHDT", "CLT": "CLT", "GYT": "GYT", "SRT": "SRT", "HKT": "HKT", "HEPMX": "HEPMX", "BOT": "BOT", "ACWST": "vest-sentralaustralsk standardtid", "TMST": "TMST", "AEDT": "austaustralsk sommartid", "EAT": "austafrikansk tid", "GFT": "GFT", "ECT": "ECT", "HNCU": "HNCU", "HECU": "HECU", "MYT": "MYT", "HNOG": "HNOG", "AKDT": "AKDT", "HAST": "HAST", "ADT": "ADT", "WIB": "WIB", "HADT": "HADT", "ARST": "ARST", "HNPMX": "HNPMX", "CHAST": "CHAST", "UYT": "UYT", "NZST": "NZST", "HNNOMX": "HNNOMX", "HEEG": "HEEG", "HNT": "HNT", "HEPM": "HEPM", "UYST": "UYST", "WART": "WART", "JDT": "JDT", "HNEG": "HNEG", "COT": "COT", "HKST": "HKST", "CLST": "CLST", "HNPM": "HNPM", "VET": "VET", "HEOG": "HEOG"},
80 // Locale returns the current translators string locale
81 func (nn *nn) Locale() string {
85 // PluralsCardinal returns the list of cardinal plural rules associated with 'nn'
86 func (nn *nn) PluralsCardinal() []locales.PluralRule {
87 return nn.pluralsCardinal
90 // PluralsOrdinal returns the list of ordinal plural rules associated with 'nn'
91 func (nn *nn) PluralsOrdinal() []locales.PluralRule {
92 return nn.pluralsOrdinal
95 // PluralsRange returns the list of range plural rules associated with 'nn'
96 func (nn *nn) PluralsRange() []locales.PluralRule {
97 return nn.pluralsRange
100 // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'nn'
101 func (nn *nn) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
106 return locales.PluralRuleOne
109 return locales.PluralRuleOther
112 // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'nn'
113 func (nn *nn) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
114 return locales.PluralRuleUnknown
117 // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'nn'
118 func (nn *nn) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
119 return locales.PluralRuleUnknown
122 // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
123 func (nn *nn) MonthAbbreviated(month time.Month) string {
124 return nn.monthsAbbreviated[month]
127 // MonthsAbbreviated returns the locales abbreviated months
128 func (nn *nn) MonthsAbbreviated() []string {
129 return nn.monthsAbbreviated[1:]
132 // MonthNarrow returns the locales narrow month given the 'month' provided
133 func (nn *nn) MonthNarrow(month time.Month) string {
134 return nn.monthsNarrow[month]
137 // MonthsNarrow returns the locales narrow months
138 func (nn *nn) MonthsNarrow() []string {
139 return nn.monthsNarrow[1:]
142 // MonthWide returns the locales wide month given the 'month' provided
143 func (nn *nn) MonthWide(month time.Month) string {
144 return nn.monthsWide[month]
147 // MonthsWide returns the locales wide months
148 func (nn *nn) MonthsWide() []string {
149 return nn.monthsWide[1:]
152 // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
153 func (nn *nn) WeekdayAbbreviated(weekday time.Weekday) string {
154 return nn.daysAbbreviated[weekday]
157 // WeekdaysAbbreviated returns the locales abbreviated weekdays
158 func (nn *nn) WeekdaysAbbreviated() []string {
159 return nn.daysAbbreviated
162 // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
163 func (nn *nn) WeekdayNarrow(weekday time.Weekday) string {
164 return nn.daysNarrow[weekday]
167 // WeekdaysNarrow returns the locales narrow weekdays
168 func (nn *nn) WeekdaysNarrow() []string {
172 // WeekdayShort returns the locales short weekday given the 'weekday' provided
173 func (nn *nn) WeekdayShort(weekday time.Weekday) string {
174 return nn.daysShort[weekday]
177 // WeekdaysShort returns the locales short weekdays
178 func (nn *nn) WeekdaysShort() []string {
182 // WeekdayWide returns the locales wide weekday given the 'weekday' provided
183 func (nn *nn) WeekdayWide(weekday time.Weekday) string {
184 return nn.daysWide[weekday]
187 // WeekdaysWide returns the locales wide weekdays
188 func (nn *nn) WeekdaysWide() []string {
192 // Decimal returns the decimal point of number
193 func (nn *nn) Decimal() string {
197 // Group returns the group of number
198 func (nn *nn) Group() string {
202 // Group returns the minus sign of number
203 func (nn *nn) Minus() string {
207 // FmtNumber returns 'num' with digits/precision of 'v' for 'nn' and handles both Whole and Real numbers based on 'v'
208 func (nn *nn) FmtNumber(num float64, v uint64) string {
210 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
211 l := len(s) + 4 + 2*len(s[:len(s)-int(v)-1])/3
214 b := make([]byte, 0, l)
216 for i := len(s) - 1; i >= 0; i-- {
219 b = append(b, nn.decimal[0])
226 for j := len(nn.group) - 1; j >= 0; j-- {
227 b = append(b, nn.group[j])
239 for j := len(nn.minus) - 1; j >= 0; j-- {
240 b = append(b, nn.minus[j])
245 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
246 b[i], b[j] = b[j], b[i]
252 // FmtPercent returns 'num' with digits/precision of 'v' for 'nn' and handles both Whole and Real numbers based on 'v'
253 // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
254 func (nn *nn) FmtPercent(num float64, v uint64) string {
255 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
257 b := make([]byte, 0, l)
259 for i := len(s) - 1; i >= 0; i-- {
262 b = append(b, nn.decimal[0])
270 for j := len(nn.minus) - 1; j >= 0; j-- {
271 b = append(b, nn.minus[j])
276 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
277 b[i], b[j] = b[j], b[i]
280 b = append(b, nn.percentSuffix...)
282 b = append(b, nn.percent...)
287 // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'nn'
288 func (nn *nn) FmtCurrency(num float64, v uint64, currency currency.Type) string {
290 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
291 symbol := nn.currencies[currency]
292 l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
295 b := make([]byte, 0, l)
297 for i := len(s) - 1; i >= 0; i-- {
300 b = append(b, nn.decimal[0])
307 for j := len(nn.group) - 1; j >= 0; j-- {
308 b = append(b, nn.group[j])
320 for j := len(nn.minus) - 1; j >= 0; j-- {
321 b = append(b, nn.minus[j])
326 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
327 b[i], b[j] = b[j], b[i]
333 b = append(b, nn.decimal...)
336 for i := 0; i < 2-int(v); i++ {
341 b = append(b, nn.currencyPositiveSuffix...)
343 b = append(b, symbol...)
348 // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'nn'
349 // in accounting notation.
350 func (nn *nn) FmtAccounting(num float64, v uint64, currency currency.Type) string {
352 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
353 symbol := nn.currencies[currency]
354 l := len(s) + len(symbol) + 6 + 2*len(s[:len(s)-int(v)-1])/3
357 b := make([]byte, 0, l)
359 for i := len(s) - 1; i >= 0; i-- {
362 b = append(b, nn.decimal[0])
369 for j := len(nn.group) - 1; j >= 0; j-- {
370 b = append(b, nn.group[j])
383 for j := len(nn.minus) - 1; j >= 0; j-- {
384 b = append(b, nn.minus[j])
390 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
391 b[i], b[j] = b[j], b[i]
397 b = append(b, nn.decimal...)
400 for i := 0; i < 2-int(v); i++ {
406 b = append(b, nn.currencyNegativeSuffix...)
407 b = append(b, symbol...)
410 b = append(b, nn.currencyPositiveSuffix...)
411 b = append(b, symbol...)
417 // FmtDateShort returns the short date representation of 't' for 'nn'
418 func (nn *nn) FmtDateShort(t time.Time) string {
420 b := make([]byte, 0, 32)
426 b = strconv.AppendInt(b, int64(t.Day()), 10)
427 b = append(b, []byte{0x2e}...)
433 b = strconv.AppendInt(b, int64(t.Month()), 10)
435 b = append(b, []byte{0x2e}...)
438 b = strconv.AppendInt(b, int64(t.Year()), 10)
440 b = strconv.AppendInt(b, int64(-t.Year()), 10)
446 // FmtDateMedium returns the medium date representation of 't' for 'nn'
447 func (nn *nn) FmtDateMedium(t time.Time) string {
449 b := make([]byte, 0, 32)
451 b = strconv.AppendInt(b, int64(t.Day()), 10)
452 b = append(b, []byte{0x2e, 0x20}...)
453 b = append(b, nn.monthsAbbreviated[t.Month()]...)
454 b = append(b, []byte{0x20}...)
457 b = strconv.AppendInt(b, int64(t.Year()), 10)
459 b = strconv.AppendInt(b, int64(-t.Year()), 10)
465 // FmtDateLong returns the long date representation of 't' for 'nn'
466 func (nn *nn) FmtDateLong(t time.Time) string {
468 b := make([]byte, 0, 32)
470 b = strconv.AppendInt(b, int64(t.Day()), 10)
471 b = append(b, []byte{0x2e, 0x20}...)
472 b = append(b, nn.monthsWide[t.Month()]...)
473 b = append(b, []byte{0x20}...)
476 b = strconv.AppendInt(b, int64(t.Year()), 10)
478 b = strconv.AppendInt(b, int64(-t.Year()), 10)
484 // FmtDateFull returns the full date representation of 't' for 'nn'
485 func (nn *nn) FmtDateFull(t time.Time) string {
487 b := make([]byte, 0, 32)
489 b = append(b, nn.daysWide[t.Weekday()]...)
490 b = append(b, []byte{0x20}...)
491 b = strconv.AppendInt(b, int64(t.Day()), 10)
492 b = append(b, []byte{0x2e, 0x20}...)
493 b = append(b, nn.monthsWide[t.Month()]...)
494 b = append(b, []byte{0x20}...)
497 b = strconv.AppendInt(b, int64(t.Year()), 10)
499 b = strconv.AppendInt(b, int64(-t.Year()), 10)
505 // FmtTimeShort returns the short time representation of 't' for 'nn'
506 func (nn *nn) FmtTimeShort(t time.Time) string {
508 b := make([]byte, 0, 32)
514 b = strconv.AppendInt(b, int64(t.Hour()), 10)
515 b = append(b, nn.timeSeparator...)
521 b = strconv.AppendInt(b, int64(t.Minute()), 10)
526 // FmtTimeMedium returns the medium time representation of 't' for 'nn'
527 func (nn *nn) FmtTimeMedium(t time.Time) string {
529 b := make([]byte, 0, 32)
535 b = strconv.AppendInt(b, int64(t.Hour()), 10)
536 b = append(b, nn.timeSeparator...)
542 b = strconv.AppendInt(b, int64(t.Minute()), 10)
543 b = append(b, nn.timeSeparator...)
549 b = strconv.AppendInt(b, int64(t.Second()), 10)
554 // FmtTimeLong returns the long time representation of 't' for 'nn'
555 func (nn *nn) FmtTimeLong(t time.Time) string {
557 b := make([]byte, 0, 32)
563 b = strconv.AppendInt(b, int64(t.Hour()), 10)
564 b = append(b, nn.timeSeparator...)
570 b = strconv.AppendInt(b, int64(t.Minute()), 10)
571 b = append(b, nn.timeSeparator...)
577 b = strconv.AppendInt(b, int64(t.Second()), 10)
578 b = append(b, []byte{0x20}...)
586 // FmtTimeFull returns the full time representation of 't' for 'nn'
587 func (nn *nn) FmtTimeFull(t time.Time) string {
589 b := make([]byte, 0, 32)
591 b = append(b, []byte{0x6b, 0x6c}...)
592 b = append(b, []byte{0x2e, 0x20}...)
598 b = strconv.AppendInt(b, int64(t.Hour()), 10)
599 b = append(b, nn.timeSeparator...)
605 b = strconv.AppendInt(b, int64(t.Minute()), 10)
606 b = append(b, nn.timeSeparator...)
612 b = strconv.AppendInt(b, int64(t.Second()), 10)
613 b = append(b, []byte{0x20}...)
617 if btz, ok := nn.timezones[tz]; ok {
618 b = append(b, btz...)