From eb45f9bfb1b264d5e75a57c11c646a7b041d69f7 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 06:22:17 +0000 Subject: [PATCH] feat(i18n): add article phrase template helper Co-Authored-By: Virgil --- grammar.go | 11 ++++++++++- grammar_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/grammar.go b/grammar.go index 672f392..c84250a 100644 --- a/grammar.go +++ b/grammar.go @@ -555,6 +555,15 @@ func Quote(s string) string { return `"` + s + `"` } +// ArticlePhrase prefixes a noun phrase with the correct article. +func ArticlePhrase(word string) string { + article := Article(word) + if article == "" || word == "" { + return "" + } + return article + " " + word +} + // TemplateFuncs returns the template.FuncMap with all grammar functions. func TemplateFuncs() template.FuncMap { return template.FuncMap{ @@ -565,7 +574,7 @@ func TemplateFuncs() template.FuncMap { "gerund": Gerund, "plural": Pluralize, "pluralForm": PluralForm, - "article": Article, + "article": ArticlePhrase, "quote": Quote, } } diff --git a/grammar_test.go b/grammar_test.go index f4e41c7..29ca559 100644 --- a/grammar_test.go +++ b/grammar_test.go @@ -1,6 +1,10 @@ package i18n -import "testing" +import ( + "strings" + "testing" + "text/template" +) func TestPastTense(t *testing.T) { // Ensure grammar data is loaded from embedded JSON @@ -342,6 +346,26 @@ func TestQuote(t *testing.T) { } } +func TestArticlePhrase(t *testing.T) { + tests := []struct { + word string + want string + }{ + {"file", "a file"}, + {"error", "an error"}, + {"", ""}, + } + + for _, tt := range tests { + t.Run(tt.word, func(t *testing.T) { + got := ArticlePhrase(tt.word) + if got != tt.want { + t.Errorf("ArticlePhrase(%q) = %q, want %q", tt.word, got, tt.want) + } + }) + } +} + func TestLabel(t *testing.T) { svc, err := New() if err != nil { @@ -627,6 +651,22 @@ func TestTemplateFuncs(t *testing.T) { } } +func TestTemplateFuncs_Article(t *testing.T) { + tmpl, err := template.New("").Funcs(TemplateFuncs()).Parse(`{{article "apple"}}`) + 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) + } + + if got, want := buf.String(), "an apple"; got != want { + t.Fatalf("template article = %q, want %q", got, want) + } +} + // --- Benchmarks --- func BenchmarkPastTense_Irregular(b *testing.B) { -- 2.45.3