From 174d78b37d687e2321031897c7ffc9d1888ad47c Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 10:09:24 +0000 Subject: [PATCH] feat(i18n): extend core service options Co-Authored-By: Virgil --- core_service.go | 18 ++++++++++++++++++ hooks_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/core_service.go b/core_service.go index ae9b5c1..b4ee935 100644 --- a/core_service.go +++ b/core_service.go @@ -25,8 +25,16 @@ type CoreService struct { type ServiceOptions struct { // Language overrides auto-detection (e.g., "en-GB", "de") Language string + // Fallback sets the fallback language for missing translations. + Fallback string + // Formality sets the default formality level. + Formality Formality + // Location sets the default location context. + Location string // Mode sets the translation mode (Normal, Strict, Collect) Mode Mode + // Debug prefixes translated output with the message key. + Debug bool // ExtraFS loads additional translation files on top of the embedded defaults. // Each entry is an fs.FS + directory path within it. ExtraFS []FSSource @@ -67,8 +75,18 @@ func NewCoreService(opts ServiceOptions) func(*core.Core) (any, error) { return nil, langErr } } + if opts.Fallback != "" { + svc.SetFallback(opts.Fallback) + } + if opts.Formality != FormalityNeutral { + svc.SetFormality(opts.Formality) + } + if opts.Location != "" { + svc.SetLocation(opts.Location) + } svc.SetMode(opts.Mode) + svc.SetDebug(opts.Debug) SetDefault(svc) return &CoreService{ diff --git a/hooks_test.go b/hooks_test.go index d1beff1..0437ba9 100644 --- a/hooks_test.go +++ b/hooks_test.go @@ -311,6 +311,35 @@ func TestNewCoreService_InvalidLanguagePreservesSetLanguageError(t *testing.T) { assert.NotContains(t, msg, "invalid language") } +func TestNewCoreService_AppliesOptions(t *testing.T) { + prev := Default() + SetDefault(nil) + t.Cleanup(func() { + SetDefault(prev) + }) + + factory := NewCoreService(ServiceOptions{ + Language: "en", + Fallback: "fr", + Formality: FormalityFormal, + Location: "workspace", + Mode: ModeCollect, + Debug: true, + }) + + _, err := factory(nil) + require.NoError(t, err) + + svc := Default() + require.NotNil(t, svc) + assert.Equal(t, "en", svc.Language()) + assert.Equal(t, "fr", svc.Fallback()) + assert.Equal(t, FormalityFormal, svc.Formality()) + assert.Equal(t, "workspace", svc.Location()) + assert.Equal(t, ModeCollect, svc.Mode()) + assert.True(t, svc.Debug()) +} + func TestInit_ReDetectsRegisteredLocales(t *testing.T) { t.Setenv("LANG", "de_DE.UTF-8") -- 2.45.3