From 7cfdfd8bdfe4f8d7bf74348817f2c046412bd124 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 1 Jan 2025 11:04:20 +0100 Subject: Change the semantics of Printer.M() --- src/i18n.go | 41 ++++++++++++++++++++++-------- src/templates/coins-designs-ad.html.tmpl | 16 ++++++------ src/templates/coins-mintages.html.tmpl | 4 +-- src/templates/collecting-vending.html.tmpl | 4 +-- 4 files changed, 43 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/i18n.go b/src/i18n.go index 0ebcedc..017dbf7 100644 --- a/src/i18n.go +++ b/src/i18n.go @@ -5,11 +5,14 @@ package src import ( "fmt" + "log" "strings" "time" "golang.org/x/text/language" "golang.org/x/text/message" + + "git.thomasvoss.com/euro-cash.eu/src/email" ) type Printer struct { @@ -235,28 +238,36 @@ func (p Printer) Date(d time.Time) string { return d.Format(p.Locale.dateFmt) } -func (p Printer) M(val float64, round bool) string { - var valstr string +func (p Printer) M(val any) string { + var vstr string /* Hack to avoid gotext writing these two ‘translations’ into the translations file */ f := p.inner.Sprintf - if round { - valstr = f("%d", int(val)) - } else { - valstr = f("%.2f", val) + + switch val.(type) { + case int: + vstr = f("%d", val) + case float64: + vstr = f("%.2f", val) + default: + if err := email.ServerError(badMType{"TODO"}); err != nil { + log.Print(err) + } + /* Hopefully this never happens */ + vstr = "ERROR" } /* All Eurozone languages place the eurosign after the value except - for Dutch, English, Gaelic, and Maltese. Austrian German also + for Dutch, English, Irish, and Maltese. Austrian German also uses Dutch-style formatting, but we do not support that dialect. */ switch p.Locale.Bcp { case "en", "en-US", "ga", "mt": - return fmt.Sprintf("€%s", valstr) + return fmt.Sprintf("€%s", vstr) case "nl": - return fmt.Sprintf("€ %s", valstr) + return fmt.Sprintf("€ %s", vstr) default: - return fmt.Sprintf("%s €", valstr) + return fmt.Sprintf("%s €", vstr) } } @@ -264,3 +275,13 @@ func (p Printer) M(val float64, round bool) string { func (l locale) Language() string { return l.Bcp[:2] } + +type badMType struct { + inner any +} + +func (e badMType) Error() string { + return fmt.Sprintf( + "Attempted to format ‘%v’ of type ‘%T’ as a monetary value in Printer.M()", + e.inner, e.inner) +} diff --git a/src/templates/coins-designs-ad.html.tmpl b/src/templates/coins-designs-ad.html.tmpl index 1effaeb..c42930a 100644 --- a/src/templates/coins-designs-ad.html.tmpl +++ b/src/templates/coins-designs-ad.html.tmpl @@ -23,16 +23,16 @@

{{ .T "%s, %s, and %s" - (.Printer.M 0.01 false) - (.Printer.M 0.02 false) - (.Printer.M 0.05 false) }}
+ (.Printer.M 0.01) + (.Printer.M 0.02) + (.Printer.M 0.05) }}
{{ .T "Andorran landscapes, nature, fauna, and flora" }}
{{ .T "%s, %s, and %s" - (.Printer.M 0.10 false) - (.Printer.M 0.20 false) - (.Printer.M 0.50 false) }}
+ (.Printer.M 0.10) + (.Printer.M 0.20) + (.Printer.M 0.50) }}
{{ .T "Andorra’s Romanesque art" }}
-
{{ .Printer.M 1.00 false }}
+
{{ .Printer.M 1.00 }}
{{ .T "Casa de la Vall" }}

@@ -88,4 +88,4 @@ ` `` `` }}

-{{ end }} +{{ end }} \ No newline at end of file diff --git a/src/templates/coins-mintages.html.tmpl b/src/templates/coins-mintages.html.tmpl index 4ac29e8..8c60248 100644 --- a/src/templates/coins-mintages.html.tmpl +++ b/src/templates/coins-mintages.html.tmpl @@ -73,7 +73,7 @@ {{ .T "Year" }} {{ with $p := .Printer }} {{ range denoms }} - {{ $p.M . false }} + {{ $p.M . }} {{ end }} {{ end }} @@ -157,4 +157,4 @@ /> {{ index . 2 }} -{{ end }} +{{ end }} \ No newline at end of file diff --git a/src/templates/collecting-vending.html.tmpl b/src/templates/collecting-vending.html.tmpl index 5fcfd0c..2bfea22 100644 --- a/src/templates/collecting-vending.html.tmpl +++ b/src/templates/collecting-vending.html.tmpl @@ -107,7 +107,7 @@ if you throw in five euros. You can try to go above the limit if you throw in, say, %s and then another one- or two euro coin; the machine will probably accept it. - ` (.Printer.M 4.80 false) }} + ` (.Printer.M 4.80) }}
{{ .T "Maximum Change Limit" }}
@@ -150,7 +150,7 @@ something to purchase, throw in less money than the cost, and then cancel the purchase. Note that most cigarette machines in Austria have a %s max change limit. - ` (.Printer.M 4.90 false) }} + ` (.Printer.M 4.90) }}

{{ .T ` -- cgit v1.2.3