mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-20 16:10:50 +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:  
This commit is contained in:
parent
01d9466bfd
commit
19de52e0f4
24 changed files with 94 additions and 227 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue