Skip to content

Commit c682983

Browse files
committed
use context key for locale
1 parent d5f70c6 commit c682983

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

modules/context/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ func (ctx *Context) Value(key interface{}) interface{} {
628628
if key == git.RepositoryContextKey && ctx.Repo != nil {
629629
return ctx.Repo.GitRepo
630630
}
631-
631+
if key == translation.ContextKey && ctx.Locale != nil {
632+
return ctx.Locale
633+
}
632634
return ctx.Req.Context().Value(key)
633635
}
634636

modules/markup/html.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package markup
55

66
import (
77
"bytes"
8-
"fmt"
98
"io"
109
"net/url"
1110
"path"
@@ -809,29 +808,31 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
809808
if ctx.Metas == nil {
810809
return
811810
}
812-
fmt.Println("ctx.Metas")
813-
fmt.Println(ctx.Metas)
814811
next := node.NextSibling
815812
for node != nil && node != next {
816813
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
817814
if m == nil {
818815
return
819816
}
820817

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

827825
link := node.Data[m[0]:m[1]]
828826
id := "#" + node.Data[m[2]:m[3]]
829-
827+
text := id
830828
// if m[4] and m[5] is not -1, then link is to a comment
831829
// indicate that in the text by appending (comment)
832830
if m[4] != -1 && m[5] != -1 {
833-
locale := translation.NewLocale(ctx.Metas["language"])
834-
id += " " + locale.Tr("repo.from_comment")
831+
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
832+
text += " " + locale.Tr("repo.from_comment")
833+
} else {
834+
text += " " + " (comment)"
835+
}
835836
}
836837

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

843844
if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
844-
replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
845+
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
845846
} else {
846-
orgRepoID := matchOrg + "/" + matchRepo + id
847-
replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "ref-issue"))
847+
text = matchOrg + "/" + matchRepo + text
848+
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
848849
}
849850
node = node.NextSibling.NextSibling
850851
}

modules/translation/translation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"golang.org/x/text/language"
1919
)
2020

21+
type contextKey struct{}
22+
23+
var ContextKey interface{} = &contextKey{}
24+
2125
// Locale represents an interface to translation
2226
type Locale interface {
2327
Language() string

routers/common/markup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func RenderMarkup(ctx *context.Context, mode, text, urlPrefix, filePath string,
7373
if mode != "comment" {
7474
meta["mode"] = "document"
7575
}
76-
meta["language"] = ctx.Locale.Language()
76+
7777
if err := markup.Render(&markup.RenderContext{
7878
Ctx: ctx,
7979
URLPrefix: urlPrefix,

routers/web/repo/issue.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,11 +1319,10 @@ func ViewIssue(ctx *context.Context) {
13191319
}
13201320
}
13211321
ctx.Data["IssueWatch"] = iw
1322-
metas := ctx.Repo.Repository.ComposeMetas()
1323-
metas["language"] = ctx.Locale.Language()
1322+
13241323
issue.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
13251324
URLPrefix: ctx.Repo.RepoLink,
1326-
Metas: metas,
1325+
Metas: ctx.Repo.Repository.ComposeMetas(),
13271326
GitRepo: ctx.Repo.GitRepo,
13281327
Ctx: ctx,
13291328
}, issue.Content)
@@ -2022,11 +2021,10 @@ func UpdateIssueContent(ctx *context.Context) {
20222021
return
20232022
}
20242023
}
2025-
metas := ctx.Repo.Repository.ComposeMetas()
2026-
metas["language"] = ctx.Locale.Language()
2024+
20272025
content, err := markdown.RenderString(&markup.RenderContext{
20282026
URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
2029-
Metas: metas,
2027+
Metas: ctx.Repo.Repository.ComposeMetas(),
20302028
GitRepo: ctx.Repo.GitRepo,
20312029
Ctx: ctx,
20322030
}, issue.Content)
@@ -2831,11 +2829,10 @@ func UpdateCommentContent(ctx *context.Context) {
28312829
return
28322830
}
28332831
}
2834-
metas := ctx.Repo.Repository.ComposeMetas()
2835-
metas["language"] = ctx.Locale.Language()
2832+
28362833
content, err := markdown.RenderString(&markup.RenderContext{
28372834
URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
2838-
Metas: metas,
2835+
Metas: ctx.Repo.Repository.ComposeMetas(),
28392836
GitRepo: ctx.Repo.GitRepo,
28402837
Ctx: ctx,
28412838
}, comment.Content)

0 commit comments

Comments
 (0)