From c68298343ed7186e377a0b8b424a6fc2d180f2e0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 28 Mar 2023 14:40:35 +0800 Subject: [PATCH] use context key for locale --- modules/context/context.go | 4 +++- modules/markup/html.go | 25 +++++++++++++------------ modules/translation/translation.go | 4 ++++ routers/common/markup.go | 2 +- routers/web/repo/issue.go | 15 ++++++--------- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/modules/context/context.go b/modules/context/context.go index 5876e23cc40a..1eff1459a14a 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -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) } diff --git a/modules/markup/html.go b/modules/markup/html.go index 0c2702df19d0..f0ba70634736 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -5,7 +5,6 @@ package markup import ( "bytes" - "fmt" "io" "net/url" "path" @@ -809,8 +808,6 @@ 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) @@ -818,20 +815,24 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) { 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 @@ -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 } diff --git a/modules/translation/translation.go b/modules/translation/translation.go index 5a1009bfa3d8..3165390c3238 100644 --- a/modules/translation/translation.go +++ b/modules/translation/translation.go @@ -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 diff --git a/routers/common/markup.go b/routers/common/markup.go index 3eb3182d8e00..89f24e00075c 100644 --- a/routers/common/markup.go +++ b/routers/common/markup.go @@ -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, diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 136ed203e4d5..3715320f10c8 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -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) @@ -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) @@ -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)