Skip to content

Commit

Permalink
fix: 正确处理本地化时未指定 message.Printer 的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Oct 15, 2023
1 parent fc5f3c6 commit d4cadc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func (e *Record) With(name string, val any) Logger {
return e
}

func (e *Record) StdLogger() *log.Logger {
return log.New(e.asWriter(), "", 0)
}
func (e *Record) StdLogger() *log.Logger { return log.New(e.asWriter(), "", 0) }

func (e *Record) Error(err error) { e.DepthError(2, err) }

Expand Down Expand Up @@ -215,6 +213,10 @@ func (e *Record) output() {
}

func replaceLocaleString(p *localeutil.Printer, v []any) {
if p == nil {
return
}

for i, val := range v {
if ls, ok := val.(localeutil.Stringer); ok {
v[i] = ls.LocaleString(p)
Expand Down
16 changes: 16 additions & 0 deletions record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ func TestRecord_Error(t *testing.T) {
a.True(strings.HasSuffix(buf.String(), "root\nroot\ncn\n"), buf.String())
})
}

func TestRecord_Println(t *testing.T) {
a := assert.New(t, false)

c := catalog.NewBuilder()
a.NotError(c.SetString(language.SimplifiedChinese, "abc", "cn"))
p := message.NewPrinter(language.SimplifiedChinese, message.Catalog(c))
a.NotNil(p)
buf := &bytes.Buffer{}
l := New(NewTextHandler(buf), WithLocation(true), WithCreated(MicroLayout), WithLocale(p))
a.NotNil(l)

e := l.NewRecord(LevelWarn)
e.Println(localeutil.Phrase("abc"))
a.True(strings.HasSuffix(buf.String(), "cn\n\n"), buf.String()) // Println 本身包含了一个回车符
}

0 comments on commit d4cadc9

Please sign in to comment.