From 7b1e17569e737224be86f8de4af9a80252fa0735 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 24 Jul 2023 17:09:43 +0200 Subject: [PATCH] Add Gettext and GettextNoop to locale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xgettext does not recognize Get as marker for translatable strings, and i can not add it as is because Go has many functions called Get, and it would mark many strings as translatable that should not be, like HTTP headers. I believe that Gettext is unusual in Go, because the “correct” way to spell that function in Go would be GetText, and would not interfere in any other function. GettextNoop is just a function that marks the string as translatable but does not translate it at all. I want this for error messages and such, that i have to pass the string to a validator function, but it is only necessary to translate it when the validation fails. I doubt it makes a difference in peformance, but still. --- Makefile | 2 +- pkg/locale/locale.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 379226d..f21beaa 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ po/%.po: $(POT_FILE) $(POT_FILE): $(HTML_FILES) $(GO_FILES) xgettext $(XGETTEXTFLAGS) --language=Scheme --output=$@ --keyword=pgettext:1,2c $(HTML_FILES) - xgettext $(XGETTEXTFLAGS) --language=C --output=$@ --join-existing $(GO_FILES) + xgettext $(XGETTEXTFLAGS) --language=C --output=$@ --keyword=Gettext:1 --keyword=GettextNoop:1 --join-existing $(GO_FILES) test-deploy: sqitch deploy --db-name $(PGDATABASE) diff --git a/pkg/locale/locale.go b/pkg/locale/locale.go index 7ef458e..5d96797 100644 --- a/pkg/locale/locale.go +++ b/pkg/locale/locale.go @@ -52,6 +52,14 @@ func newLocale(lang availableLanguage) *Locale { } } +func (l *Locale) Gettext(str string) string { + return l.GetD(l.GetDomain(), str) +} + +func (l *Locale) GettextNoop(str string) string { + return str +} + func Match(r *http.Request, locales Locales, defaultLocale *Locale, matcher language.Matcher) *Locale { var locale *Locale // TODO: find user locale