Skip to content

Commit

Permalink
use context key for locale
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 28, 2023
1 parent d5f70c6 commit c682983
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
4 changes: 3 additions & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ func (ctx *Context) Value(key interface{}) interface{} {
if key == git.RepositoryContextKey && ctx.Repo != nil {
return ctx.Repo.GitRepo
}

if key == translation.ContextKey && ctx.Locale != nil {
return ctx.Locale
}
return ctx.Req.Context().Value(key)
}

Expand Down
25 changes: 13 additions & 12 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package markup

import (
"bytes"
"fmt"
"io"
"net/url"
"path"
Expand Down Expand Up @@ -809,29 +808,31 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
return
}
fmt.Println("ctx.Metas")
fmt.Println(ctx.Metas)
next := node.NextSibling
for node != nil && node != next {
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
if m == nil {
return
}

Changedm := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
// if the link is from files changed tab in pull requests, leave it as it is
if Changedm != nil {
mDiffView := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
// if the link is from "Files Changed" tab in pull requests https://domain/org/repo/pulls/27/files (aka: the files diff view)
// leave it as it is
if mDiffView != nil {
return
}

link := node.Data[m[0]:m[1]]
id := "#" + node.Data[m[2]:m[3]]

text := id
// if m[4] and m[5] is not -1, then link is to a comment
// indicate that in the text by appending (comment)
if m[4] != -1 && m[5] != -1 {
locale := translation.NewLocale(ctx.Metas["language"])
id += " " + locale.Tr("repo.from_comment")
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
text += " " + locale.Tr("repo.from_comment")
} else {
text += " " + " (comment)"
}
}

// extract repo and org name from matched link like
Expand All @@ -841,10 +842,10 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
matchRepo := linkParts[len(linkParts)-3]

if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
} else {
orgRepoID := matchOrg + "/" + matchRepo + id
replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "ref-issue"))
text = matchOrg + "/" + matchRepo + text
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
}
node = node.NextSibling.NextSibling
}
Expand Down
4 changes: 4 additions & 0 deletions modules/translation/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"golang.org/x/text/language"
)

type contextKey struct{}

var ContextKey interface{} = &contextKey{}

// Locale represents an interface to translation
type Locale interface {
Language() string
Expand Down
2 changes: 1 addition & 1 deletion routers/common/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RenderMarkup(ctx *context.Context, mode, text, urlPrefix, filePath string,
if mode != "comment" {
meta["mode"] = "document"
}
meta["language"] = ctx.Locale.Language()

if err := markup.Render(&markup.RenderContext{
Ctx: ctx,
URLPrefix: urlPrefix,
Expand Down
15 changes: 6 additions & 9 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,11 +1319,10 @@ func ViewIssue(ctx *context.Context) {
}
}
ctx.Data["IssueWatch"] = iw
metas := ctx.Repo.Repository.ComposeMetas()
metas["language"] = ctx.Locale.Language()

issue.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.Repo.RepoLink,
Metas: metas,
Metas: ctx.Repo.Repository.ComposeMetas(),
GitRepo: ctx.Repo.GitRepo,
Ctx: ctx,
}, issue.Content)
Expand Down Expand Up @@ -2022,11 +2021,10 @@ func UpdateIssueContent(ctx *context.Context) {
return
}
}
metas := ctx.Repo.Repository.ComposeMetas()
metas["language"] = ctx.Locale.Language()

content, err := markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
Metas: metas,
Metas: ctx.Repo.Repository.ComposeMetas(),
GitRepo: ctx.Repo.GitRepo,
Ctx: ctx,
}, issue.Content)
Expand Down Expand Up @@ -2831,11 +2829,10 @@ func UpdateCommentContent(ctx *context.Context) {
return
}
}
metas := ctx.Repo.Repository.ComposeMetas()
metas["language"] = ctx.Locale.Language()

content, err := markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
Metas: metas,
Metas: ctx.Repo.Repository.ComposeMetas(),
GitRepo: ctx.Repo.GitRepo,
Ctx: ctx,
}, comment.Content)
Expand Down

0 comments on commit c682983

Please sign in to comment.