1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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)
This commit is contained in:
wxiaoguang
2023-04-04 00:58:09 +08:00
committed by GitHub
parent 01d9466bfd
commit 19de52e0f4
24 changed files with 94 additions and 227 deletions

View File

@@ -35,27 +35,3 @@ func SplitStringAtByteN(input string, n int) (left, right string) {
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
}
// SplitStringAtRuneN splits a string at rune n accounting for rune boundaries. (Combining characters are not accounted for.)
func SplitStringAtRuneN(input string, n int) (left, right string) {
if !utf8.ValidString(input) {
if len(input) <= n || n-3 < 0 {
return input, ""
}
return input[:n-3] + asciiEllipsis, asciiEllipsis + input[n-3:]
}
if utf8.RuneCountInString(input) <= n {
return input, ""
}
count := 0
end := 0
for count < n-1 {
_, size := utf8.DecodeRuneInString(input[end:])
end += size
count++
}
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
}

View File

@@ -43,18 +43,4 @@ func TestSplitString(t *testing.T) {
{"\xef\x03", 1, "\xef\x03", ""},
}
test(tc, SplitStringAtByteN)
tc = []*testCase{
{"abc123xyz", 0, "", utf8Ellipsis},
{"abc123xyz", 1, "", utf8Ellipsis},
{"abc123xyz", 4, "abc", utf8Ellipsis},
{"啊bc123xyz", 4, "啊bc", utf8Ellipsis},
{"啊bc123xyz", 6, "啊bc12", utf8Ellipsis},
{"啊bc", 3, "啊bc", ""},
{"啊bc", 4, "啊bc", ""},
{"abc\xef\x03\xfe", 3, "", asciiEllipsis},
{"abc\xef\x03\xfe", 4, "a", asciiEllipsis},
{"\xef\x03", 1, "\xef\x03", ""},
}
test(tc, SplitStringAtRuneN)
}

View File

@@ -7,8 +7,9 @@ import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"math/big"
"regexp"
"os"
"strconv"
"strings"
@@ -200,40 +201,14 @@ func ToTitleCaseNoLower(s string) string {
return titleCaserNoLower.String(s)
}
var (
whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$")
leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])")
)
// Dedent removes common indentation of a multi-line string along with whitespace around it
// Based on https://github.com/lithammer/dedent
func Dedent(s string) string {
var margin string
s = whitespaceOnly.ReplaceAllString(s, "")
indents := leadingWhitespace.FindAllStringSubmatch(s, -1)
for i, indent := range indents {
if i == 0 {
margin = indent[1]
} else if strings.HasPrefix(indent[1], margin) {
continue
} else if strings.HasPrefix(margin, indent[1]) {
margin = indent[1]
} else {
margin = ""
break
}
}
if margin != "" {
s = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(s, "")
}
return strings.TrimSpace(s)
func logError(msg string, args ...any) {
// TODO: the "util" package can not import the "modules/log" package, so we use the "fmt" package here temporarily.
// In the future, we should decouple the dependency between them.
_, _ = fmt.Fprintf(os.Stderr, msg, args...)
}
// NumberIntoInt64 transform a given int into int64.
func NumberIntoInt64(number interface{}) int64 {
// ToInt64 transform a given int into int64.
func ToInt64(number interface{}) int64 {
var value int64
switch v := number.(type) {
case int:
@@ -246,6 +221,23 @@ func NumberIntoInt64(number interface{}) int64 {
value = int64(v)
case int64:
value = v
case uint:
value = int64(v)
case uint8:
value = int64(v)
case uint16:
value = int64(v)
case uint32:
value = int64(v)
case uint64:
value = int64(v)
case string:
var err error
if value, err = strconv.ParseInt(v, 10, 64); err != nil {
logError("strconv.ParseInt failed for %q: %v", v, err)
}
default:
logError("unable to convert %q to int64", v)
}
return value
}

View File

@@ -224,10 +224,3 @@ func TestToTitleCase(t *testing.T) {
assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`)
assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`)
}
func TestDedent(t *testing.T) {
assert.Equal(t, Dedent(`
foo
bar
`), "foo\n\tbar")
}