refactor(i18n): extract action handlers to actions.go
Move handler functions from mode.go to actions.go: - OnMissingKey, SetActionHandler, dispatchMissingKey mode.go now contains only Mode type and constants. interface.go keeps all types/interfaces. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
c78f1d9df1
commit
002f8c7813
2 changed files with 46 additions and 44 deletions
46
pkg/i18n/actions.go
Normal file
46
pkg/i18n/actions.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// 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
|
||||
}
|
||||
|
||||
// SetActionHandler registers a handler for missing key dispatches.
|
||||
// Deprecated: Use OnMissingKey instead.
|
||||
func SetActionHandler(h func(action MissingKeyAction)) {
|
||||
OnMissingKey(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,
|
||||
})
|
||||
}
|
||||
|
|
@ -1,10 +1,6 @@
|
|||
// Package i18n provides internationalization for the CLI.
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Mode determines how the i18n service handles missing translation keys.
|
||||
type Mode int
|
||||
|
||||
|
|
@ -30,43 +26,3 @@ func (m Mode) String() string {
|
|||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue