Skip to content

Commit 7262713

Browse files
committed
tpl/lang: Warn when using reserved translation keys
When using a translation key that is reserved in the `go-i18n` package, Hugo will now log an error instead of silently failing. Fixes #14061
1 parent 9e344bb commit 7262713

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎tpl/lang/lang.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ import (
3030
"github.com/spf13/cast"
3131
)
3232

33+
var reservedTranslationIDs = map[string]string{
34+
"id": "",
35+
"description": "",
36+
"hash": "",
37+
"leftdelim": "",
38+
"rightdelim": "",
39+
"zero": "",
40+
"one": "",
41+
"two": "",
42+
"few": "",
43+
"many": "",
44+
"other": "",
45+
}
46+
3347
// New returns a new instance of the lang-namespaced template functions.
3448
func New(deps *deps.Deps, translator locales.Translator) *Namespace {
3549
return &Namespace{
@@ -60,6 +74,12 @@ func (ns *Namespace) Translate(ctx context.Context, id any, args ...any) (string
6074
return "", err
6175
}
6276

77+
sidParts := strings.Split(sid, ".")
78+
translationKey := sidParts[len(sidParts)-1]
79+
if _, found := reservedTranslationIDs[translationKey]; found {
80+
return "", fmt.Errorf("the key %q is reserved and cannot be used for translation lookups; please consult the lang.Translate documentation for a list of reserved keys", translationKey)
81+
}
82+
6383
return ns.deps.Translate(ctx, sid, templateData), nil
6484
}
6585

0 commit comments

Comments
 (0)