summaryrefslogtreecommitdiffstats
path: root/modules/templates
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-04 00:58:09 +0800
committerGitHub <noreply@github.com>2023-04-03 12:58:09 -0400
commit19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2 (patch)
treef5e2f0a5431f315946aed6d72e40e5e9893dabc0 /modules/templates
parent01d9466bfdbb2e043a03368ca7872944db211f49 (diff)
downloadgitea-19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2.tar.gz
gitea-19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2.zip
Introduce GiteaLocaleNumber custom element to handle number localization on pages. (#23861)
Follow #21429 & #22861 Use `<gitea-locale-number>` instead of backend `PrettyNumber`. All old `PrettyNumber` related functions are removed. A lot of code could be simplified. And some functions haven't been used for long time (dead code), so they are also removed by the way (eg: `SplitStringAtRuneN`, `Dedent`) This PR only tries to improve the `PrettyNumber` rendering problem, it doesn't touch the "plural" problem. Screenshot: ![image](https://user-images.githubusercontent.com/2114189/229290804-1f63db65-1e34-4a54-84ba-e00b44331b17.png) ![image](https://user-images.githubusercontent.com/2114189/229290911-c88dea00-b11d-48dd-accb-9f52edd73ce4.png)
Diffstat (limited to 'modules/templates')
-rw-r--r--modules/templates/helper.go93
1 files changed, 18 insertions, 75 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index a8343428dc..54c85863bd 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -19,7 +19,6 @@ import (
"reflect"
"regexp"
"runtime"
- "strconv"
"strings"
texttmpl "text/template"
"time"
@@ -112,18 +111,17 @@ func NewFuncMap() []template.FuncMap {
"IsShowFullName": func() bool {
return setting.UI.DefaultShowFullName
},
- "Safe": Safe,
- "SafeJS": SafeJS,
- "JSEscape": JSEscape,
- "Str2html": Str2html,
- "TimeSince": timeutil.TimeSince,
- "TimeSinceUnix": timeutil.TimeSinceUnix,
- "FileSize": base.FileSize,
- "PrettyNumber": base.PrettyNumber,
- "JsPrettyNumber": JsPrettyNumber,
- "Subtract": base.Subtract,
- "EntryIcon": base.EntryIcon,
- "MigrationIcon": MigrationIcon,
+ "Safe": Safe,
+ "SafeJS": SafeJS,
+ "JSEscape": JSEscape,
+ "Str2html": Str2html,
+ "TimeSince": timeutil.TimeSince,
+ "TimeSinceUnix": timeutil.TimeSinceUnix,
+ "FileSize": base.FileSize,
+ "LocaleNumber": LocaleNumber,
+ "Subtract": base.Subtract,
+ "EntryIcon": base.EntryIcon,
+ "MigrationIcon": MigrationIcon,
"Add": func(a ...int) int {
sum := 0
for _, val := range a {
@@ -410,62 +408,9 @@ func NewFuncMap() []template.FuncMap {
"Join": strings.Join,
"QueryEscape": url.QueryEscape,
"DotEscape": DotEscape,
- "Iterate": func(arg interface{}) (items []uint64) {
- count := uint64(0)
- switch val := arg.(type) {
- case uint64:
- count = val
- case *uint64:
- count = *val
- case int64:
- if val < 0 {
- val = 0
- }
- count = uint64(val)
- case *int64:
- if *val < 0 {
- *val = 0
- }
- count = uint64(*val)
- case int:
- if val < 0 {
- val = 0
- }
- count = uint64(val)
- case *int:
- if *val < 0 {
- *val = 0
- }
- count = uint64(*val)
- case uint:
- count = uint64(val)
- case *uint:
- count = uint64(*val)
- case int32:
- if val < 0 {
- val = 0
- }
- count = uint64(val)
- case *int32:
- if *val < 0 {
- *val = 0
- }
- count = uint64(*val)
- case uint32:
- count = uint64(val)
- case *uint32:
- count = uint64(*val)
- case string:
- cnt, _ := strconv.ParseInt(val, 10, 64)
- if cnt < 0 {
- cnt = 0
- }
- count = uint64(cnt)
- }
- if count <= 0 {
- return items
- }
- for i := uint64(0); i < count; i++ {
+ "Iterate": func(arg interface{}) (items []int64) {
+ count := util.ToInt64(arg)
+ for i := int64(0); i < count; i++ {
items = append(items, i)
}
return items
@@ -1067,10 +1012,8 @@ func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteNa
return a
}
-// JsPrettyNumber renders a number using english decimal separators, e.g. 1,200 and subsequent
-// JS will replace the number with locale-specific separators, based on the user's selected language
-func JsPrettyNumber(i interface{}) template.HTML {
- num := util.NumberIntoInt64(i)
-
- return template.HTML(`<span class="js-pretty-number" data-value="` + strconv.FormatInt(num, 10) + `">` + base.PrettyNumber(num) + `</span>`)
+// LocaleNumber renders a number with a Custom Element, browser will render it with a locale number
+func LocaleNumber(v interface{}) template.HTML {
+ num := util.ToInt64(v)
+ return template.HTML(fmt.Sprintf(`<gitea-locale-number data-number="%d">%d</gitea-locale-number>`, num, num))
}