fix(html): honour Metadata alias for text counts
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Text() count/pluralisation now reads from either Context.Data or Context.Metadata, matching the alias contract when callers populate only one field.\n\nCo-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-04-15 00:29:05 +01:00
parent 7679d2ded5
commit 0c19ccf7cc
2 changed files with 32 additions and 3 deletions

View file

@ -47,6 +47,20 @@ func TestNewContextWithService_AppliesLocaleToService_Good(t *testing.T) {
}
}
func TestTextNode_UsesMetadataAliasWhenDataNil_Good(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
ctx := &Context{
Metadata: map[string]any{"count": 1},
}
got := Text("i18n.count.file").Render(ctx)
if got != "1 file" {
t.Fatalf("Text with metadata-only count = %q, want %q", got, "1 file")
}
}
func TestContext_SetService_AppliesLocale_Good(t *testing.T) {
svc, _ := i18n.New()
ctx := NewContext("fr-FR")

View file

@ -12,11 +12,11 @@ import (
)
func translationArgs(ctx *Context, key string, args []any) []any {
if ctx == nil || len(ctx.Data) == 0 {
if ctx == nil {
return args
}
count, ok := contextCount(ctx.Data)
count, ok := contextCount(ctx)
if !ok {
return args
}
@ -30,10 +30,25 @@ func translationArgs(ctx *Context, key string, args []any) []any {
return args
}
func contextCount(data map[string]any) (int, bool) {
func contextCount(ctx *Context) (int, bool) {
if ctx == nil {
return 0, false
}
if n, ok := contextCountMap(ctx.Data); ok {
return n, true
}
if n, ok := contextCountMap(ctx.Metadata); ok {
return n, true
}
return 0, false
}
func contextCountMap(data map[string]any) (int, bool) {
if len(data) == 0 {
return 0, false
}
if v, ok := data["Count"]; ok {
if n, ok := countInt(v); ok {
return n, true