From 5a95df9947591964b90adc4c33b6b81b63e57b50 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 06:30:36 +0000 Subject: [PATCH] feat(i18n): expose composite helpers in templates Co-Authored-By: Virgil --- grammar.go | 23 ++++++++++++++--------- grammar_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/grammar.go b/grammar.go index c84250a..6d0693b 100644 --- a/grammar.go +++ b/grammar.go @@ -567,15 +567,20 @@ func ArticlePhrase(word string) string { // TemplateFuncs returns the template.FuncMap with all grammar functions. func TemplateFuncs() template.FuncMap { return template.FuncMap{ - "title": Title, - "lower": core.Lower, - "upper": core.Upper, - "past": PastTense, - "gerund": Gerund, - "plural": Pluralize, - "pluralForm": PluralForm, - "article": ArticlePhrase, - "quote": Quote, + "title": Title, + "lower": core.Lower, + "upper": core.Upper, + "past": PastTense, + "gerund": Gerund, + "plural": Pluralize, + "pluralForm": PluralForm, + "article": ArticlePhrase, + "quote": Quote, + "label": Label, + "progress": Progress, + "progressSubject": ProgressSubject, + "actionResult": ActionResult, + "actionFailed": ActionFailed, } } diff --git a/grammar_test.go b/grammar_test.go index 29ca559..1fe2710 100644 --- a/grammar_test.go +++ b/grammar_test.go @@ -643,7 +643,22 @@ func TestFrenchGrammarData(t *testing.T) { func TestTemplateFuncs(t *testing.T) { funcs := TemplateFuncs() - expected := []string{"title", "lower", "upper", "past", "gerund", "plural", "pluralForm", "article", "quote"} + expected := []string{ + "title", + "lower", + "upper", + "past", + "gerund", + "plural", + "pluralForm", + "article", + "quote", + "label", + "progress", + "progressSubject", + "actionResult", + "actionFailed", + } for _, name := range expected { if _, ok := funcs[name]; !ok { t.Errorf("TemplateFuncs() missing %q", name) @@ -667,6 +682,31 @@ func TestTemplateFuncs_Article(t *testing.T) { } } +func TestTemplateFuncs_CompositeHelpers(t *testing.T) { + svc, err := New() + if err != nil { + t.Fatalf("New() failed: %v", err) + } + SetDefault(svc) + + tmpl, err := template.New("").Funcs(TemplateFuncs()).Parse( + `{{label "status"}}|{{progress "build"}}|{{progressSubject "build" "project"}}|{{actionResult "delete" "file"}}|{{actionFailed "delete" "file"}}`, + ) + if err != nil { + t.Fatalf("Parse() failed: %v", err) + } + + var buf strings.Builder + if err := tmpl.Execute(&buf, nil); err != nil { + t.Fatalf("Execute() failed: %v", err) + } + + want := "Status:|Building...|Building project...|File deleted|Failed to delete file" + if got := buf.String(); got != want { + t.Fatalf("template composite helpers = %q, want %q", got, want) + } +} + // --- Benchmarks --- func BenchmarkPastTense_Irregular(b *testing.B) { -- 2.45.3