From f9f30e96c7749499de59a0377083c2b42633ea95 Mon Sep 17 00:00:00 2001
From: Nicolai Ort
Date: Mon, 9 Dec 2024 17:15:06 +0100
Subject: [PATCH] refactor(templater): Format decimals according to locale
---
handlers/certificate.go | 1 +
models/certificate.go | 1 +
services/templater.go | 8 ++++++--
templates/certificate/de.html | 10 +++++-----
templates/certificate/en.html | 10 +++++-----
5 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/handlers/certificate.go b/handlers/certificate.go
index 0a7625e..439785a 100644
--- a/handlers/certificate.go
+++ b/handlers/certificate.go
@@ -51,6 +51,7 @@ func GenerateCertificate(c *fiber.Ctx) error {
EventName: "Event name",
Footer: "Footer",
CurrencySymbol: "€",
+ Locale: certificateRequest.Locale,
}
result, err := generator.Execute(template, genConfig)
diff --git a/models/certificate.go b/models/certificate.go
index b17004e..3fb3b08 100644
--- a/models/certificate.go
+++ b/models/certificate.go
@@ -37,4 +37,5 @@ type CertificateTemplateOptions struct {
EventName string `json:"event_name"`
Footer string `json:"footer"`
CurrencySymbol string `json:"currency_symbol"`
+ Locale string `json:"locale"`
}
diff --git a/services/templater.go b/services/templater.go
index c1178c4..72c85b9 100644
--- a/services/templater.go
+++ b/services/templater.go
@@ -91,7 +91,7 @@ func (t *DefaultTemplater) SelectSponsorImage(id int) (string, error) {
return templates.GetImage(sponsors[id%len(sponsors)]), nil
}
-func FormatUnit(unit string, amount int) (string, error) {
+func FormatUnit(unit string, locale string, amount int) (string, error) {
var formatted string
switch unit {
case "kilometer":
@@ -101,7 +101,11 @@ func FormatUnit(unit string, amount int) (string, error) {
default:
return "", errors.New("unknown unit")
}
- return strings.Replace(formatted, ".", ",", -1), nil
+
+ if locale == "de" {
+ return strings.Replace(formatted, ".", ",", -1), nil
+ }
+ return formatted, nil
}
func (t *DefaultTemplater) StringToTemplate(templateString string) (*template.Template, error) {
diff --git a/templates/certificate/de.html b/templates/certificate/de.html
index 2a2b517..7ff02bd 100644
--- a/templates/certificate/de.html
+++ b/templates/certificate/de.html
@@ -56,7 +56,7 @@
{{ .MiddleName }} {{ .LastName }}
hat beim {{ $.EventName }}
- {{formatUnit "kilometer" .Distance}}km
+ {{formatUnit "kilometer" $.Locale .Distance}}km
für den guten Zweck zurückgelegt