From efbea846b67c6b86d2260375166612c1343cbcda Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 07:13:52 +0000 Subject: [PATCH] feat(i18n): respect verb base overrides Co-Authored-By: Virgil --- loader.go | 7 ++++++- loader_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/loader.go b/loader.go index 31f98e4..5260909 100644 --- a/loader.go +++ b/loader.go @@ -117,8 +117,13 @@ func flattenWithGrammar(prefix string, data map[string]any, out map[string]Messa // Verb form object (has base/past/gerund keys) if grammar != nil && isVerbFormObject(v) { verbName := key + if base, ok := v["base"].(string); ok && base != "" { + verbName = base + } if after, ok := strings.CutPrefix(fullKey, "gram.verb."); ok { - verbName = after + if base, ok := v["base"].(string); !ok || base == "" { + verbName = after + } } forms := VerbForms{} if past, ok := v["past"].(string); ok { diff --git a/loader_test.go b/loader_test.go index a64361d..6889205 100644 --- a/loader_test.go +++ b/loader_test.go @@ -132,6 +132,11 @@ func TestFlattenWithGrammar(t *testing.T) { "past": "tested", "gerund": "testing", }, + "publish_draft": map[string]any{ + "base": "publish", + "past": "published", + "gerund": "publishing", + }, }, "noun": map[string]any{ "widget": map[string]any{ @@ -175,6 +180,19 @@ func TestFlattenWithGrammar(t *testing.T) { t.Errorf("test.past = %q, want 'tested'", v.Past) } } + if v, ok := grammar.Verbs["publish"]; !ok { + t.Error("verb base override 'publish' not extracted") + } else { + if v.Past != "published" { + t.Errorf("publish.past = %q, want 'published'", v.Past) + } + if v.Gerund != "publishing" { + t.Errorf("publish.gerund = %q, want 'publishing'", v.Gerund) + } + } + if _, ok := grammar.Verbs["publish_draft"]; ok { + t.Error("verb should be stored under explicit base, not JSON key") + } // Noun extracted if n, ok := grammar.Nouns["widget"]; !ok { @@ -459,6 +477,7 @@ func TestCustomFSLoader(t *testing.T) { Data: []byte(`{ "gram": { "verb": { + "draft": { "base": "draft", "past": "drafted", "gerund": "drafting" }, "zap": { "base": "zap", "past": "zapped", "gerund": "zapping" } }, "word": { @@ -488,4 +507,7 @@ func TestCustomFSLoader(t *testing.T) { if v, ok := gd.Verbs["zap"]; !ok || v.Past != "zapped" { t.Errorf("verb 'zap' not loaded correctly") } + if v, ok := gd.Verbs["draft"]; !ok || v.Past != "drafted" { + t.Errorf("verb base override 'draft' not loaded correctly") + } } -- 2.45.3