feat(i18n): add service constructor aliases

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 09:16:06 +00:00
parent 63f7e3e6cc
commit 3927860245
2 changed files with 55 additions and 8 deletions

View file

@ -97,11 +97,24 @@ func New(opts ...Option) (*Service, error) {
return NewWithLoader(NewFSLoader(localeFS, "locales"), opts...)
}
// NewService creates a new i18n service with embedded locales.
//
// This is a named alias for New that keeps the constructor intent explicit
// for callers that prefer service-oriented naming.
func NewService(opts ...Option) (*Service, error) {
return New(opts...)
}
// NewWithFS creates a new i18n service loading locales from the given filesystem.
func NewWithFS(fsys fs.FS, dir string, opts ...Option) (*Service, error) {
return NewWithLoader(NewFSLoader(fsys, dir), opts...)
}
// NewServiceWithFS creates a new i18n service loading locales from the given filesystem.
func NewServiceWithFS(fsys fs.FS, dir string, opts ...Option) (*Service, error) {
return NewWithFS(fsys, dir, opts...)
}
// NewWithLoader creates a new i18n service with a custom loader.
func NewWithLoader(loader Loader, opts ...Option) (*Service, error) {
if loader == nil {
@ -169,6 +182,11 @@ func NewWithLoader(loader Loader, opts ...Option) (*Service, error) {
return s, nil
}
// NewServiceWithLoader creates a new i18n service with a custom loader.
func NewServiceWithLoader(loader Loader, opts ...Option) (*Service, error) {
return NewWithLoader(loader, opts...)
}
// Init initialises the default global service if none has been set via SetDefault.
func Init() error {
if defaultService.Load() != nil {

View file

@ -67,16 +67,16 @@ func (duplicateLangLoader) Load(lang string) (map[string]Message, *GrammarData,
switch lang {
case "en_US":
return map[string]Message{
"first.key": {Text: "first"},
}, &GrammarData{
Words: map[string]string{"pkg": "package"},
}, nil
"first.key": {Text: "first"},
}, &GrammarData{
Words: map[string]string{"pkg": "package"},
}, nil
case "en-US":
return map[string]Message{
"second.key": {Text: "second"},
}, &GrammarData{
Words: map[string]string{"api": "API"},
}, nil
"second.key": {Text: "second"},
}, &GrammarData{
Words: map[string]string{"api": "API"},
}, nil
default:
return map[string]Message{}, nil, nil
}
@ -104,6 +104,35 @@ func TestNewService(t *testing.T) {
}
}
func TestNewServiceAliases(t *testing.T) {
svc, err := NewService()
if err != nil {
t.Fatalf("NewService() failed: %v", err)
}
if svc == nil {
t.Fatal("NewService() returned nil service")
}
fsys := fstest.MapFS{
"locales/en.json": &fstest.MapFile{Data: []byte(`{}`)},
}
withFS, err := NewServiceWithFS(fsys, "locales")
if err != nil {
t.Fatalf("NewServiceWithFS() failed: %v", err)
}
if withFS == nil {
t.Fatal("NewServiceWithFS() returned nil service")
}
withLoader, err := NewServiceWithLoader(messageBaseFallbackLoader{})
if err != nil {
t.Fatalf("NewServiceWithLoader() failed: %v", err)
}
if withLoader == nil {
t.Fatal("NewServiceWithLoader() returned nil service")
}
}
func TestServiceAvailableLanguagesSorted(t *testing.T) {
svc, err := NewWithLoader(messageBaseFallbackLoader{})
if err != nil {