Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 15, 2023
1 parent db0947b commit e8b5c67
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 55 deletions.
14 changes: 3 additions & 11 deletions modules/charset/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,10 @@ then resh (ר), and finally heh (ה) (which should appear leftmost).`,
},
}

type nullLocale struct{}

func (nullLocale) Language() string { return "" }
func (nullLocale) Tr(key string, _ ...interface{}) string { return key }
func (nullLocale) TrN(cnt interface{}, key1, keyN string, args ...interface{}) string { return "" }

var _ (translation.Locale) = nullLocale{}

func TestEscapeControlString(t *testing.T) {
for _, tt := range escapeControlTests {
t.Run(tt.name, func(t *testing.T) {
status, result := EscapeControlString(tt.text, nullLocale{})
status, result := EscapeControlString(tt.text, &translation.MockLocale{})
if !reflect.DeepEqual(*status, tt.status) {
t.Errorf("EscapeControlString() status = %v, wanted= %v", status, tt.status)
}
Expand Down Expand Up @@ -179,7 +171,7 @@ func TestEscapeControlReader(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
input := strings.NewReader(tt.text)
output := &strings.Builder{}
status, err := EscapeControlReader(input, output, nullLocale{})
status, err := EscapeControlReader(input, output, &translation.MockLocale{})
result := output.String()
if err != nil {
t.Errorf("EscapeControlReader(): err = %v", err)
Expand All @@ -201,5 +193,5 @@ func TestEscapeControlReader_panic(t *testing.T) {
for i := 0; i < 6826; i++ {
bs = append(bs, []byte("—")...)
}
_, _ = EscapeControlString(string(bs), nullLocale{})
_, _ = EscapeControlString(string(bs), &translation.MockLocale{})
}
17 changes: 2 additions & 15 deletions modules/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/translation"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -550,20 +551,6 @@ a|"he said, ""here I am"""`,
}
}

type mockLocale struct{}

func (l mockLocale) Language() string {
return "en"
}

func (l mockLocale) Tr(s string, _ ...interface{}) string {
return s
}

func (l mockLocale) TrN(_cnt interface{}, key1, _keyN string, _args ...interface{}) string {
return key1
}

func TestFormatError(t *testing.T) {
cases := []struct {
err error
Expand Down Expand Up @@ -591,7 +578,7 @@ func TestFormatError(t *testing.T) {
}

for n, c := range cases {
message, err := FormatError(c.err, mockLocale{})
message, err := FormatError(c.err, &translation.MockLocale{})
if c.expectsError {
assert.Error(t, err, "case %d: expected an error to be returned", n)
} else {
Expand Down
17 changes: 2 additions & 15 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/web/middleware"

chi "github.com/go-chi/chi/v5"
Expand All @@ -34,7 +35,7 @@ func MockContext(t *testing.T, path string) *context.Context {
Values: make(url.Values),
},
Resp: context.NewResponse(resp),
Locale: &mockLocale{},
Locale: &translation.MockLocale{},
}
defer ctx.Close()

Expand Down Expand Up @@ -91,20 +92,6 @@ func LoadGitRepo(t *testing.T, ctx *context.Context) {
assert.NoError(t, err)
}

type mockLocale struct{}

func (l mockLocale) Language() string {
return "en"
}

func (l mockLocale) Tr(s string, _ ...interface{}) string {
return s
}

func (l mockLocale) TrN(_cnt interface{}, key1, _keyN string, _args ...interface{}) string {
return key1
}

type mockResponseWriter struct {
httptest.ResponseRecorder
size int
Expand Down
27 changes: 27 additions & 0 deletions modules/translation/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package translation

import "fmt"

// MockLocale provides a mocked locale without any translations
type MockLocale struct{}

var _ Locale = (*MockLocale)(nil)

func (l MockLocale) Language() string {
return "en"
}

func (l MockLocale) Tr(s string, _ ...interface{}) string {
return s
}

func (l MockLocale) TrN(_cnt interface{}, key1, _keyN string, _args ...interface{}) string {
return key1
}

func (l MockLocale) PrettyNumber(v any) string {
return fmt.Sprint(v)
}
28 changes: 14 additions & 14 deletions modules/translation/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,24 @@ func NewLocale(lang string) Locale {
langName := "unknown"
if l, ok := allLangMap[lang]; ok {
langName = l.Name
}

var msgPrinter *message.Printer
langTag, err := language.Parse(lang)
if err != nil {
log.Error("Failed to parse language tag from name %q: %v", lang, err)
msgPrinter = message.NewPrinter(language.English)
} else {
msgPrinter = message.NewPrinter(langTag)
} else if len(setting.Langs) > 0 {
lang = setting.Langs[0]
langName = setting.Names[0]
}

i18nLocale, _ := i18n.GetLocale(lang)
return &locale{
Locale: i18nLocale,
Lang: lang,
LangName: langName,
msgPrinter: msgPrinter,
l := &locale{
Locale: i18nLocale,
Lang: lang,
LangName: langName,
}
if langTag, err := language.Parse(lang); err != nil {
log.Error("Failed to parse language tag from name %q: %v", l.Lang, err)
l.msgPrinter = message.NewPrinter(language.English)
} else {
l.msgPrinter = message.NewPrinter(langTag)
}
return l
}

func (l *locale) Language() string {
Expand Down

0 comments on commit e8b5c67

Please sign in to comment.