go-i18n/transform.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

117 lines
1.6 KiB
Go

package i18n
func getCount(data any) int {
if data == nil {
return 0
}
switch d := data.(type) {
case map[string]any:
if c, ok := d["Count"]; ok {
return toInt(c)
}
case map[string]int:
if c, ok := d["Count"]; ok {
return c
}
}
return 0
}
func toInt(v any) int {
if v == nil {
return 0
}
switch n := v.(type) {
case int:
return n
case int64:
return int(n)
case int32:
return int(n)
case int16:
return int(n)
case int8:
return int(n)
case uint:
return int(n)
case uint64:
return int(n)
case uint32:
return int(n)
case uint16:
return int(n)
case uint8:
return int(n)
case float64:
return int(n)
case float32:
return int(n)
}
return 0
}
func toInt64(v any) int64 {
if v == nil {
return 0
}
switch n := v.(type) {
case int:
return int64(n)
case int64:
return n
case int32:
return int64(n)
case int16:
return int64(n)
case int8:
return int64(n)
case uint:
return int64(n)
case uint64:
return int64(n)
case uint32:
return int64(n)
case uint16:
return int64(n)
case uint8:
return int64(n)
case float64:
return int64(n)
case float32:
return int64(n)
}
return 0
}
func toFloat64(v any) float64 {
if v == nil {
return 0
}
switch n := v.(type) {
case float64:
return n
case float32:
return float64(n)
case int:
return float64(n)
case int64:
return float64(n)
case int32:
return float64(n)
case int16:
return float64(n)
case int8:
return float64(n)
case uint:
return float64(n)
case uint64:
return float64(n)
case uint32:
return float64(n)
case uint16:
return float64(n)
case uint8:
return float64(n)
}
return 0
}