Refactor the i18n package for extensibility without breaking changes: - Add KeyHandler interface for pluggable namespace handlers - Add Loader interface for format-agnostic translation loading - Add TranslationContext for translation disambiguation - Implement 6 built-in handlers (Label, Progress, Count, Done, Fail, Numeric) - Update T() to use handler chain instead of hardcoded logic - Add handler management methods (AddHandler, PrependHandler, ClearHandlers) File reorganisation: - types.go: all type definitions - loader.go: Loader interface + FSLoader (from mutate.go, checks.go) - handler.go: KeyHandler interface + built-in handlers - context.go: TranslationContext + C() builder - hooks.go: renamed from actions.go - service.go: merged interfaces.go content Deleted: interfaces.go, mode.go, mutate.go, checks.go Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
40 lines
964 B
Go
40 lines
964 B
Go
// Package i18n provides internationalization for the CLI.
|
|
package i18n
|
|
|
|
import (
|
|
"runtime"
|
|
)
|
|
|
|
var missingKeyHandler MissingKeyHandler
|
|
|
|
// OnMissingKey registers a handler for missing translation keys.
|
|
// Called when T() 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
|
|
}
|
|
|
|
// dispatchMissingKey creates and dispatches a MissingKey event.
|
|
// Called internally when a key is missing in ModeCollect.
|
|
func dispatchMissingKey(key string, args map[string]any) {
|
|
if missingKeyHandler == nil {
|
|
return
|
|
}
|
|
|
|
_, file, line, ok := runtime.Caller(2) // Skip dispatchMissingKey and handleMissingKey
|
|
if !ok {
|
|
file = "unknown"
|
|
line = 0
|
|
}
|
|
|
|
missingKeyHandler(MissingKey{
|
|
Key: key,
|
|
Args: args,
|
|
CallerFile: file,
|
|
CallerLine: line,
|
|
})
|
|
}
|