From e59f7f8cfde9538af10ad5f58d677c49420b19f5 Mon Sep 17 00:00:00 2001 From: Snider Date: Fri, 30 Jan 2026 18:08:33 +0000 Subject: [PATCH] refactor(i18n): final code standards cleanup - Add explicit nil checks to toInt, toInt64, toFloat64 for consistency - Standardize error messages to use %q for user-provided strings - Export GetGrammarData for symmetry with SetGrammarData - Standardize String() doc comments to "the TypeName" pattern Co-Authored-By: Claude Opus 4.5 --- pkg/i18n/grammar.go | 12 ++++++------ pkg/i18n/language.go | 8 ++++---- pkg/i18n/mode.go | 2 +- pkg/i18n/service.go | 12 ++++++------ pkg/i18n/transform.go | 9 +++++++++ 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/i18n/grammar.go b/pkg/i18n/grammar.go index f58decb2..317156fa 100644 --- a/pkg/i18n/grammar.go +++ b/pkg/i18n/grammar.go @@ -7,9 +7,9 @@ import ( "unicode" ) -// getGrammarData returns the grammar data for the current language. +// GetGrammarData returns the grammar data for the specified language. // Returns nil if no grammar data is loaded for the language. -func getGrammarData(lang string) *GrammarData { +func GetGrammarData(lang string) *GrammarData { grammarCacheMu.RLock() defer grammarCacheMu.RUnlock() return grammarCache[lang] @@ -26,7 +26,7 @@ func SetGrammarData(lang string, data *GrammarData) { // getVerbForm retrieves a verb form from JSON data. // Returns empty string if not found, allowing fallback to computed form. func getVerbForm(lang, verb, form string) string { - data := getGrammarData(lang) + data := GetGrammarData(lang) if data == nil || data.Verbs == nil { return "" } @@ -45,7 +45,7 @@ func getVerbForm(lang, verb, form string) string { // getWord retrieves a base word translation from JSON data. // Returns empty string if not found, allowing fallback to the key itself. func getWord(lang, word string) string { - data := getGrammarData(lang) + data := GetGrammarData(lang) if data == nil || data.Words == nil { return "" } @@ -55,7 +55,7 @@ func getWord(lang, word string) string { // getPunct retrieves a punctuation rule for the language. // Returns the default if not found. func getPunct(lang, rule, defaultVal string) string { - data := getGrammarData(lang) + data := GetGrammarData(lang) if data == nil { return defaultVal } @@ -75,7 +75,7 @@ func getPunct(lang, rule, defaultVal string) string { // getNounForm retrieves a noun form from JSON data. // Returns empty string if not found, allowing fallback to computed form. func getNounForm(lang, noun, form string) string { - data := getGrammarData(lang) + data := GetGrammarData(lang) if data == nil || data.Nouns == nil { return "" } diff --git a/pkg/i18n/language.go b/pkg/i18n/language.go index 911c22c2..638ca784 100644 --- a/pkg/i18n/language.go +++ b/pkg/i18n/language.go @@ -1,7 +1,7 @@ // Package i18n provides internationalization for the CLI. package i18n -// String returns the string representation of a Formality level. +// String returns the string representation of the Formality. func (f Formality) String() string { switch f { case FormalityInformal: @@ -13,7 +13,7 @@ func (f Formality) String() string { } } -// String returns the string representation of a TextDirection. +// String returns the string representation of the TextDirection. func (d TextDirection) String() string { if d == DirRTL { return "rtl" @@ -21,7 +21,7 @@ func (d TextDirection) String() string { return "ltr" } -// String returns the string representation of a PluralCategory. +// String returns the string representation of the PluralCategory. func (p PluralCategory) String() string { switch p { case PluralZero: @@ -39,7 +39,7 @@ func (p PluralCategory) String() string { } } -// String returns the string representation of a GrammaticalGender. +// String returns the string representation of the GrammaticalGender. func (g GrammaticalGender) String() string { switch g { case GenderMasculine: diff --git a/pkg/i18n/mode.go b/pkg/i18n/mode.go index aeb11678..ea3be8c8 100644 --- a/pkg/i18n/mode.go +++ b/pkg/i18n/mode.go @@ -1,7 +1,7 @@ // Package i18n provides internationalization for the CLI. package i18n -// String returns the string representation of the mode. +// String returns the string representation of the Mode. func (m Mode) String() string { switch m { case ModeNormal: diff --git a/pkg/i18n/service.go b/pkg/i18n/service.go index f7527b31..5e937b08 100644 --- a/pkg/i18n/service.go +++ b/pkg/i18n/service.go @@ -36,7 +36,7 @@ func NewWithFS(fsys fs.FS, dir string) (*Service, error) { filePath := filepath.Join(dir, entry.Name()) data, err := fs.ReadFile(fsys, filePath) if err != nil { - return nil, fmt.Errorf("failed to read locale %s: %w", entry.Name(), err) + return nil, fmt.Errorf("failed to read locale %q: %w", entry.Name(), err) } lang := strings.TrimSuffix(entry.Name(), ".json") @@ -44,7 +44,7 @@ func NewWithFS(fsys fs.FS, dir string) (*Service, error) { lang = strings.ReplaceAll(lang, "_", "-") if err := s.loadJSON(lang, data); err != nil { - return nil, fmt.Errorf("failed to parse locale %s: %w", entry.Name(), err) + return nil, fmt.Errorf("failed to parse locale %q: %w", entry.Name(), err) } tag := language.Make(lang) @@ -52,7 +52,7 @@ func NewWithFS(fsys fs.FS, dir string) (*Service, error) { } if len(s.availableLangs) == 0 { - return nil, fmt.Errorf("no locale files found in %s", dir) + return nil, fmt.Errorf("no locale files found in %q", dir) } // Try to detect system language @@ -130,7 +130,7 @@ func (s *Service) SetLanguage(lang string) error { bestMatch, _, confidence := matcher.Match(requestedLang) if confidence == language.No { - return fmt.Errorf("unsupported language: %s", lang) + return fmt.Errorf("unsupported language: %q", lang) } s.currentLang = bestMatch.String() @@ -531,14 +531,14 @@ func (s *Service) LoadFS(fsys fs.FS, dir string) error { filePath := filepath.Join(dir, entry.Name()) data, err := fs.ReadFile(fsys, filePath) if err != nil { - return fmt.Errorf("failed to read locale %s: %w", entry.Name(), err) + return fmt.Errorf("failed to read locale %q: %w", entry.Name(), err) } lang := strings.TrimSuffix(entry.Name(), ".json") lang = strings.ReplaceAll(lang, "_", "-") if err := s.loadJSON(lang, data); err != nil { - return fmt.Errorf("failed to parse locale %s: %w", entry.Name(), err) + return fmt.Errorf("failed to parse locale %q: %w", entry.Name(), err) } // Add to available languages if new diff --git a/pkg/i18n/transform.go b/pkg/i18n/transform.go index 9c401748..095cd168 100644 --- a/pkg/i18n/transform.go +++ b/pkg/i18n/transform.go @@ -21,6 +21,9 @@ func getCount(data any) int { // toInt converts any numeric type to int. func toInt(v any) int { + if v == nil { + return 0 + } switch n := v.(type) { case int: return n @@ -38,6 +41,9 @@ func toInt(v any) int { // toInt64 converts any numeric type to int64. func toInt64(v any) int64 { + if v == nil { + return 0 + } switch n := v.(type) { case int: return int64(n) @@ -55,6 +61,9 @@ func toInt64(v any) int64 { // toFloat64 converts any numeric type to float64. func toFloat64(v any) float64 { + if v == nil { + return 0 + } switch n := v.(type) { case float64: return n