...

Text file src/github.com/go-playground/locales/cmd/translator.tmpl

Documentation: github.com/go-playground/locales/cmd

     1{{ define "translator" }}
     2package {{ .Locale }}
     3
     4import (
     5	"math"
     6	"strconv"
     7	"time"
     8
     9	"github.com/go-playground/locales"
    10	"github.com/go-playground/locales/currency"
    11)
    12
    13type {{ .Locale }} struct {
    14	locale     			   string
    15	pluralsCardinal 	   []locales.PluralRule
    16	pluralsOrdinal  	   []locales.PluralRule
    17	pluralsRange		   []locales.PluralRule
    18	decimal    			   string
    19	group      			   string
    20	minus      			   string
    21	percent    			   string
    22	{{- if gt (len .FmtPercentPrefix) 0}}
    23	percentPrefix   	   string
    24	{{- end }}
    25	{{- if gt (len .FmtPercentSuffix) 0}}
    26	percentSuffix   	   string
    27	{{- end }}
    28	perMille   			   string
    29	timeSeparator		   string
    30	inifinity			   string
    31	currencies 			   []string // idx = enum of currency code
    32	{{- if gt (len .FmtCurrencyPrefix) 0}}
    33	currencyPositivePrefix string
    34	{{- end }}
    35	{{- if gt (len .FmtCurrencySuffix) 0}}
    36	currencyPositiveSuffix string
    37	{{- end }}
    38	{{- if gt (len .FmtCurrencyNegativePrefix) 0}}
    39	currencyNegativePrefix string
    40	{{- end }}
    41	{{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
    42	currencyNegativeSuffix string
    43	{{- end }}
    44	monthsAbbreviated      []string
    45	monthsNarrow      	   []string
    46	monthsWide      	   []string
    47	daysAbbreviated        []string
    48	daysNarrow      	   []string
    49	daysShort      	   	   []string
    50	daysWide      	   	   []string
    51	periodsAbbreviated     []string
    52	periodsNarrow      	   []string
    53	periodsShort      	   []string
    54	periodsWide      	   []string
    55	erasAbbreviated        []string
    56	erasNarrow      	   []string
    57	erasWide      	   	   []string
    58	timezones 			   map[string]string
    59}
    60
    61// New returns a new instance of translator for the '{{ .Locale }}' locale
    62func New() locales.Translator {
    63	return &{{ .Locale }}{
    64		locale:   		 "{{ .Locale }}",
    65		pluralsCardinal: {{ .Plurals }},
    66		pluralsOrdinal:  {{ .PluralsOrdinal }},
    67		pluralsRange:    {{ .PluralsRange }},
    68		{{- if gt (len .Decimal) 0}}
    69		decimal:  		 "{{ .Decimal }}",
    70		{{- end}}
    71		{{- if gt (len .Group) 0}}
    72		group:    		 "{{ .Group }}",
    73		{{- end}}
    74		{{- if gt (len .Minus) 0}}
    75		minus:    		 "{{ .Minus }}",
    76		{{- end}}
    77		{{- if gt (len .Percent) 0}}
    78		percent:  		 "{{ .Percent }}",
    79		{{- end}}
    80		{{- if gt (len .PerMille) 0}}
    81		perMille:        "{{ .PerMille }}",
    82		{{- end}}
    83		{{- if gt (len .TimeSeparator) 0}}
    84		timeSeparator:	 "{{ .TimeSeparator }}",
    85		{{- end}}
    86		{{- if gt (len .Infinity) 0}}
    87		inifinity:		 "{{ .Infinity }}",
    88		{{- end}}
    89		currencies:      {{ .Currencies }},
    90		{{- if gt (len .FmtPercentPrefix) 0}}
    91		percentPrefix:   "{{ .FmtPercentPrefix }}",
    92		{{- end -}}
    93		{{- if gt (len .FmtPercentSuffix) 0}}
    94		percentSuffix:   "{{ .FmtPercentSuffix }}",
    95		{{- end -}}
    96		{{- if gt (len .FmtCurrencyPrefix) 0}}
    97		currencyPositivePrefix:   "{{ .FmtCurrencyPrefix }}",
    98		{{- end -}}
    99		{{- if gt (len .FmtCurrencySuffix) 0}}
   100		currencyPositiveSuffix:   "{{ .FmtCurrencySuffix }}",
   101		{{- end -}}
   102		{{- if gt (len .FmtCurrencyNegativePrefix) 0}}
   103		currencyNegativePrefix:   "{{ .FmtCurrencyNegativePrefix }}",
   104		{{- end -}}
   105		{{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
   106		currencyNegativeSuffix:   "{{ .FmtCurrencyNegativeSuffix }}",
   107		{{- end -}}
   108		{{- if gt (len .FmtMonthsAbbreviated) 0 }}
   109		monthsAbbreviated:   {{ .FmtMonthsAbbreviated }},
   110		{{- end -}}
   111		{{- if gt (len .FmtMonthsNarrow) 0 }}
   112		monthsNarrow:   {{ .FmtMonthsNarrow }},
   113		{{- end -}}
   114		{{- if gt (len .FmtMonthsWide) 0 }}
   115		monthsWide:   {{ .FmtMonthsWide }},
   116		{{- end -}}
   117		{{- if gt (len .FmtDaysAbbreviated) 0 }}
   118		daysAbbreviated:   {{ .FmtDaysAbbreviated }},
   119		{{- end -}}
   120		{{- if gt (len .FmtDaysNarrow) 0 }}
   121		daysNarrow:   {{ .FmtDaysNarrow }},
   122		{{- end -}}
   123		{{- if gt (len .FmtDaysShort) 0 }}
   124		daysShort:   {{ .FmtDaysShort }},
   125		{{- end -}}
   126		{{- if gt (len .FmtDaysWide) 0 }}
   127		daysWide:   {{ .FmtDaysWide }},
   128		{{- end -}}
   129		{{- if gt (len .FmtPeriodsAbbreviated) 0 }}
   130		periodsAbbreviated:   {{ .FmtPeriodsAbbreviated }},
   131		{{- end -}}
   132		{{- if gt (len .FmtPeriodsNarrow) 0 }}
   133		periodsNarrow:   {{ .FmtPeriodsNarrow }},
   134		{{- end -}}
   135		{{- if gt (len .FmtPeriodsShort) 0 }}
   136		periodsShort:   {{ .FmtPeriodsShort }},
   137		{{- end -}}
   138		{{- if gt (len .FmtPeriodsWide) 0 }}
   139		periodsWide:   {{ .FmtPeriodsWide }},
   140		{{- end -}}
   141		{{- if gt (len .FmtErasAbbreviated) 0 }}
   142		erasAbbreviated:   {{ .FmtErasAbbreviated }},
   143		{{- end -}}
   144		{{- if gt (len .FmtErasNarrow) 0 }}
   145		erasNarrow:   {{ .FmtErasNarrow }},
   146		{{- end -}}
   147		{{- if gt (len .FmtErasWide) 0 }}
   148		erasWide:   {{ .FmtErasWide }},
   149		{{- end }}
   150		timezones: {{ .FmtTimezones }},
   151	}
   152}
   153
   154// Locale returns the current translators string locale
   155func({{ .BaseLocale }} *{{ .Locale }}) Locale() string {
   156	return {{ .BaseLocale }}.locale
   157}
   158
   159// PluralsCardinal returns the list of cardinal plural rules associated with '{{ .Locale }}'
   160func({{ .BaseLocale }} *{{ .Locale }}) PluralsCardinal() []locales.PluralRule {
   161	return {{ .BaseLocale }}.pluralsCardinal
   162}
   163
   164// PluralsOrdinal returns the list of ordinal plural rules associated with '{{ .Locale }}'
   165func({{ .BaseLocale }} *{{ .Locale }}) PluralsOrdinal() []locales.PluralRule {
   166	return {{ .BaseLocale }}.pluralsOrdinal
   167}
   168
   169// PluralsRange returns the list of range plural rules associated with '{{ .Locale }}'
   170func({{ .BaseLocale }} *{{ .Locale }}) PluralsRange() []locales.PluralRule {
   171	return {{ .BaseLocale }}.pluralsRange
   172}
   173
   174// CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for '{{ .Locale }}'
   175func({{ .BaseLocale }} *{{ .Locale }}) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
   176	{{ .CardinalFunc }}
   177}
   178
   179// OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for '{{ .Locale }}'
   180func({{ .BaseLocale }} *{{ .Locale }}) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
   181	{{ .OrdinalFunc }}
   182}
   183
   184// RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for '{{ .Locale }}'
   185func({{ .BaseLocale }} *{{ .Locale }}) RangePluralRule(num1 float64, v1 uint64,num2 float64, v2 uint64) locales.PluralRule {
   186	{{ .RangeFunc }}
   187}
   188
   189// MonthAbbreviated returns the locales abbreviated month given the 'month' provided
   190func({{ .BaseLocale }} *{{ .Locale }}) MonthAbbreviated(month time.Month) string {
   191	return {{ .BaseLocale }}.monthsAbbreviated[month]
   192}
   193
   194// MonthsAbbreviated returns the locales abbreviated months
   195func({{ .BaseLocale }} *{{ .Locale }}) MonthsAbbreviated() []string {
   196	{{- if gt (len .FmtMonthsAbbreviated) 0 }}
   197	return {{ .BaseLocale }}.monthsAbbreviated[1:]
   198	{{ else }}
   199	return nil
   200	{{- end -}}
   201}
   202
   203// MonthNarrow returns the locales narrow month given the 'month' provided
   204func({{ .BaseLocale }} *{{ .Locale }}) MonthNarrow(month time.Month) string {
   205	return {{ .BaseLocale }}.monthsNarrow[month]
   206}
   207
   208// MonthsNarrow returns the locales narrow months
   209func({{ .BaseLocale }} *{{ .Locale }}) MonthsNarrow() []string {
   210	{{- if gt (len .FmtMonthsNarrow) 0 }}
   211	return {{ .BaseLocale }}.monthsNarrow[1:]
   212	{{ else }}
   213	return nil
   214	{{- end -}}
   215}
   216
   217// MonthWide returns the locales wide month given the 'month' provided
   218func({{ .BaseLocale }} *{{ .Locale }}) MonthWide(month time.Month) string {
   219	return {{ .BaseLocale }}.monthsWide[month]
   220}
   221
   222// MonthsWide returns the locales wide months
   223func({{ .BaseLocale }} *{{ .Locale }}) MonthsWide() []string {
   224	{{- if gt (len .FmtMonthsWide) 0 }}
   225	return {{ .BaseLocale }}.monthsWide[1:]
   226	{{ else }}
   227	return nil
   228	{{- end -}}
   229}
   230
   231// WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
   232func({{ .BaseLocale }} *{{ .Locale }}) WeekdayAbbreviated(weekday time.Weekday) string {
   233	return {{ .BaseLocale }}.daysAbbreviated[weekday]
   234}
   235
   236// WeekdaysAbbreviated returns the locales abbreviated weekdays
   237func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysAbbreviated() []string {
   238	return {{ .BaseLocale }}.daysAbbreviated
   239}
   240
   241// WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
   242func({{ .BaseLocale }} *{{ .Locale }}) WeekdayNarrow(weekday time.Weekday) string {
   243	return {{ .BaseLocale }}.daysNarrow[weekday]
   244}
   245
   246// WeekdaysNarrow returns the locales narrow weekdays
   247func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysNarrow() []string {
   248	return {{ .BaseLocale }}.daysNarrow
   249}
   250
   251// WeekdayShort returns the locales short weekday given the 'weekday' provided
   252func({{ .BaseLocale }} *{{ .Locale }}) WeekdayShort(weekday time.Weekday) string {
   253	return {{ .BaseLocale }}.daysShort[weekday]
   254}
   255
   256// WeekdaysShort returns the locales short weekdays
   257func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysShort() []string {
   258	return {{ .BaseLocale }}.daysShort
   259}
   260
   261// WeekdayWide returns the locales wide weekday given the 'weekday' provided
   262func({{ .BaseLocale }} *{{ .Locale }}) WeekdayWide(weekday time.Weekday) string {
   263	return {{ .BaseLocale }}.daysWide[weekday]
   264}
   265
   266// WeekdaysWide returns the locales wide weekdays
   267func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysWide() []string {
   268	return {{ .BaseLocale }}.daysWide
   269}
   270
   271// Decimal returns the decimal point of number
   272func({{ .BaseLocale }} *{{ .Locale }}) Decimal() string {
   273	return {{ .BaseLocale }}.decimal
   274}
   275
   276// Group returns the group of number
   277func({{ .BaseLocale }} *{{ .Locale }}) Group() string {
   278	return {{ .BaseLocale }}.group
   279}
   280
   281// Group returns the minus sign of number
   282func({{ .BaseLocale }} *{{ .Locale }}) Minus() string {
   283	return {{ .BaseLocale }}.minus
   284}
   285
   286// FmtNumber returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
   287func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) string {
   288
   289	{{ if eq .FmtNumberExists true }}
   290	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   291	{{- if gt .FmtNumberGroupLen 0 }}
   292	{{- $byteCountGroup := byte_count .Group -}}
   293	{{ if ne $byteCountGroup "0" }}
   294	l := len(s) + {{ byte_count .Decimal .Minus }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtNumberGroupLen }}
   295	{{ else }}
   296	l := len(s) + {{ byte_count .Decimal .Minus }}
   297	{{ end -}}
   298	count := 0
   299	inWhole := v == 0
   300	{{- if gt .FmtNumberSecondaryGroupLen 0}}
   301	inSecondary := false
   302	groupThreshold := {{ .FmtNumberGroupLen }}
   303	{{ end -}}
   304	{{ else }}
   305	l := len(s) + {{ byte_count .Decimal .Minus }}
   306	{{ end }}
   307	b := make([]byte, 0, l)
   308
   309	for i := len(s) - 1; i >= 0; i-- {
   310
   311		if s[i] == '.' {
   312
   313			{{- if is_multibyte .Decimal }}
   314			for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
   315				b = append(b, {{ .BaseLocale }}.decimal[j])
   316			}
   317			{{- else }}
   318			b = append(b, {{ .BaseLocale }}.decimal[0])
   319			{{- end -}}
   320			{{- if gt .FmtNumberGroupLen 0 }}
   321			inWhole = true
   322			{{- end }}
   323			continue
   324		}
   325
   326		{{ if gt .FmtNumberGroupLen 0 }}
   327		if inWhole {
   328
   329			{{- if gt .FmtNumberSecondaryGroupLen 0}}
   330			
   331			if count == groupThreshold {
   332				{{- if is_multibyte .Group }}
   333				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   334					b = append(b, {{ .BaseLocale }}.group[j])
   335				}
   336				{{- else }}
   337				b = append(b, {{ .BaseLocale }}.group[0])
   338				{{- end }}
   339				count = 1
   340
   341				if !inSecondary {
   342					inSecondary = true
   343					groupThreshold = {{ .FmtNumberSecondaryGroupLen }}
   344				}
   345			{{ else }}
   346			if count == {{ .FmtNumberGroupLen }} {
   347				{{- if is_multibyte .Group }}
   348				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   349					b = append(b, {{ .BaseLocale }}.group[j])
   350				}
   351				{{- else }}
   352				b = append(b, {{ .BaseLocale }}.group[0])
   353				{{- end }}
   354				count = 1
   355			{{ end -}}
   356			} else {
   357				count++
   358			}
   359		}
   360
   361		{{ end }}
   362
   363		b = append(b, s[i])
   364	}
   365
   366	if num < 0 {
   367		{{- if is_multibyte .Minus }}
   368		for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
   369			b = append(b, {{ .BaseLocale }}.minus[j])
   370		}
   371		{{ else }}
   372		b = append(b, {{ .BaseLocale }}.minus[0])
   373		{{ end -}}
   374	}
   375
   376	// reverse
   377	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   378		b[i], b[j] = b[j], b[i]
   379	}
   380
   381	{{ if gt .FmtNumberMinDecimalLen 0 }}
   382	if int(v) < {{ .FmtNumberMinDecimalLen }} {
   383
   384		if v == 0 {
   385			b = append(b, {{ .BaseLocale }}.decimal...)
   386		}
   387
   388		for i := 0; i < {{ .FmtNumberMinDecimalLen }}-int(v); i++ {
   389			b = append(b, '0')
   390		}
   391	}
   392	{{ end }}
   393
   394	return string(b)
   395	{{ else }}
   396	return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   397	{{ end -}}
   398}
   399
   400// FmtPercent returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
   401// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
   402func({{ .BaseLocale }} *{{ .Locale }}) FmtPercent(num float64, v uint64) string {
   403
   404	{{- if eq .FmtPercentExists true }}
   405	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   406	{{- if gt .FmtPercentGroupLen 0 }}
   407	{{- $byteCountGroup := byte_count .Group -}}
   408	{{ if ne $byteCountGroup "0" }}
   409	l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtPercentGroupLen }}
   410	{{ else }}
   411	l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }}
   412	{{ end -}}
   413	count := 0
   414	inWhole := v == 0
   415	{{- if gt .FmtPercentSecondaryGroupLen 0}}
   416	inSecondary := false
   417	groupThreshold := {{ .FmtPercentGroupLen }}
   418	{{ end -}}
   419	{{ else }}
   420	l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }}
   421	{{- end }}
   422	b := make([]byte, 0, l)
   423
   424	for i := len(s) - 1; i >= 0; i-- {
   425
   426		if s[i] == '.' {
   427
   428			{{- if is_multibyte .Decimal }}
   429			for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
   430				b = append(b, {{ .BaseLocale }}.decimal[j])
   431			}
   432			{{- else }}
   433			b = append(b, {{ .BaseLocale }}.decimal[0])
   434			{{- end -}}
   435			{{- if gt .FmtPercentGroupLen 0 }}
   436			inWhole = true
   437			{{ end }}
   438			continue
   439		}
   440
   441		{{ if gt .FmtPercentGroupLen 0 }}
   442		if inWhole {
   443
   444			{{- if gt .FmtPercentSecondaryGroupLen 0}}
   445			
   446			if count == groupThreshold {
   447				{{- if is_multibyte .Group }}
   448				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   449					b = append(b, {{ .BaseLocale }}.group[j])
   450				}
   451				{{- else }}
   452				b = append(b, {{ .BaseLocale }}.group[0])
   453				{{- end }}
   454				count = 1
   455
   456				if !inSecondary {
   457					inSecondary = true
   458					groupThreshold = {{ .FmtPercentSecondaryGroupLen }}
   459				}
   460			{{ else }}
   461			if count == {{ .FmtPercentGroupLen }} {
   462				{{- if is_multibyte .Group }}
   463				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   464					b = append(b, {{ .BaseLocale }}.group[j])
   465				}
   466				{{- else }}
   467				b = append(b, {{ .BaseLocale }}.group[0])
   468				{{- end }}
   469				count = 1
   470			{{ end -}}
   471			} else {
   472				count++
   473			}
   474		}
   475
   476		{{ end }}
   477
   478		b = append(b, s[i])
   479	}
   480
   481	if num < 0 {
   482		{{- if is_multibyte .Minus }}
   483		for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
   484			b = append(b, {{ .BaseLocale }}.minus[j])
   485		}
   486		{{ else }}
   487		b = append(b, {{ .BaseLocale }}.minus[0])
   488		{{ end -}}
   489	}
   490
   491	{{ if and .FmtPercentInPrefix (not .FmtPercentLeft) }}
   492		{{- if is_multibyte .Percent }}
   493		for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
   494			b = append(b, {{ .BaseLocale }}.percent[j])
   495		}
   496		{{ else }}
   497		b = append(b, {{ .BaseLocale }}.percent[0])
   498		{{ end }}
   499	{{ end }}
   500
   501	{{ if gt (len .FmtPercentPrefix) 0}}
   502		{{- if is_multibyte .FmtPercentPrefix }}
   503		for j := len({{ .BaseLocale }}.percentPrefix) - 1; j >= 0; j-- {
   504			b = append(b, {{ .BaseLocale }}.percentPrefix[j])
   505		}
   506		{{ else }}
   507		b = append(b, {{ .BaseLocale }}.percentPrefix[0])
   508		{{ end }}
   509	{{ end }}
   510
   511	{{ if and .FmtPercentInPrefix .FmtPercentLeft }}
   512		{{- if is_multibyte .Percent }}
   513		for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
   514			b = append(b, {{ .BaseLocale }}.percent[j])
   515		}
   516		{{ else }}
   517		b = append(b, {{ .BaseLocale }}.percent[0])
   518		{{ end }}
   519	{{ end }}
   520
   521	// reverse
   522	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   523		b[i], b[j] = b[j], b[i]
   524	}
   525
   526	{{ if gt .FmtPercentMinDecimalLen 0 }}
   527	if int(v) < {{ .FmtPercentMinDecimalLen }} {
   528
   529		if v == 0 {
   530			b = append(b, {{ .BaseLocale }}.decimal...)
   531		}
   532
   533		for i := 0; i < {{ .FmtPercentMinDecimalLen }}-int(v); i++ {
   534			b = append(b, '0')
   535		}
   536	}
   537	{{ end }}
   538
   539	{{ if and (not .FmtPercentInPrefix) .FmtPercentLeft }}
   540		b = append(b, {{ .BaseLocale }}.percent...)
   541	{{ end }}
   542
   543	{{ if gt (len .FmtPercentSuffix) 0}}
   544		b = append(b, {{ .BaseLocale }}.percentSuffix...)
   545	{{ end }}
   546
   547	{{ if and (not .FmtPercentInPrefix) (not .FmtPercentLeft) }}
   548		b = append(b, {{ .BaseLocale }}.percent...)
   549	{{ end }}
   550
   551	return string(b)
   552	{{ else }}
   553	return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   554	{{ end -}}
   555}
   556
   557// FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
   558func({{ .BaseLocale }} *{{ .Locale }}) FmtCurrency(num float64, v uint64, currency currency.Type) string {
   559
   560	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   561	symbol := {{ .BaseLocale }}.currencies[currency]
   562	{{- if eq .FmtCurrencyExists true }}
   563	{{- if gt .FmtCurrencyGroupLen 0 }}
   564	{{- $byteCountGroup := byte_count .Group -}}
   565	{{ if ne $byteCountGroup "0" }}
   566	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
   567	{{ else }}
   568	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }}
   569	{{ end -}}
   570	count := 0
   571	inWhole := v == 0
   572	{{- if gt .FmtCurrencySecondaryGroupLen 0}}
   573	inSecondary := false
   574	groupThreshold := {{ .FmtCurrencyGroupLen }}
   575	{{ end -}}
   576	{{ else }}
   577	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }}
   578	{{ end }}
   579	b := make([]byte, 0, l)
   580
   581	for i := len(s) - 1; i >= 0; i-- {
   582
   583		if s[i] == '.' {
   584
   585			{{- if is_multibyte .Decimal }}
   586			for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
   587				b = append(b, {{ .BaseLocale }}.decimal[j])
   588			}
   589			{{- else }}
   590			b = append(b, {{ .BaseLocale }}.decimal[0])
   591			{{- end -}}
   592			{{- if gt .FmtCurrencyGroupLen 0 }}
   593			inWhole = true
   594			{{- end }}
   595			continue
   596		}
   597
   598		{{ if gt .FmtCurrencyGroupLen 0 }}
   599		if inWhole {
   600
   601			{{- if gt .FmtCurrencySecondaryGroupLen 0}}
   602			
   603			if count == groupThreshold {
   604				{{- if is_multibyte .Group }}
   605				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   606					b = append(b, {{ .BaseLocale }}.group[j])
   607				}
   608				{{- else }}
   609				b = append(b, {{ .BaseLocale }}.group[0])
   610				{{- end }}
   611				count = 1
   612
   613				if !inSecondary {
   614					inSecondary = true
   615					groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
   616				}
   617			{{ else }}
   618			if count == {{ .FmtCurrencyGroupLen }} {
   619				{{- if is_multibyte .Group }}
   620				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   621					b = append(b, {{ .BaseLocale }}.group[j])
   622				}
   623				{{- else }}
   624				b = append(b, {{ .BaseLocale }}.group[0])
   625				{{- end }}
   626				count = 1
   627			{{ end -}}
   628			} else {
   629				count++
   630			}
   631		}
   632
   633		{{ end }}
   634
   635		b = append(b, s[i])
   636	}
   637
   638	{{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
   639		for j := len(symbol) - 1; j >= 0; j-- {
   640			b = append(b, symbol[j])
   641		}
   642	{{ end }}
   643
   644	{{ if gt (len .FmtCurrencyPrefix) 0}}
   645		{{- if is_multibyte .FmtCurrencyPrefix }}
   646		for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
   647			b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
   648		}
   649		{{ else }}
   650		b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
   651		{{ end }}
   652	{{ end }}
   653
   654	{{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
   655		for j := len(symbol) - 1; j >= 0; j-- {
   656			b = append(b, symbol[j])
   657		}
   658	{{ end }}
   659
   660	if num < 0 {
   661		{{- if is_multibyte .Minus }}
   662		for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
   663			b = append(b, {{ .BaseLocale }}.minus[j])
   664		}
   665		{{ else -}}
   666		b = append(b, {{ .BaseLocale }}.minus[0])
   667		{{ end -}}
   668	}
   669
   670	// reverse
   671	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   672		b[i], b[j] = b[j], b[i]
   673	}
   674
   675	{{ if gt .FmtCurrencyMinDecimalLen 0 }}
   676	if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
   677
   678		if v == 0 {
   679			b = append(b, {{ .BaseLocale }}.decimal...)
   680		}
   681
   682		for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
   683			b = append(b, '0')
   684		}
   685	}
   686	{{ end }}
   687
   688	{{ if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
   689		b = append(b, symbol...)
   690	{{ end }}
   691
   692	{{ if gt (len .FmtCurrencySuffix) 0}}
   693		b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
   694	{{ end }}
   695
   696	{{ if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
   697		b = append(b, symbol...)
   698	{{ end }}
   699
   700	return string(b)
   701	{{ else }}
   702	return string(append(append([]byte{}, symbol...), s...))
   703	{{ end -}}
   704}
   705
   706// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
   707// in accounting notation.
   708func({{ .BaseLocale }} *{{ .Locale }}) FmtAccounting(num float64, v uint64, currency currency.Type) string {
   709
   710	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   711	symbol := {{ .BaseLocale }}.currencies[currency]
   712	{{- if eq .FmtCurrencyExists true }}
   713	{{- if gt .FmtCurrencyGroupLen 0 }}
   714	{{- $byteCountGroup := byte_count .Group -}}
   715	{{ if ne $byteCountGroup "0" }}
   716	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
   717	{{ else }}
   718	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }}
   719	{{ end -}}
   720	count := 0
   721	inWhole := v == 0
   722	{{- if gt .FmtCurrencySecondaryGroupLen 0}}
   723	inSecondary := false
   724	groupThreshold := {{ .FmtCurrencyGroupLen }}
   725	{{ end -}}
   726	{{ else }}
   727	l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }}
   728	{{ end }}
   729	b := make([]byte, 0, l)
   730
   731	for i := len(s) - 1; i >= 0; i-- {
   732
   733		if s[i] == '.' {
   734
   735			{{- if is_multibyte .Decimal }}
   736			for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
   737				b = append(b, {{ .BaseLocale }}.decimal[j])
   738			}
   739			{{- else }}
   740			b = append(b, {{ .BaseLocale }}.decimal[0])
   741			{{- end -}}
   742			{{- if gt .FmtCurrencyGroupLen 0 }}
   743			inWhole = true
   744			{{- end }}
   745			continue
   746		}
   747
   748		{{ if gt .FmtCurrencyGroupLen 0 }}
   749		if inWhole {
   750
   751			{{- if gt .FmtCurrencySecondaryGroupLen 0}}
   752			
   753			if count == groupThreshold {
   754				{{- if is_multibyte .Group }}
   755				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   756					b = append(b, {{ .BaseLocale }}.group[j])
   757				}
   758				{{- else }}
   759				b = append(b, {{ .BaseLocale }}.group[0])
   760				{{- end }}
   761				count = 1
   762
   763				if !inSecondary {
   764					inSecondary = true
   765					groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
   766				}
   767			{{ else }}
   768			if count == {{ .FmtCurrencyGroupLen }} {
   769				{{- if is_multibyte .Group }}
   770				for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
   771					b = append(b, {{ .BaseLocale }}.group[j])
   772				}
   773				{{- else }}
   774				b = append(b, {{ .BaseLocale }}.group[0])
   775				{{- end }}
   776				count = 1
   777			{{ end -}}
   778			} else {
   779				count++
   780			}
   781		}
   782
   783		{{ end }}
   784
   785		b = append(b, s[i])
   786	}
   787
   788	if num < 0 {
   789
   790		{{ if and .FmtCurrencyNegativeInPrefix (not .FmtCurrencyNegativeLeft) }}
   791		for j := len(symbol) - 1; j >= 0; j-- {
   792			b = append(b, symbol[j])
   793		}
   794		{{ end }}
   795
   796		{{ if gt (len .FmtCurrencyNegativePrefix) 0}}
   797			{{- if is_multibyte .FmtCurrencyNegativePrefix }}
   798			for j := len({{ .BaseLocale }}.currencyNegativePrefix) - 1; j >= 0; j-- {
   799				b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[j])
   800			}
   801			{{ else }}
   802			b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[0])
   803			{{ end }}
   804		{{ end }}
   805
   806		{{ if and .FmtCurrencyNegativeInPrefix .FmtCurrencyNegativeLeft }}
   807			for j := len(symbol) - 1; j >= 0; j-- {
   808				b = append(b, symbol[j])
   809			}
   810		{{ end }}
   811
   812		{{ if eq (not .FmtCurrencyNegativeExists) true}}
   813			{{- if is_multibyte .Minus }}
   814			for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
   815				b = append(b, {{ .BaseLocale }}.minus[j])
   816			}
   817			{{ else -}}
   818			b = append(b, {{ .BaseLocale }}.minus[0])
   819			{{ end -}}
   820		{{ end }}
   821
   822	{{ if or .FmtCurrencyInPrefix (gt (len .FmtCurrencyPrefix) 0) }}
   823	} else {
   824	{{ end }}
   825		
   826		{{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
   827		for j := len(symbol) - 1; j >= 0; j-- {
   828			b = append(b, symbol[j])
   829		}
   830		{{ end }}
   831
   832		{{ if gt (len .FmtCurrencyPrefix) 0}}
   833			{{- if is_multibyte .FmtCurrencyPrefix }}
   834			for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
   835				b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
   836			}
   837			{{ else }}
   838			b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
   839			{{ end }}
   840		{{ end }}
   841
   842		{{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
   843			for j := len(symbol) - 1; j >= 0; j-- {
   844				b = append(b, symbol[j])
   845			}
   846		{{- end }}
   847	}
   848
   849	// reverse
   850	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   851		b[i], b[j] = b[j], b[i]
   852	}
   853
   854	{{ if gt .FmtCurrencyMinDecimalLen 0 }}
   855	if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
   856
   857		if v == 0 {
   858			b = append(b, {{ .BaseLocale }}.decimal...)
   859		}
   860
   861		for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
   862			b = append(b, '0')
   863		}
   864	}
   865	{{- end }}
   866
   867	{{ if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
   868	if num < 0 {
   869	{{- end }}
   870		{{- if and (not .FmtCurrencyNegativeInPrefix) .FmtCurrencyNegativeLeft }}
   871			b = append(b, symbol...)
   872		{{- end -}}
   873
   874		{{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
   875			b = append(b, {{ .BaseLocale }}.currencyNegativeSuffix...)
   876		{{- end -}}
   877
   878		{{- if and (not .FmtCurrencyNegativeInPrefix) (not .FmtCurrencyNegativeLeft) }}
   879			b = append(b, symbol...)
   880		{{- end -}}
   881	{{ if or (not .FmtCurrencyInPrefix) (gt (len .FmtCurrencySuffix) 0)}}
   882	} else {
   883	{{ end }}
   884		{{- if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
   885			b = append(b, symbol...)
   886		{{- end -}}
   887
   888		{{- if gt (len .FmtCurrencySuffix) 0}}
   889			b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
   890		{{- end -}}
   891
   892		{{- if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
   893			b = append(b, symbol...)
   894		{{- end -}}
   895	{{- if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
   896	}
   897	{{- end }}
   898
   899	return string(b)
   900	{{ else }}
   901	return string(append(append([]byte{}, symbol...), s...))
   902	{{ end -}}
   903}
   904
   905// FmtDateShort returns the short date representation of 't' for '{{ .Locale }}'
   906func({{ .BaseLocale }} *{{ .Locale }}) FmtDateShort(t time.Time) string {
   907	
   908	b := make([]byte, 0, 32)
   909
   910	{{ .FmtDateShort }}
   911
   912	return string(b)
   913}
   914
   915// FmtDateMedium returns the medium date representation of 't' for '{{ .Locale }}'
   916func({{ .BaseLocale }} *{{ .Locale }}) FmtDateMedium(t time.Time) string {
   917	
   918	b := make([]byte, 0, 32)
   919
   920	{{ .FmtDateMedium }}
   921
   922	return string(b)
   923}
   924
   925// FmtDateLong returns the long date representation of 't' for '{{ .Locale }}'
   926func({{ .BaseLocale }} *{{ .Locale }}) FmtDateLong(t time.Time) string {
   927	
   928	b := make([]byte, 0, 32)
   929
   930	{{ .FmtDateLong }}
   931
   932	return string(b)
   933}
   934
   935// FmtDateFull returns the full date representation of 't' for '{{ .Locale }}'
   936func({{ .BaseLocale }} *{{ .Locale }}) FmtDateFull(t time.Time) string {
   937	
   938	b := make([]byte, 0, 32)
   939
   940	{{ .FmtDateFull }}
   941
   942	return string(b)
   943}
   944
   945// FmtTimeShort returns the short time representation of 't' for '{{ .Locale }}'
   946func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeShort(t time.Time) string {
   947	
   948	b := make([]byte, 0, 32)
   949
   950	{{ .FmtTimeShort }}
   951
   952	return string(b)
   953}
   954
   955// FmtTimeMedium returns the medium time representation of 't' for '{{ .Locale }}'
   956func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeMedium(t time.Time) string {
   957	
   958	b := make([]byte, 0, 32)
   959
   960	{{ .FmtTimeMedium }}
   961
   962	return string(b)
   963}
   964
   965// FmtTimeLong returns the long time representation of 't' for '{{ .Locale }}'
   966func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeLong(t time.Time) string {
   967	
   968	b := make([]byte, 0, 32)
   969
   970	{{ .FmtTimeLong }}
   971
   972	return string(b)
   973}
   974
   975// FmtTimeFull returns the full time representation of 't' for '{{ .Locale }}'
   976func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeFull(t time.Time) string {
   977	
   978	b := make([]byte, 0, 32)
   979
   980	{{ .FmtTimeFull }}
   981
   982	return string(b)
   983}
   984
   985{{ end }}

View as plain text