feat(core): add LocaleProvider interface for automatic i18n collection
Services implementing LocaleProvider have their locale FS collected during RegisterService. The i18n service reads Core.Locales() on startup to load all translations. Zero explicit wiring needed. Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e1294b8412
commit
d64099b028
2 changed files with 28 additions and 0 deletions
|
|
@ -295,7 +295,13 @@ func (c *Core) RegisterTask(handler TaskHandler) {
|
|||
}
|
||||
|
||||
// RegisterService adds a new service to the Core.
|
||||
// If the service implements LocaleProvider, its locale FS is collected
|
||||
// for the i18n service to load during startup.
|
||||
func (c *Core) RegisterService(name string, api any) error {
|
||||
// Collect locale filesystems from services that provide them
|
||||
if lp, ok := api.(LocaleProvider); ok {
|
||||
c.locales = append(c.locales, lp.Locales())
|
||||
}
|
||||
return c.svc.registerService(name, api)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"embed"
|
||||
goio "io"
|
||||
"io/fs"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
|
@ -77,6 +78,20 @@ type Stoppable interface {
|
|||
OnShutdown(ctx context.Context) error
|
||||
}
|
||||
|
||||
// LocaleProvider is implemented by services that ship their own translation files.
|
||||
// Core discovers this interface during service registration and collects the
|
||||
// locale filesystems. The i18n service loads them during startup.
|
||||
//
|
||||
// Usage in a service package:
|
||||
//
|
||||
// //go:embed locales
|
||||
// var localeFS embed.FS
|
||||
//
|
||||
// func (s *MyService) Locales() fs.FS { return localeFS }
|
||||
type LocaleProvider interface {
|
||||
Locales() fs.FS
|
||||
}
|
||||
|
||||
// Core is the central application object that manages services, assets, and communication.
|
||||
type Core struct {
|
||||
App any // GUI runtime (e.g., Wails App) - set by WithApp option
|
||||
|
|
@ -84,12 +99,19 @@ type Core struct {
|
|||
Features *Features
|
||||
svc *serviceManager
|
||||
bus *messageBus
|
||||
locales []fs.FS // collected from LocaleProvider services
|
||||
|
||||
taskIDCounter atomic.Uint64
|
||||
wg sync.WaitGroup
|
||||
shutdown atomic.Bool
|
||||
}
|
||||
|
||||
// Locales returns all locale filesystems collected from registered services.
|
||||
// The i18n service uses this during startup to load translations.
|
||||
func (c *Core) Locales() []fs.FS {
|
||||
return c.locales
|
||||
}
|
||||
|
||||
// Config provides access to application configuration.
|
||||
type Config interface {
|
||||
// Get retrieves a configuration value by key and stores it in the 'out' variable.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue