Add Gettext and GettextNoop to locale

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.
This commit is contained in:
jordi fita mas 2023-07-24 17:09:43 +02:00
parent 2300735030
commit 7b1e17569e
2 changed files with 9 additions and 1 deletions

View File

@ -17,7 +17,7 @@ po/%.po: $(POT_FILE)
$(POT_FILE): $(HTML_FILES) $(GO_FILES) $(POT_FILE): $(HTML_FILES) $(GO_FILES)
xgettext $(XGETTEXTFLAGS) --language=Scheme --output=$@ --keyword=pgettext:1,2c $(HTML_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: test-deploy:
sqitch deploy --db-name $(PGDATABASE) sqitch deploy --db-name $(PGDATABASE)

View File

@ -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 { func Match(r *http.Request, locales Locales, defaultLocale *Locale, matcher language.Matcher) *Locale {
var locale *Locale var locale *Locale
// TODO: find user locale // TODO: find user locale