fix: fs.go use Result{}.Result() return value, i18n uses i.locale
fs.go: Value receiver Result() returns new Result — must use return
value not discard it. Changed from r.Result(...); return *r to
return Result{}.Result(os.ReadDir(...)).
i18n: SetLanguage sets i.locale directly. Language() reads i.locale.
Translator reload is core/go-i18n's responsibility.
231 tests passing.
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
9bcb367dd0
commit
b0ec660e78
3 changed files with 20 additions and 43 deletions
|
|
@ -181,9 +181,7 @@ func (m *Fs) List(p string) Result {
|
|||
if !vp.OK {
|
||||
return Result{}
|
||||
}
|
||||
r := &Result{}
|
||||
r.Result(os.ReadDir(vp.Value.(string)))
|
||||
return *r
|
||||
return Result{}.Result(os.ReadDir(vp.Value.(string)))
|
||||
}
|
||||
|
||||
// Stat returns file info.
|
||||
|
|
@ -192,9 +190,7 @@ func (m *Fs) Stat(p string) Result {
|
|||
if !vp.OK {
|
||||
return Result{}
|
||||
}
|
||||
r := &Result{}
|
||||
r.Result(os.Stat(vp.Value.(string)))
|
||||
return *r
|
||||
return Result{}.Result(os.Stat(vp.Value.(string)))
|
||||
}
|
||||
|
||||
// Open opens the named file for reading.
|
||||
|
|
@ -203,9 +199,7 @@ func (m *Fs) Open(p string) Result {
|
|||
if !vp.OK {
|
||||
return Result{}
|
||||
}
|
||||
r := &Result{}
|
||||
r.Result(os.Open(vp.Value.(string)))
|
||||
return *r
|
||||
return Result{}.Result(os.Open(vp.Value.(string)))
|
||||
}
|
||||
|
||||
// Create creates or truncates the named file.
|
||||
|
|
@ -218,9 +212,7 @@ func (m *Fs) Create(p string) Result {
|
|||
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
|
||||
return Result{}
|
||||
}
|
||||
r := &Result{}
|
||||
r.Result(os.Create(full))
|
||||
return *r
|
||||
return Result{}.Result(os.Create(full))
|
||||
}
|
||||
|
||||
// Append opens the named file for appending, creating it if it doesn't exist.
|
||||
|
|
@ -233,9 +225,7 @@ func (m *Fs) Append(p string) Result {
|
|||
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
|
||||
return Result{}
|
||||
}
|
||||
r := &Result{}
|
||||
r.Result(os.OpenFile(full, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644))
|
||||
return *r
|
||||
return Result{}.Result(os.OpenFile(full, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644))
|
||||
}
|
||||
|
||||
// ReadStream returns a reader for the file content.
|
||||
|
|
|
|||
|
|
@ -43,11 +43,11 @@ type LocaleProvider interface {
|
|||
// I18n manages locale collection and translation dispatch.
|
||||
type I18n struct {
|
||||
mu sync.RWMutex
|
||||
locales []*Embed // collected from LocaleProvider services
|
||||
locales []*Embed // collected from LocaleProvider services
|
||||
locale string
|
||||
translator Translator // registered implementation (nil until set)
|
||||
}
|
||||
|
||||
|
||||
// AddLocales adds locale mounts (called during service registration).
|
||||
func (i *I18n) AddLocales(mounts ...*Embed) {
|
||||
i.mu.Lock()
|
||||
|
|
@ -93,24 +93,17 @@ func (i *I18n) T(messageID string, args ...any) string {
|
|||
|
||||
// SetLanguage sets the active language. No-op if no translator is registered.
|
||||
func (i *I18n) SetLanguage(lang string) Result {
|
||||
i.mu.RLock()
|
||||
t := i.translator
|
||||
i.mu.RUnlock()
|
||||
if t != nil {
|
||||
r := &Result{}
|
||||
r.Result(nil, t.SetLanguage(lang))
|
||||
return *r
|
||||
|
||||
if lang != "" {
|
||||
i.locale = lang
|
||||
}
|
||||
return Result{OK: true}
|
||||
}
|
||||
|
||||
// Language returns the current language code, or "en" if no translator.
|
||||
// Language returns the current language code, or "en" if not set.
|
||||
func (i *I18n) Language() string {
|
||||
i.mu.RLock()
|
||||
t := i.translator
|
||||
i.mu.RUnlock()
|
||||
if t != nil {
|
||||
return t.Language()
|
||||
if i.locale != "" {
|
||||
return i.locale
|
||||
}
|
||||
return "en"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,31 +46,25 @@ type Result struct {
|
|||
// Result gets or sets the value. Zero args returns Value. With args, maps
|
||||
// Go (value, error) pairs to Result and returns self.
|
||||
//
|
||||
// r.Result() // returns r.Value
|
||||
// r.Result(file, err) // OK = err == nil, Value = file
|
||||
// r.Result(value) // OK = true, Value = value
|
||||
// r.Result() // after set — returns the value
|
||||
func (r *Result) Result(args ...any) any {
|
||||
if len(args) == 0 {
|
||||
return r.Value
|
||||
func (r Result) Result(args ...any) Result {
|
||||
|
||||
if len(args) == 1 {
|
||||
return Result{args[0], true}
|
||||
}
|
||||
|
||||
if len(args) >= 2 {
|
||||
if err, ok := args[len(args)-1].(error); ok {
|
||||
if err != nil {
|
||||
r.Value = err
|
||||
r.OK = false
|
||||
return r.Value
|
||||
return Result{err, false}
|
||||
}
|
||||
r.Value = args[0]
|
||||
r.OK = true
|
||||
return r.Value
|
||||
return Result{args[0], true}
|
||||
}
|
||||
}
|
||||
return Result{args[0], true}
|
||||
|
||||
r.Value = args[0]
|
||||
r.OK = true
|
||||
return r.Value
|
||||
}
|
||||
|
||||
// Option is a single key-value configuration pair.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue