Skip to content

Commit dee6c5d

Browse files
committed
refactor
1 parent 4635e6d commit dee6c5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+310
-239
lines changed

models/actions/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (r *ActionRunner) StatusName() string {
9797
}
9898

9999
func (r *ActionRunner) StatusLocaleName(lang translation.Locale) string {
100-
return lang.Tr("actions.runners.status." + r.StatusName())
100+
return lang.TrString("actions.runners.status." + r.StatusName())
101101
}
102102

103103
func (r *ActionRunner) IsOnline() bool {

models/actions/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s Status) String() string {
4141

4242
// LocaleString returns the locale string name of the Status
4343
func (s Status) LocaleString(lang translation.Locale) string {
44-
return lang.Tr("actions.status." + s.String())
44+
return lang.TrString("actions.status." + s.String())
4545
}
4646

4747
// IsDone returns whether the Status is final

models/git/commit_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (status *CommitStatus) APIURL(ctx context.Context) string {
194194

195195
// LocaleString returns the locale string name of the Status
196196
func (status *CommitStatus) LocaleString(lang translation.Locale) string {
197-
return lang.Tr("repo.commitstatus." + status.State.String())
197+
return lang.TrString("repo.commitstatus." + status.State.String())
198198
}
199199

200200
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc

models/issues/comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ const (
210210

211211
// LocaleString returns the locale string name of the role
212212
func (r RoleInRepo) LocaleString(lang translation.Locale) string {
213-
return lang.Tr("repo.issues.role." + string(r))
213+
return lang.TrString("repo.issues.role." + string(r))
214214
}
215215

216216
// LocaleHelper returns the locale tooltip of the role
217217
func (r RoleInRepo) LocaleHelper(lang translation.Locale) string {
218-
return lang.Tr("repo.issues.role." + string(r) + "_helper")
218+
return lang.TrString("repo.issues.role." + string(r) + "_helper")
219219
}
220220

221221
// Comment represents a comment in commit and issue page.

models/shared/types/ownertype.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ const (
1717
func (o OwnerType) LocaleString(locale translation.Locale) string {
1818
switch o {
1919
case OwnerTypeSystemGlobal:
20-
return locale.Tr("concept_system_global")
20+
return locale.TrString("concept_system_global")
2121
case OwnerTypeIndividual:
22-
return locale.Tr("concept_user_individual")
22+
return locale.TrString("concept_user_individual")
2323
case OwnerTypeRepository:
24-
return locale.Tr("concept_code_repository")
24+
return locale.TrString("concept_code_repository")
2525
case OwnerTypeOrganization:
26-
return locale.Tr("concept_user_organization")
26+
return locale.TrString("concept_user_organization")
2727
}
28-
return locale.Tr("unknown")
28+
return locale.TrString("unknown")
2929
}

modules/auth/password/password.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"crypto/rand"
1010
"errors"
11+
"html/template"
1112
"math/big"
1213
"strings"
1314
"sync"
@@ -121,15 +122,15 @@ func Generate(n int) (string, error) {
121122
}
122123

123124
// BuildComplexityError builds the error message when password complexity checks fail
124-
func BuildComplexityError(locale translation.Locale) string {
125+
func BuildComplexityError(locale translation.Locale) template.HTML {
125126
var buffer bytes.Buffer
126-
buffer.WriteString(locale.Tr("form.password_complexity"))
127+
buffer.WriteString(locale.TrString("form.password_complexity"))
127128
buffer.WriteString("<ul>")
128129
for _, c := range requiredList {
129130
buffer.WriteString("<li>")
130-
buffer.WriteString(locale.Tr(c.TrNameOne))
131+
buffer.WriteString(locale.TrString(c.TrNameOne))
131132
buffer.WriteString("</li>")
132133
}
133134
buffer.WriteString("</ul>")
134-
return buffer.String()
135+
return template.HTML(buffer.String())
135136
}

modules/charset/escape_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (e *escapeStreamer) ambiguousRune(r, c rune) error {
173173
Val: "ambiguous-code-point",
174174
}, html.Attribute{
175175
Key: "data-tooltip-content",
176-
Val: e.locale.Tr("repo.ambiguous_character", r, c),
176+
Val: e.locale.TrString("repo.ambiguous_character", r, c),
177177
}); err != nil {
178178
return err
179179
}

modules/context/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func APIContexter() func(http.Handler) http.Handler {
245245
// NotFound handles 404s for APIContext
246246
// String will replace message, errors will be added to a slice
247247
func (ctx *APIContext) NotFound(objs ...any) {
248-
message := ctx.Tr("error.not_found")
248+
message := ctx.Locale.TrString("error.not_found")
249249
var errors []string
250250
for _, obj := range objs {
251251
// Ignore nil

modules/context/base.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package context
66
import (
77
"context"
88
"fmt"
9+
"html/template"
910
"io"
1011
"net/http"
1112
"net/url"
@@ -286,11 +287,11 @@ func (b *Base) cleanUp() {
286287
}
287288
}
288289

289-
func (b *Base) Tr(msg string, args ...any) string {
290+
func (b *Base) Tr(msg string, args ...any) template.HTML {
290291
return b.Locale.Tr(msg, args...)
291292
}
292293

293-
func (b *Base) TrN(cnt any, key1, keyN string, args ...any) string {
294+
func (b *Base) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
294295
return b.Locale.TrN(cnt, key1, keyN, args...)
295296
}
296297

modules/context/context.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package context
66

77
import (
88
"context"
9-
"html"
9+
"fmt"
1010
"html/template"
1111
"io"
1212
"net/http"
@@ -71,16 +71,6 @@ func init() {
7171
})
7272
}
7373

74-
// TrHTMLEscapeArgs runs ".Locale.Tr()" but pre-escapes all arguments with html.EscapeString.
75-
// This is useful if the locale message is intended to only produce HTML content.
76-
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
77-
trArgs := make([]any, len(args))
78-
for i, arg := range args {
79-
trArgs[i] = html.EscapeString(arg)
80-
}
81-
return ctx.Locale.Tr(msg, trArgs...)
82-
}
83-
8474
type webContextKeyType struct{}
8575

8676
var WebContextKey = webContextKeyType{}
@@ -253,6 +243,12 @@ func (ctx *Context) JSONOK() {
253243
ctx.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it
254244
}
255245

256-
func (ctx *Context) JSONError(msg string) {
257-
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg})
246+
func (ctx *Context) JSONError(msg any) {
247+
switch v := msg.(type) {
248+
case string:
249+
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": v, "renderFormat": "text"})
250+
case template.HTML:
251+
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": v, "renderFormat": "html"})
252+
}
253+
panic(fmt.Sprintf("unsupported type: %T", msg))
258254
}

0 commit comments

Comments
 (0)