go-i18n/context.go
Claude e8a87b0f50
feat: grammar-aware i18n module extracted from core
Standalone grammar-aware translation engine with:
- 3-tier verb/noun fallback (JSON locale → irregular maps → regular rules)
- 6 built-in i18n.* namespace handlers (label, progress, count, done, fail, numeric)
- Nested en.json with gram/prompt/time/lang sections (no flat command keys)
- CLDR plural rules for 10 languages
- Subject fluent API, number/time formatting, RTL detection
- 55 tests passing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 19:51:27 +00:00

70 lines
1.6 KiB
Go

package i18n
// TranslationContext provides disambiguation for translations.
//
// T("direction.right", C("navigation")) // "rechts" (German)
// T("status.right", C("correctness")) // "richtig" (German)
type TranslationContext struct {
Context string
Gender string
Formality Formality
Extra map[string]any
}
// C creates a TranslationContext.
func C(context string) *TranslationContext {
return &TranslationContext{Context: context}
}
func (c *TranslationContext) WithGender(gender string) *TranslationContext {
if c == nil { return nil }
c.Gender = gender
return c
}
func (c *TranslationContext) Formal() *TranslationContext {
if c == nil { return nil }
c.Formality = FormalityFormal
return c
}
func (c *TranslationContext) Informal() *TranslationContext {
if c == nil { return nil }
c.Formality = FormalityInformal
return c
}
func (c *TranslationContext) WithFormality(f Formality) *TranslationContext {
if c == nil { return nil }
c.Formality = f
return c
}
func (c *TranslationContext) Set(key string, value any) *TranslationContext {
if c == nil { return nil }
if c.Extra == nil {
c.Extra = make(map[string]any)
}
c.Extra[key] = value
return c
}
func (c *TranslationContext) Get(key string) any {
if c == nil || c.Extra == nil { return nil }
return c.Extra[key]
}
func (c *TranslationContext) ContextString() string {
if c == nil { return "" }
return c.Context
}
func (c *TranslationContext) GenderString() string {
if c == nil { return "" }
return c.Gender
}
func (c *TranslationContext) FormalityValue() Formality {
if c == nil { return FormalityNeutral }
return c.Formality
}