Merge pull request 'Data size unit localization' (#2528) from 0ko/forgejo:sizelocalize into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2528 Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
commit
385bcca788
25 changed files with 237 additions and 32 deletions
|
@ -63,7 +63,7 @@ func NewFuncMap() template.FuncMap {
|
|||
|
||||
// -----------------------------------------------------------------
|
||||
// time / number / format
|
||||
"FileSize": base.FileSize,
|
||||
"FileSize": FileSizePanic,
|
||||
"CountFmt": base.FormatNumberSI,
|
||||
"TimeSince": timeutil.TimeSince,
|
||||
"TimeSinceUnix": timeutil.TimeSinceUnix,
|
||||
|
@ -249,3 +249,7 @@ func Eval(tokens ...any) (any, error) {
|
|||
n, err := eval.Expr(tokens...)
|
||||
return n.Value, err
|
||||
}
|
||||
|
||||
func FileSizePanic(s int64) string {
|
||||
panic("Usage of FileSize in templates is deprecated in Forgejo. Locale.TrSize should be used instead.")
|
||||
}
|
||||
|
|
|
@ -31,6 +31,10 @@ func (l MockLocale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
|
|||
return template.HTML(key1)
|
||||
}
|
||||
|
||||
func (l MockLocale) TrSize(s int64) ReadableSize {
|
||||
return ReadableSize{fmt.Sprint(s), ""}
|
||||
}
|
||||
|
||||
func (l MockLocale) PrettyNumber(v any) string {
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/translation/i18n"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/message"
|
||||
"golang.org/x/text/number"
|
||||
|
@ -33,6 +34,8 @@ type Locale interface {
|
|||
Tr(key string, args ...any) template.HTML
|
||||
TrN(cnt any, key1, keyN string, args ...any) template.HTML
|
||||
|
||||
TrSize(size int64) ReadableSize
|
||||
|
||||
PrettyNumber(v any) string
|
||||
}
|
||||
|
||||
|
@ -252,6 +255,35 @@ func (l *locale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
|
|||
return l.Tr(keyN, args...)
|
||||
}
|
||||
|
||||
type ReadableSize struct {
|
||||
PrettyNumber string
|
||||
TranslatedUnit string
|
||||
}
|
||||
|
||||
func (bs ReadableSize) String() string {
|
||||
return bs.PrettyNumber + " " + bs.TranslatedUnit
|
||||
}
|
||||
|
||||
// TrSize returns array containing pretty formatted size and localized output of FileSize
|
||||
// output of humanize.IBytes has to be split in order to be localized
|
||||
func (l *locale) TrSize(s int64) ReadableSize {
|
||||
us := uint64(s)
|
||||
if s < 0 {
|
||||
us = uint64(-s)
|
||||
}
|
||||
untranslated := humanize.IBytes(us)
|
||||
if s < 0 {
|
||||
untranslated = "-" + untranslated
|
||||
}
|
||||
numberVal, unitVal, found := strings.Cut(untranslated, " ")
|
||||
if !found {
|
||||
log.Error("no space in go-humanized size of %d: %q", s, untranslated)
|
||||
}
|
||||
numberVal = l.PrettyNumber(numberVal)
|
||||
unitVal = l.TrString("munits.data." + strings.ToLower(unitVal))
|
||||
return ReadableSize{numberVal, unitVal}
|
||||
}
|
||||
|
||||
func (l *locale) PrettyNumber(v any) string {
|
||||
// TODO: this mechanism is not good enough, the complete solution is to switch the translation system to ICU message format
|
||||
if s, ok := v.(string); ok {
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
package translation
|
||||
|
||||
// TODO: make this package friendly to testing
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
|
@ -11,9 +13,25 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPrettyNumber(t *testing.T) {
|
||||
// TODO: make this package friendly to testing
|
||||
func TestTrSize(t *testing.T) {
|
||||
l := NewLocale("")
|
||||
size := int64(1)
|
||||
assert.EqualValues(t, "1 munits.data.b", l.TrSize(size).String())
|
||||
size *= 2048
|
||||
assert.EqualValues(t, "2 munits.data.kib", l.TrSize(size).String())
|
||||
size *= 2048
|
||||
assert.EqualValues(t, "4 munits.data.mib", l.TrSize(size).String())
|
||||
size *= 2048
|
||||
assert.EqualValues(t, "8 munits.data.gib", l.TrSize(size).String())
|
||||
size *= 2048
|
||||
assert.EqualValues(t, "16 munits.data.tib", l.TrSize(size).String())
|
||||
size *= 2048
|
||||
assert.EqualValues(t, "32 munits.data.pib", l.TrSize(size).String())
|
||||
size *= 128
|
||||
assert.EqualValues(t, "4 munits.data.eib", l.TrSize(size).String())
|
||||
}
|
||||
|
||||
func TestPrettyNumber(t *testing.T) {
|
||||
i18n.ResetDefaultLocales()
|
||||
|
||||
allLangMap = make(map[string]*LangType)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue