From 440f96fc16ad55840d48207bc3e55455ee1b39fd Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 07:37:58 +0000 Subject: [PATCH] refactor(i18n): align core primitives with ax conventions Co-Authored-By: Virgil --- compose.go | 10 +++++++--- context.go | 4 +--- handler.go | 3 +-- hooks.go | 13 +++++++------ i18n.go | 3 +-- loader.go | 7 ++++--- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/compose.go b/compose.go index 0162846..6600c9b 100644 --- a/compose.go +++ b/compose.go @@ -1,6 +1,10 @@ package i18n -import "fmt" +import ( + "fmt" + + "dappco.re/go/core" +) // S creates a new Subject with the given noun and value. // @@ -82,7 +86,7 @@ func (s *Subject) String() string { if stringer, ok := s.Value.(fmt.Stringer); ok { return stringer.String() } - return fmt.Sprint(s.Value) + return core.Sprintf("%v", s.Value) } func (s *Subject) IsPlural() bool { return s != nil && s.count != 1 } @@ -96,7 +100,7 @@ func (s *Subject) CountString() string { if s == nil { return "1" } - return fmt.Sprint(s.count) + return core.Sprintf("%d", s.count) } func (s *Subject) GenderString() string { if s == nil { diff --git a/context.go b/context.go index dbb7dcc..8ffb002 100644 --- a/context.go +++ b/context.go @@ -1,7 +1,5 @@ package i18n -import "fmt" - // TranslationContext provides disambiguation for translations. // // T("direction.right", C("navigation")) // "rechts" (German) @@ -88,7 +86,7 @@ func (c *TranslationContext) String() string { if c == nil { return "" } - return fmt.Sprint(c.Context) + return c.Context } func (c *TranslationContext) GenderString() string { diff --git a/handler.go b/handler.go index f564d27..5b02bb7 100644 --- a/handler.go +++ b/handler.go @@ -2,7 +2,6 @@ package i18n import ( "fmt" - "strings" "unicode" "dappco.re/go/core" @@ -184,7 +183,7 @@ func isPluralisableWordDisplay(s string) bool { } func isUpperAcronymPlural(s string) bool { - if len(s) < 2 || !strings.HasSuffix(s, "s") { + if len(s) < 2 || !core.HasSuffix(s, "s") { return false } hasLetter := false diff --git a/hooks.go b/hooks.go index 2d38c60..fdd4402 100644 --- a/hooks.go +++ b/hooks.go @@ -2,11 +2,12 @@ package i18n import ( "io/fs" - "log" "runtime" - "strings" "sync" "sync/atomic" + + "dappco.re/go/core" + log "dappco.re/go/core/log" ) var missingKeyHandler atomic.Value @@ -59,7 +60,7 @@ func RegisterLocales(fsys fs.FS, dir string) { registeredLocalesMu.Unlock() if svc != nil { if err := svc.LoadFS(fsys, dir); err != nil { - log.Printf("i18n: RegisterLocales failed to load %q: %v", dir, err) + log.Error("i18n: RegisterLocales failed to load", "dir", dir, "err", err) } else { svc.markLocaleRegistrationLoaded(reg.id) } @@ -99,7 +100,7 @@ func loadRegisteredLocales(svc *Service) { continue } if err := svc.LoadFS(reg.fsys, reg.dir); err != nil { - log.Printf("i18n: loadRegisteredLocales failed to load %q: %v", reg.dir, err) + log.Error("i18n: loadRegisteredLocales failed to load", "dir", reg.dir, "err", err) continue } svc.markLocaleRegistrationLoaded(reg.id) @@ -122,7 +123,7 @@ func loadLocaleProvider(svc *Service, provider localeProviderRegistration) { } for _, src := range provider.provider.LocaleSources() { if err := svc.LoadFS(src.FS, src.Dir); err != nil { - log.Printf("i18n: loadLocaleProvider failed to load %q: %v", src.Dir, err) + log.Error("i18n: loadLocaleProvider failed to load", "dir", src.Dir, "err", err) } } svc.markLocaleProviderLoaded(provider.id) @@ -191,7 +192,7 @@ func missingKeyCaller() (string, int) { frames := runtime.CallersFrames(pcs[:n]) for { frame, more := frames.Next() - if !strings.HasPrefix(frame.Function, packagePrefix) || strings.HasSuffix(frame.File, "_test.go") { + if !core.HasPrefix(frame.Function, packagePrefix) || core.HasSuffix(frame.File, "_test.go") { return frame.File, frame.Line } if !more { diff --git a/i18n.go b/i18n.go index 5c1815e..8b68d84 100644 --- a/i18n.go +++ b/i18n.go @@ -3,7 +3,6 @@ package i18n import ( "bytes" "io/fs" - "strings" "text/template" "dappco.re/go/core" @@ -151,7 +150,7 @@ func Lang(key string) string { if got := T("lang." + key); got != "lang."+key { return got } - if idx := strings.IndexAny(key, "-_"); idx > 0 { + if idx := indexAny(key, "-_"); idx > 0 { if base := key[:idx]; base != "" { if got := T("lang." + base); got != "lang."+base { return got diff --git a/loader.go b/loader.go index cacde28..294814b 100644 --- a/loader.go +++ b/loader.go @@ -4,7 +4,6 @@ import ( "io/fs" "path" "slices" - "strings" "sync" "dappco.re/go/core" @@ -136,7 +135,8 @@ func flattenWithGrammar(prefix string, data map[string]any, out map[string]Messa if base, ok := v["base"].(string); ok && base != "" { verbName = base } - if after, ok := strings.CutPrefix(fullKey, "gram.verb."); ok { + if core.HasPrefix(fullKey, "gram.verb.") { + after := core.TrimPrefix(fullKey, "gram.verb.") if base, ok := v["base"].(string); !ok || base == "" { verbName = after } @@ -155,7 +155,8 @@ func flattenWithGrammar(prefix string, data map[string]any, out map[string]Messa // Noun form object (under gram.noun.* or has gender field) if grammar != nil && (core.HasPrefix(fullKey, "gram.noun.") || isNounFormObject(v)) { nounName := key - if after, ok := strings.CutPrefix(fullKey, "gram.noun."); ok { + if core.HasPrefix(fullKey, "gram.noun.") { + after := core.TrimPrefix(fullKey, "gram.noun.") nounName = after } if shouldSkipDeprecatedEnglishGrammarEntry(fullKey) { -- 2.45.3