Add semantic intent system for natural language CLI interactions:
- Mode system (Normal/Strict/Collect) for missing key handling
- Subject type with fluent builder for typed subjects
- Composed type with Question/Confirm/Success/Failure forms
- 30+ core.* intents (delete, create, commit, push, etc.)
- Grammar engine: verb conjugation, noun pluralization, articles
- Template functions: title, lower, upper, past, plural, article
- Enhanced CLI: Confirm with options, Question, Choose functions
- Collect mode handler for QA testing
Usage:
i18n.T("core.delete", i18n.S("file", "config.yaml"))
result := i18n.C("core.delete", subject)
cli.ConfirmIntent("core.delete", subject)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
74 lines
2 KiB
Go
74 lines
2 KiB
Go
// Package i18n provides internationalization for the CLI.
|
|
package i18n
|
|
|
|
import (
|
|
"runtime"
|
|
)
|
|
|
|
// Mode determines how the i18n service handles missing translation keys.
|
|
type Mode int
|
|
|
|
const (
|
|
// ModeNormal returns the key as-is when a translation is missing (production).
|
|
ModeNormal Mode = iota
|
|
// ModeStrict panics immediately when a translation is missing (dev/CI).
|
|
ModeStrict
|
|
// ModeCollect dispatches a MissingKeyAction and returns [key] (QA testing).
|
|
ModeCollect
|
|
)
|
|
|
|
// String returns the string representation of the mode.
|
|
func (m Mode) String() string {
|
|
switch m {
|
|
case ModeNormal:
|
|
return "normal"
|
|
case ModeStrict:
|
|
return "strict"
|
|
case ModeCollect:
|
|
return "collect"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
// MissingKeyAction is dispatched when a translation key is not found in collect mode.
|
|
// It contains caller information for debugging and QA purposes.
|
|
type MissingKeyAction struct {
|
|
Key string // The missing translation key
|
|
Args map[string]any // Arguments passed to the translation
|
|
CallerFile string // Source file where T() was called
|
|
CallerLine int // Line number where T() was called
|
|
}
|
|
|
|
// ActionHandler is a function that handles MissingKeyAction dispatches.
|
|
// Register handlers via SetActionHandler to receive missing key notifications.
|
|
type ActionHandler func(action MissingKeyAction)
|
|
|
|
var actionHandler ActionHandler
|
|
|
|
// SetActionHandler registers a handler for MissingKeyAction dispatches.
|
|
// Only one handler can be active at a time; subsequent calls replace the previous handler.
|
|
func SetActionHandler(h ActionHandler) {
|
|
actionHandler = h
|
|
}
|
|
|
|
// dispatchMissingKey creates and dispatches a MissingKeyAction.
|
|
// Called internally when a key is missing in collect mode.
|
|
func dispatchMissingKey(key string, args map[string]any) {
|
|
if actionHandler == nil {
|
|
return
|
|
}
|
|
|
|
_, file, line, ok := runtime.Caller(2) // Skip dispatchMissingKey and handleMissingKey
|
|
if !ok {
|
|
file = "unknown"
|
|
line = 0
|
|
}
|
|
|
|
actionHandler(MissingKeyAction{
|
|
Key: key,
|
|
Args: args,
|
|
CallerFile: file,
|
|
CallerLine: line,
|
|
})
|
|
}
|