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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 94 additions and 227 deletions

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
}