Skip to content

Localize numbers server-side (maintainers' opinion requested) #21770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
"code.gitea.io/gitea/services/gitdiff"

"github.com/editorconfig/editorconfig-core-go/v2"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

// Used from static.go && dynamic.go
Expand Down Expand Up @@ -170,6 +172,7 @@ func NewFuncMap() []template.FuncMap {
"RenderEmojiPlain": emoji.ReplaceAliases,
"ReactionToEmoji": ReactionToEmoji,
"RenderNote": RenderNote,
"RenderNumber": RenderNumber,
"RenderMarkdownToHtml": func(input string) template.HTML {
output, err := markdown.RenderString(&markup.RenderContext{
URLPrefix: setting.AppSubURL,
Expand Down Expand Up @@ -782,6 +785,13 @@ func RenderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
return template.HTML(htmlWithCodeTags)
}

// RenderNumber render any number according to the given language code (e.g. 1234 -> 1,234)
// Should RenderNumber, JsPrettyNumber, and CountFmt coexist on this codebase? RenderNumber is server-rendered and localized
func RenderNumber(number int64, languageCode string) template.HTML {
formatter := message.NewPrinter(language.MustParse(languageCode))
return template.HTML(formatter.Sprintf("%d", number))
}

// RenderIssueTitle renders issue/pull title with defined post processors
func RenderIssueTitle(ctx context.Context, text, urlPrefix string, metas map[string]string) template.HTML {
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{
Expand Down
2 changes: 1 addition & 1 deletion templates/code/searchresults.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<a class="ui text-label df ac mr-1 my-1 {{if eq $.Language $term.Language}}primary {{end}}basic label" href="{{AppSubUrl}}{{if $.ContextUser}}/{{$.ContextUser.Name}}/-/code{{else}}/explore/code{{end}}?q={{$.Keyword}}{{if ne $.Language $term.Language}}&l={{$term.Language}}{{end}}{{if ne $.queryType ""}}&t={{$.queryType}}{{end}}">
<i class="color-icon mr-3" style="background-color: {{$term.Color}}"></i>
{{$term.Language}}
<div class="detail">{{$term.Count}}</div>
<div class="detail">{{RenderNumber $term.Count $.locale.Language}}</div>
</a>
{{end}}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<div class="item">{{svg "octicon-repo" 16 "mr-3"}} <a href="{{.PackageDescriptor.Repository.HTMLURL}}">{{.PackageDescriptor.Repository.FullName}}</a></div>
{{end}}
<div class="item">{{svg "octicon-calendar" 16 "mr-3"}} {{TimeSinceUnix .PackageDescriptor.Version.CreatedUnix $.locale}}</div>
<div class="item">{{svg "octicon-download" 16 "mr-3"}} {{.PackageDescriptor.Version.DownloadCount}}</div>
<div class="item">{{svg "octicon-download" 16 "mr-3"}} {{RenderNumber .PackageDescriptor.Version.DownloadCount $.locale.Language}}</div>
{{template "package/metadata/composer" .}}
{{template "package/metadata/conan" .}}
{{template "package/metadata/container" .}}
Expand Down
8 changes: 4 additions & 4 deletions templates/repo/activity.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@
<div class="ui attached segment horizontal segments">
{{if .Permission.CanRead $.UnitTypePullRequests}}
<a href="#merged-pull-requests" class="ui attached segment text center">
<span class="text purple">{{svg "octicon-git-pull-request"}}</span> <strong>{{.Activity.MergedPRCount}}</strong><br>
<span class="text purple">{{svg "octicon-git-pull-request"}}</span> <strong>{{RenderNumber .Activity.MergedPRCount $.locale.Language}}</strong><br>
{{.locale.TrN .Activity.MergedPRCount "repo.activity.merged_prs_count_1" "repo.activity.merged_prs_count_n"}}
</a>
<a href="#proposed-pull-requests" class="ui attached segment text center">
<span class="text green">{{svg "octicon-git-branch"}}</span> <strong>{{.Activity.OpenedPRCount}}</strong><br>
<span class="text green">{{svg "octicon-git-branch"}}</span> <strong>{{RenderNumber .Activity.OpenedPRCount $.locale.Language}}</strong><br>
{{.locale.TrN .Activity.OpenedPRCount "repo.activity.opened_prs_count_1" "repo.activity.opened_prs_count_n"}}
</a>
{{end}}
{{if .Permission.CanRead $.UnitTypeIssues}}
<a href="#closed-issues" class="ui attached segment text center">
<span class="text red">{{svg "octicon-issue-closed"}}</span> <strong>{{.Activity.ClosedIssueCount}}</strong><br>
<span class="text red">{{svg "octicon-issue-closed"}}</span> <strong>{{RenderNumber .Activity.ClosedIssueCount $.locale.Language}}</strong><br>
{{.locale.TrN .Activity.ClosedIssueCount "repo.activity.closed_issues_count_1" "repo.activity.closed_issues_count_n"}}
</a>
<a href="#new-issues" class="ui attached segment text center">
<span class="text green">{{svg "octicon-issue-opened"}}</span> <strong>{{.Activity.OpenedIssueCount}}</strong><br>
<span class="text green">{{svg "octicon-issue-opened"}}</span> <strong>{{RenderNumber .Activity.OpenedIssueCount $.locale.Language}}</strong><br>
{{.locale.TrN .Activity.OpenedIssueCount "repo.activity.new_issues_count_1" "repo.activity.new_issues_count_n"}}
</a>
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/commits_table.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h4 class="ui top attached header commits-table df ac sb">
<div class="commits-table-left df ac">
{{if or .PageIsCommits (gt .CommitCount 0)}}
{{.CommitCount}} {{.locale.Tr "repo.commits.commits"}} {{if .RefName}}({{.RefName}}){{end}}
{{RenderNumber .CommitCount .locale.Language}} {{.locale.Tr "repo.commits.commits"}} {{if .RefName}}({{.RefName}}){{end}}
{{else if .IsNothingToCompare}}
{{.locale.Tr "repo.commits.nothing_to_compare"}} {{if .RefName}}({{.RefName}}){{end}}
{{else}}
Expand Down
6 changes: 3 additions & 3 deletions templates/repo/sub_menu.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<div class="ui two horizontal center link list">
{{if and (.Permission.CanRead $.UnitTypeCode) (not .IsEmptyRepo)}}
<div class="item{{if .PageIsCommits}} active{{end}}">
<a class="ui" href="{{.RepoLink}}/commits/{{.BranchNameSubURL}}">{{svg "octicon-history"}} <b>{{.CommitsCount}}</b> {{.locale.TrN .CommitsCount "repo.commit" "repo.commits"}}</a>
<a class="ui" href="{{.RepoLink}}/commits/{{.BranchNameSubURL}}">{{svg "octicon-history"}} <b>{{RenderNumber .CommitsCount .locale.Language}}</b> {{.locale.TrN .CommitsCount "repo.commit" "repo.commits"}}</a>
</div>
<div class="item{{if .PageIsBranches}} active{{end}}">
<a class="ui" href="{{.RepoLink}}/branches">{{svg "octicon-git-branch"}} <b>{{.BranchesCount}}</b> {{.locale.TrN .BranchesCount "repo.branch" "repo.branches"}}</a>
<a class="ui" href="{{.RepoLink}}/branches">{{svg "octicon-git-branch"}} <b>{{RenderNumber .BranchesCount .locale.Language}}</b> {{.locale.TrN .BranchesCount "repo.branch" "repo.branches"}}</a>
</div>
{{if $.Permission.CanRead $.UnitTypeCode}}
<div class="item">
<a class="ui" href="{{.RepoLink}}/tags">{{svg "octicon-tag"}} <b>{{.NumTags}}</b> {{.locale.TrN .NumTags "repo.tag" "repo.tags"}}</a>
<a class="ui" href="{{.RepoLink}}/tags">{{svg "octicon-tag"}} <b>{{RenderNumber .NumTags .locale.Language}}</b> {{.locale.TrN .NumTags "repo.tag" "repo.tags"}}</a>
</div>
{{end}}
<div class="item">
Expand Down