Get locales directly from context like the other code; add translations for subtitle

This commit is contained in:
Mai-Lapyst 2024-03-16 01:17:04 +01:00
parent 6721cba75b
commit 562e5cdf32
No known key found for this signature in database
GPG key ID: F88D929C09E239F8
5 changed files with 25 additions and 33 deletions

View file

@ -5,6 +5,7 @@ package markup
import (
"bytes"
"html/template"
"io"
"net/url"
"path"
@ -1065,7 +1066,7 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
return
}
if DefaultProcessorHelper.GetRepoFileContent == nil || DefaultProcessorHelper.GetLocale == nil {
if DefaultProcessorHelper.GetRepoFileContent == nil {
return
}
@ -1119,9 +1120,17 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
lineSpecs := strings.Split(hash, "-")
lineCount := len(fileContent)
var subTitle string
commitLinkBuffer := new(bytes.Buffer)
html.Render(commitLinkBuffer, createLink(node.Data[m[0]:m[5]], commitSha[0:7], "text black"))
var subTitle template.HTML
var lineOffset int
locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale)
if !ok {
locale = translation.NewLocale("en-US")
}
if len(lineSpecs) == 1 {
line, _ := strconv.Atoi(strings.TrimPrefix(lineSpecs[0], "L"))
if line < 1 || line > lineCount {
@ -1129,7 +1138,10 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
}
fileContent = fileContent[line-1 : line]
subTitle = "Line " + strconv.Itoa(line)
subTitle = locale.Tr(
"markup.filepreview.line", line,
template.HTML(commitLinkBuffer.String()),
)
lineOffset = line - 1
} else {
@ -1141,7 +1153,10 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
}
fileContent = fileContent[startLine-1 : endLine]
subTitle = "Lines " + strconv.Itoa(startLine) + " to " + strconv.Itoa(endLine)
subTitle = locale.Tr(
"markup.filepreview.lines", startLine, endLine,
template.HTML(commitLinkBuffer.String()),
)
lineOffset = startLine - 1
}
@ -1156,12 +1171,6 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
Data: atom.Tbody.String(),
}
locale, err := DefaultProcessorHelper.GetLocale(ctx.Ctx)
if err != nil {
log.Error("Unable to get locale. Error: %v", err)
return
}
status := &charset.EscapeStatus{}
statuses := make([]*charset.EscapeStatus, len(fileContent))
for i, line := range fileContent {
@ -1286,10 +1295,9 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
Attr: []html.Attribute{{Key: "class", Val: "text small grey"}},
}
psubtitle.AppendChild(&html.Node{
Type: html.TextNode,
Data: subTitle + " in ",
Type: html.RawNode,
Data: string(subTitle),
})
psubtitle.AppendChild(createLink(urlFull[m[0]:m[5]], commitSha[0:7], "text black"))
header.AppendChild(psubtitle)
preview := &html.Node{

View file

@ -19,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
@ -684,9 +683,6 @@ func TestRender_FilePreview(t *testing.T) {
buf := []byte("A\nB\nC\nD\n")
return highlight.PlainText(buf), nil
},
GetLocale: func(ctx context.Context) (translation.Locale, error) {
return translation.NewLocale("en-US"), nil
},
})
sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"

View file

@ -17,7 +17,6 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/util"
"github.com/yuin/goldmark/ast"
@ -34,7 +33,6 @@ const (
type ProcessorHelper struct {
IsUsernameMentionable func(ctx context.Context, username string) bool
GetRepoFileContent func(ctx context.Context, ownerName, repoName, commitSha, filePath string) ([]template.HTML, error)
GetLocale func(ctx context.Context) (translation.Locale, error)
ElementDir string // the direction of the elements, eg: "ltr", "rtl", "auto", default to no direction attribute
}

View file

@ -3707,3 +3707,7 @@ normal_file = Normal file
executable_file = Executable file
symbolic_link = Symbolic link
submodule = Submodule
[markup]
filepreview.line = Line %[1]d in %[3]s
filepreview.lines = Lines %[1]d to %[2]d in %[3]s

View file

@ -17,7 +17,6 @@ import (
"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/translation"
gitea_context "code.gitea.io/gitea/services/context"
file_service "code.gitea.io/gitea/services/repository/files"
)
@ -100,18 +99,5 @@ func ProcessorHelper() *markup.ProcessorHelper {
return fileContent, nil
},
GetLocale: func(ctx context.Context) (translation.Locale, error) {
giteaCtx, ok := ctx.(*gitea_context.Context)
if ok {
return giteaCtx.Locale, nil
}
giteaBaseCtx, ok := ctx.(*gitea_context.Base)
if ok {
return giteaBaseCtx.Locale, nil
}
return nil, fmt.Errorf("could not retrieve locale from context")
},
}
}