[PATCH links] translations: fix error when translations default catalog was overwritten
Export this patch
Changelog-fixed: translations bug where catalog was overwritten and
translations failed to show Spanish
---
internal/localizer/localizer.go | 12 +++---------
internal/translations/appcat.go | 24 ++++++++++++++++++++++++
2 files changed, 27 insertions(+), 9 deletions(-)
create mode 100644 internal/translations/appcat.go
diff --git a/internal/localizer/localizer.go b/internal/localizer/localizer.go
index 62a268d..bb4c803 100644
--- a/internal/localizer/localizer.go
+++ b/internal/localizer/localizer.go
@@ -7,11 +7,7 @@ import (
"netlandish.com/x/gobwebs/core"
- // Import the internal/translations so that it's init() function
- // is run. It's really important that we do this here so that the
- // default message catalog is updated to use our translations
- // *before* we initialize the message.Printer instances below.
- _ "links/internal/translations"
+ trans "links/internal/translations"
"github.com/labstack/echo/v4"
"golang.org/x/text/language"
@@ -39,14 +35,12 @@ func (l *Localizer) Translate(key message.Reference, args ...any) string {
// catalog named Gatalog
var locales = []Localizer{
{
- // Spanish
ID: "es",
- printer: message.NewPrinter(language.MustParse("es")),
+ printer: message.NewPrinter(language.MustParse("es"), message.Catalog(trans.AppCatalog)),
},
{
- // English
ID: "en",
- printer: message.NewPrinter(language.MustParse("en")),
+ printer: message.NewPrinter(language.MustParse("en"), message.Catalog(trans.AppCatalog)),
},
}
diff --git a/internal/translations/appcat.go b/internal/translations/appcat.go
new file mode 100644
index 0000000..27cde5a
--- /dev/null
+++ b/internal/translations/appcat.go
@@ -0,0 +1,24 @@
+package translations
+
+import (
+ "golang.org/x/text/language"
+ "golang.org/x/text/message/catalog"
+)
+
+// AppCatalog is the application-specific translation catalog.
+// Exported so the localizer can bind printers explicitly,
+// avoiding DefaultCatalog races with gobwebs' catalog.
+var AppCatalog catalog.Catalog
+
+func init() {
+ dict := map[string]catalog.Dictionary{
+ "en": &dictionary{index: enIndex, data: enData},
+ "es": &dictionary{index: esIndex, data: esData},
+ }
+ fallback := language.MustParse("en")
+ cat, err := catalog.NewFromMap(dict, catalog.Fallback(fallback))
+ if err != nil {
+ panic(err)
+ }
+ AppCatalog = cat
+}
--
2.52.0
Applied.
To git@git.code.netlandish.com:~netlandish/links
90a1561..630dcb3 master -> master