Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/locales.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/translator.tmpl')
-rw-r--r--cmd/translator.tmpl132
1 files changed, 132 insertions, 0 deletions
diff --git a/cmd/translator.tmpl b/cmd/translator.tmpl
index 96a75bea..623adb5d 100644
--- a/cmd/translator.tmpl
+++ b/cmd/translator.tmpl
@@ -175,4 +175,136 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte {
{{ end -}}
}
+// FmtPercent returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
+// returned as a []byte just in case the caller wishes to add more and can help
+// avoid allocations; otherwise just cast as string.
+// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
+func({{ .BaseLocale }} *{{ .Locale }}) FmtPercent(num float64, v uint64) []byte {
+
+ s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
+ {{- if eq .FmtPercentExists true }}
+ {{- if gt .FmtPercentGroupLen 0 }}
+ l := len(s) + len({{ .BaseLocale }}.decimal) + len({{ .BaseLocale }}.group) * len(s[:len(s)-int(v)-1]) / {{ .FmtPercentGroupLen }}
+ count := 0
+ inWhole := v == 0
+ {{- if gt .FmtPercentSecondaryGroupLen 0}}
+ inSecondary := false
+ groupThreshold := {{ .FmtPercentGroupLen }}
+ {{ end -}}
+ {{ else }}
+ l := len(s) + len({{ .BaseLocale }}.decimal)
+ {{ end }}
+ b := make([]byte, 0, l)
+
+ for i := len(s) - 1; i >= 0; i-- {
+
+ if s[i] == '.' {
+
+ {{- if eq .DecimalLen 1 }}
+ b = append(b, {{ .BaseLocale }}.decimal[0])
+ {{- else }}
+ for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.decimal[j])
+ }
+ {{ end -}}
+ {{- if gt .FmtPercentGroupLen 0 }}
+ inWhole = true
+ {{ end }}
+ continue
+ }
+
+ {{ if gt .FmtPercentGroupLen 0 }}
+ if inWhole {
+
+ {{- if gt .FmtPercentSecondaryGroupLen 0}}
+
+ if count == groupThreshold {
+ {{- if eq .GroupLen 1 }}
+ b = append(b, {{ .BaseLocale }}.group[0])
+ {{- else }}
+ for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.group[j])
+ }
+ {{ end }}
+ count = 1
+
+ if !inSecondary {
+ inSecondary = true
+ groupThreshold = {{ .FmtPercentSecondaryGroupLen }}
+ }
+ {{ else }}
+ if count == {{ .FmtPercentGroupLen }} {
+ {{- if eq .GroupLen 1 }}
+ b = append(b, {{ .BaseLocale }}.group[0])
+ {{- else }}
+ for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.group[j])
+ }
+ {{ end }}
+ count = 1
+ {{ end -}}
+ } else {
+ count++
+ }
+ }
+
+ {{ end }}
+
+ b = append(b, s[i])
+ }
+
+ if num < 0 {
+ {{- if eq .MinusLen 1 }}
+ b = append(b, {{ .BaseLocale }}.minus[0])
+ {{ else }}
+ for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.minus[j])
+ }
+ {{ end -}}
+ }
+
+ {{ if eq .FmtPercentLeft true }}
+ {{- if eq .PercentLen 1 }}
+ b = append(b, {{ .BaseLocale }}.Percent[0])
+ {{ else }}
+ for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.percent[j])
+ }
+ {{ end }}
+ {{ end }}
+
+ // reverse
+ for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
+ b[i], b[j] = b[j], b[i]
+ }
+
+ {{ if gt .FmtPercentMinDecimalLen 0 }}
+ if int(v) < {{ .FmtPercentMinDecimalLen }} {
+
+ if v == 0 {
+ b = append(b, {{ .BaseLocale }}.decimal...)
+ }
+
+ for i := 0; i < {{ .FmtPercentMinDecimalLen }}-int(v); i++ {
+ b = append(b, '0')
+ }
+ }
+ {{ end }}
+
+ {{ if eq .FmtPercentLeft false }}
+ {{- if eq .PercentLen 1 }}
+ b = append(b, {{ .BaseLocale }}.Percent[0])
+ {{ else }}
+ for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
+ b = append(b, {{ .BaseLocale }}.percent[j])
+ }
+ {{ end }}
+ {{ end }}
+
+ return b
+ {{ else }}
+ return []byte(s)
+ {{ end -}}
+}
+
{{ end }} \ No newline at end of file