diff --git a/templates/repo/release/new.tmpl b/templates/repo/release/new.tmpl
index c01f9a421..b63ad5dcd 100644
--- a/templates/repo/release/new.tmpl
+++ b/templates/repo/release/new.tmpl
@@ -64,7 +64,7 @@
-
{{.Size | FileSize}}
+
{{.Size | ctx.Locale.TrSize}}
{{svg "octicon-info"}}
diff --git a/templates/repo/settings/lfs.tmpl b/templates/repo/settings/lfs.tmpl
index e0864ff22..d63de3f53 100644
--- a/templates/repo/settings/lfs.tmpl
+++ b/templates/repo/settings/lfs.tmpl
@@ -16,7 +16,7 @@
{{ShortSha .Oid}}
-
{{FileSize .Size}} |
+
{{ctx.Locale.TrSize .Size}} |
{{TimeSince .CreatedUnix.AsTime ctx.Locale}} |
{{ctx.Locale.Tr "repo.settings.lfs_findcommits"}}
diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl
index 21a5471cb..8b73afe6d 100644
--- a/templates/repo/settings/options.tmpl
+++ b/templates/repo/settings/options.tmpl
@@ -14,7 +14,7 @@
- {{FileSize .Repository.Size}}
+ {{ctx.Locale.TrSize .Repository.Size}}
diff --git a/templates/repo/sub_menu.tmpl b/templates/repo/sub_menu.tmpl
index e43353ef0..32e7e9795 100644
--- a/templates/repo/sub_menu.tmpl
+++ b/templates/repo/sub_menu.tmpl
@@ -13,10 +13,9 @@
{{svg "octicon-tag"}} {{ctx.Locale.TrN .NumTags "repo.n_tag_one" "repo.n_tag_few" (printf "%d" .NumTags | SafeHTML)}}
{{end}}
-
- {{$fileSizeFormatted := FileSize .Repository.Size}}{{/* the formatted string is always "{val} {unit}" */}}
- {{$fileSizeFields := StringUtils.Split $fileSizeFormatted " "}}
- {{svg "octicon-database"}} {{ctx.Locale.PrettyNumber (index $fileSizeFields 0)}} {{index $fileSizeFields 1}}
+
+ {{$fileSizeFields := ctx.Locale.TrSize .Repository.Size }}
+ {{svg "octicon-database"}} {{$fileSizeFields.PrettyNumber}} {{$fileSizeFields.TranslatedUnit}}
{{end}}
diff --git a/templates/user/settings/repos.tmpl b/templates/user/settings/repos.tmpl
index c874ccd87..4fd0c37d5 100644
--- a/templates/user/settings/repos.tmpl
+++ b/templates/user/settings/repos.tmpl
@@ -24,7 +24,7 @@
{{svg "octicon-repo"}}
{{end}}
{{$repo.OwnerName}}/{{$repo.Name}}
- {{FileSize $repo.Size}}
+ {{ctx.Locale.TrSize $repo.Size}}
{{if $repo.IsFork}}
{{ctx.Locale.Tr "repo.forked_from"}}
{{$repo.BaseRepo.OwnerName}}/{{$repo.BaseRepo.Name}}
@@ -97,7 +97,7 @@
{{svg "octicon-repo" 16 "tw-mr-1 iconFloat"}}
{{end}}
{{.OwnerName}}/{{.Name}}
- {{FileSize .Size}}
+ {{ctx.Locale.TrSize .Size}}
{{if .IsFork}}
{{ctx.Locale.Tr "repo.forked_from"}}
{{.BaseRepo.OwnerName}}/{{.BaseRepo.Name}}
From 075e0de142d450f7e6083f56b153e6eacbd5a4a1 Mon Sep 17 00:00:00 2001
From: 0ko <0ko@noreply.codeberg.org>
Date: Tue, 19 Mar 2024 18:47:20 +0500
Subject: [PATCH 3/6] Add unit test for TrSize
---
modules/translation/translation_test.go | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/modules/translation/translation_test.go b/modules/translation/translation_test.go
index 464aa3266..bffbb155c 100644
--- a/modules/translation/translation_test.go
+++ b/modules/translation/translation_test.go
@@ -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)
From bba18845829a3c55c0a66aaaf67a600d7718c7b3 Mon Sep 17 00:00:00 2001
From: 0ko <0ko@noreply.codeberg.org>
Date: Tue, 19 Mar 2024 18:47:48 +0500
Subject: [PATCH 4/6] Add integration test for TrSize
---
tests/integration/integration_test.go | 10 ++
tests/integration/size_translations_test.go | 114 ++++++++++++++++++++
2 files changed, 124 insertions(+)
create mode 100644 tests/integration/size_translations_test.go
diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go
index e8f28105c..23567efc9 100644
--- a/tests/integration/integration_test.go
+++ b/tests/integration/integration_test.go
@@ -180,6 +180,16 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
return nil
}
+func (s *TestSession) SetCookie(cookie *http.Cookie) *http.Cookie {
+ baseURL, err := url.Parse(setting.AppURL)
+ if err != nil {
+ return nil
+ }
+
+ s.jar.SetCookies(baseURL, []*http.Cookie{cookie})
+ return nil
+}
+
func (s *TestSession) MakeRequest(t testing.TB, rw *RequestWrapper, expectedStatus int) *httptest.ResponseRecorder {
t.Helper()
req := rw.Request
diff --git a/tests/integration/size_translations_test.go b/tests/integration/size_translations_test.go
new file mode 100644
index 000000000..2edb0373c
--- /dev/null
+++ b/tests/integration/size_translations_test.go
@@ -0,0 +1,114 @@
+// Copyright 2024 The Forgejo Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+ "net/http"
+ "net/url"
+ "path"
+ "regexp"
+ "strings"
+ "testing"
+
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+ files_service "code.gitea.io/gitea/services/repository/files"
+ "github.com/PuerkitoBio/goquery"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestDataSizeTranslation(t *testing.T) {
+ onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
+ testUser := "user2"
+ testRepoName := "data_size_test"
+ noDigits := regexp.MustCompile("[0-9]+")
+ longString100 := `testRepoMigrate(t, session, "https://code.forgejo.org/forgejo/test_repo.git", testRepoName, struct)` + "\n"
+
+ // Login user
+ user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: testUser})
+ session := loginUser(t, testUser)
+
+ // Create test repo
+ testRepo, _, f := CreateDeclarativeRepo(t, user2, testRepoName, nil, nil,
+ []*files_service.ChangeRepoFile{
+ {
+ Operation: "create",
+ TreePath: "137byteFile.txt",
+ ContentReader: strings.NewReader(longString100 + strings.Repeat("1", 36) + "\n"),
+ },
+ {
+ Operation: "create",
+ TreePath: "1.5kibFile.txt",
+ ContentReader: strings.NewReader(strings.Repeat(longString100, 15) + strings.Repeat("1", 35) + "\n"),
+ },
+ {
+ Operation: "create",
+ TreePath: "1.25mibFile.txt",
+ ContentReader: strings.NewReader(strings.Repeat(longString100, 13107) + strings.Repeat("1", 19) + "\n"),
+ },
+ })
+ defer f()
+
+ // Change language from English to catch regressions that make translated sizes fall back to
+ // not translated, like to raw output of FileSize() or humanize.IBytes()
+ lang := session.GetCookie("lang")
+ lang.Value = "ru-RU"
+ session.SetCookie(lang)
+
+ // Go to /user/settings/repos
+ req := NewRequest(t, "GET", "user/settings/repos")
+ resp := session.MakeRequest(t, req, http.StatusOK)
+
+ // Check if repo size is translated
+ repos := NewHTMLParser(t, resp.Body).Find(".user-setting-content .list .item .content")
+ assert.True(t, repos.Length() > 0)
+ repos.Each(func(i int, repo *goquery.Selection) {
+ repoName := repo.Find("a.name").Text()
+ if repoName == path.Join(testUser, testRepo.Name) {
+ repoSize := repo.Find("span").Text()
+ repoSize = noDigits.ReplaceAllString(repoSize, "")
+ assert.Equal(t, " КиБ", repoSize)
+ }
+ })
+
+ // Go to /user2/repo1
+ req = NewRequest(t, "GET", path.Join(testUser, testRepoName))
+ resp = session.MakeRequest(t, req, http.StatusOK)
+
+ // Check if repo size in repo summary is translated
+ repo := NewHTMLParser(t, resp.Body).Find(".repository-summary span")
+ repoSize := strings.TrimSpace(repo.Text())
+ repoSize = noDigits.ReplaceAllString(repoSize, "")
+ assert.Equal(t, " КиБ", repoSize)
+
+ // Check if repo sizes in the tooltip are translated
+ fullSize, exists := repo.Attr("data-tooltip-content")
+ assert.True(t, exists)
+ fullSize = noDigits.ReplaceAllString(fullSize, "")
+ assert.Equal(t, "git: КиБ, lfs: Б", fullSize)
+
+ // Check if file sizes are correclty translated
+ testFileSizeTranslated(t, session, path.Join(testUser, testRepoName, "src/branch/main/137byteFile.txt"), "137 Б")
+ testFileSizeTranslated(t, session, path.Join(testUser, testRepoName, "src/branch/main/1.5kibFile.txt"), "1,5 КиБ")
+ testFileSizeTranslated(t, session, path.Join(testUser, testRepoName, "src/branch/main/1.25mibFile.txt"), "1,3 МиБ")
+ })
+}
+
+func testFileSizeTranslated(t *testing.T, session *TestSession, filePath, correctSize string) {
+ // Go to specified file page
+ req := NewRequest(t, "GET", filePath)
+ resp := session.MakeRequest(t, req, http.StatusOK)
+
+ // Check if file size is translated
+ sizeCorrent := false
+ fileInfo := NewHTMLParser(t, resp.Body).Find(".file-info .file-info-entry")
+ fileInfo.Each(func(i int, info *goquery.Selection) {
+ infoText := strings.TrimSpace(info.Text())
+ if infoText == correctSize {
+ sizeCorrent = true
+ }
+ })
+
+ assert.True(t, sizeCorrent)
+}
From dae95f473e64b0b5614625b692297c9bca943d82 Mon Sep 17 00:00:00 2001
From: 0ko <0ko@noreply.codeberg.org>
Date: Tue, 19 Mar 2024 18:47:53 +0500
Subject: [PATCH 5/6] Deprecate usage of FileSize in templates
---
modules/templates/helper.go | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index 18a2993fb..c9799a38e 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -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.")
+}
From c2d137d1f24cb03fad9685694529068276f5f302 Mon Sep 17 00:00:00 2001
From: 0ko <0ko@noreply.codeberg.org>
Date: Sun, 31 Mar 2024 20:17:12 +0500
Subject: [PATCH 6/6] Rename ByteSize to ReadableSize
---
modules/translation/mock.go | 4 ++--
modules/translation/translation.go | 12 ++++++------
templates/repo/sub_menu.tmpl | 2 +-
tests/integration/size_translations_test.go | 1 +
4 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/modules/translation/mock.go b/modules/translation/mock.go
index c4f8822db..fe3a1502e 100644
--- a/modules/translation/mock.go
+++ b/modules/translation/mock.go
@@ -31,8 +31,8 @@ func (l MockLocale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
return template.HTML(key1)
}
-func (l MockLocale) TrSize(s int64) ByteSize {
- return ByteSize{fmt.Sprint(s), ""}
+func (l MockLocale) TrSize(s int64) ReadableSize {
+ return ReadableSize{fmt.Sprint(s), ""}
}
func (l MockLocale) PrettyNumber(v any) string {
diff --git a/modules/translation/translation.go b/modules/translation/translation.go
index a6013a288..16eb55e28 100644
--- a/modules/translation/translation.go
+++ b/modules/translation/translation.go
@@ -15,8 +15,8 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation/i18n"
"code.gitea.io/gitea/modules/util"
- "github.com/dustin/go-humanize"
+ "github.com/dustin/go-humanize"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
@@ -34,7 +34,7 @@ type Locale interface {
Tr(key string, args ...any) template.HTML
TrN(cnt any, key1, keyN string, args ...any) template.HTML
- TrSize(size int64) ByteSize
+ TrSize(size int64) ReadableSize
PrettyNumber(v any) string
}
@@ -255,18 +255,18 @@ func (l *locale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
return l.Tr(keyN, args...)
}
-type ByteSize struct {
+type ReadableSize struct {
PrettyNumber string
TranslatedUnit string
}
-func (bs ByteSize) String() 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) ByteSize {
+func (l *locale) TrSize(s int64) ReadableSize {
us := uint64(s)
if s < 0 {
us = uint64(-s)
@@ -281,7 +281,7 @@ func (l *locale) TrSize(s int64) ByteSize {
}
numberVal = l.PrettyNumber(numberVal)
unitVal = l.TrString("munits.data." + strings.ToLower(unitVal))
- return ByteSize{numberVal, unitVal}
+ return ReadableSize{numberVal, unitVal}
}
func (l *locale) PrettyNumber(v any) string {
diff --git a/templates/repo/sub_menu.tmpl b/templates/repo/sub_menu.tmpl
index 32e7e9795..ddf01b33e 100644
--- a/templates/repo/sub_menu.tmpl
+++ b/templates/repo/sub_menu.tmpl
@@ -14,7 +14,7 @@
{{end}}
- {{$fileSizeFields := ctx.Locale.TrSize .Repository.Size }}
+ {{$fileSizeFields := ctx.Locale.TrSize .Repository.Size}}
{{svg "octicon-database"}} {{$fileSizeFields.PrettyNumber}} {{$fileSizeFields.TranslatedUnit}}
{{end}}
diff --git a/tests/integration/size_translations_test.go b/tests/integration/size_translations_test.go
index 2edb0373c..0a296ad38 100644
--- a/tests/integration/size_translations_test.go
+++ b/tests/integration/size_translations_test.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
files_service "code.gitea.io/gitea/services/repository/files"
+
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
)
|