feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
// 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
|
2026-01-30 13:36:55 +00:00
|
|
|
// ModeCollect dispatches MissingKey actions and returns [key] (QA testing).
|
feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 13:36:55 +00:00
|
|
|
var missingKeyHandler MissingKeyHandler
|
|
|
|
|
|
|
|
|
|
// OnMissingKey registers a handler for missing translation keys.
|
|
|
|
|
// Called when T() or C() can't find a key in ModeCollect.
|
|
|
|
|
//
|
|
|
|
|
// i18n.SetMode(i18n.ModeCollect)
|
|
|
|
|
// i18n.OnMissingKey(func(m i18n.MissingKey) {
|
|
|
|
|
// log.Printf("MISSING: %s at %s:%d", m.Key, m.CallerFile, m.CallerLine)
|
|
|
|
|
// })
|
|
|
|
|
func OnMissingKey(h MissingKeyHandler) {
|
|
|
|
|
missingKeyHandler = h
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetActionHandler registers a handler for missing key dispatches.
|
|
|
|
|
// Deprecated: Use OnMissingKey instead.
|
|
|
|
|
func SetActionHandler(h func(action MissingKeyAction)) {
|
|
|
|
|
OnMissingKey(h)
|
feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 13:36:55 +00:00
|
|
|
// dispatchMissingKey creates and dispatches a MissingKey event.
|
|
|
|
|
// Called internally when a key is missing in ModeCollect.
|
feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
func dispatchMissingKey(key string, args map[string]any) {
|
2026-01-30 13:36:55 +00:00
|
|
|
if missingKeyHandler == nil {
|
feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, file, line, ok := runtime.Caller(2) // Skip dispatchMissingKey and handleMissingKey
|
|
|
|
|
if !ok {
|
|
|
|
|
file = "unknown"
|
|
|
|
|
line = 0
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 13:36:55 +00:00
|
|
|
missingKeyHandler(MissingKey{
|
feat(i18n): implement semantic i18n system with grammar engine
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>
2026-01-30 12:29:44 +00:00
|
|
|
Key: key,
|
|
|
|
|
Args: args,
|
|
|
|
|
CallerFile: file,
|
|
|
|
|
CallerLine: line,
|
|
|
|
|
})
|
|
|
|
|
}
|