1
0

Localization of size units.

This commit is contained in:
Alexander Andreev 2024-12-30 15:45:23 +04:00
parent 28488a75c2
commit 3bb26f397c
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
5 changed files with 28 additions and 12 deletions

View File

@ -10,9 +10,10 @@ import (
const FileDateFormat = "2006-01-02 15:04:05 MST"
type DirStat struct {
Files int64
FilesSize string
Directories int64
Files int64
FilesSize string
FilesSizeUnit string
Directories int64
}
type DirEntry struct {
@ -20,6 +21,7 @@ type DirEntry struct {
Link string
Datetime time.Time
Size string
SizeUnit string
}
// ScanDirectory returns entries of directory which is located by its relative
@ -58,11 +60,13 @@ func ScanDirectory(base, rel string) (entries []DirEntry, stats DirStat, err err
})
stats.Directories++
} else {
sz, ui := convertFileSize(entry.Size())
fileEntries = append(fileEntries, DirEntry{
Name: entry.Name(),
Link: "/file" + rel + url.PathEscape(entry.Name()),
Datetime: entry.ModTime(),
Size: convertFileSize(entry.Size()),
Size: sz,
SizeUnit: ui,
})
totalFilesSize += entry.Size()
@ -70,7 +74,7 @@ func ScanDirectory(base, rel string) (entries []DirEntry, stats DirStat, err err
}
}
stats.FilesSize = convertFileSize(totalFilesSize)
stats.FilesSize, stats.FilesSizeUnit = convertFileSize(totalFilesSize)
entries = append(entries, dirEntries...)
entries = append(entries, fileEntries...)

View File

@ -8,9 +8,9 @@ import (
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"}
// convertFileSize converts size in bytes down to biggest units it represents.
// Returns a concatenation of a converted size and a unit
func convertFileSize(size int64) string {
var idx int
// Returns a converted size string and a unit idx
func convertFileSize(size int64) (string, string) {
var idx uint8
var fSize float64 = float64(size)
for idx = 0; fSize >= 1024; fSize /= 1024 {
idx++
@ -18,5 +18,5 @@ func convertFileSize(size int64) string {
fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64)
fSizeStr = strings.TrimRight(fSizeStr, "0")
fSizeStr = strings.TrimSuffix(fSizeStr, ".")
return fSizeStr + " " + sizeSuffixes[idx]
return fSizeStr, sizeSuffixes[idx]
}

View File

@ -74,7 +74,7 @@ templ Index(currentPath, progVer string, stat *files.DirStat, entries *[]files.D
<tr tabindex={ strconv.FormatInt(int64(i)+1, 10) }>
<td><a href={ templ.SafeURL(entry.Link) }>{ entry.Name }</a></td>
<td>{ utils.ToClientTimezone(entry.Datetime, r).Format(files.FileDateFormat) }</td>
<td>{ entry.Size }</td>
<td>{ entry.Size + " " + i18n.T(ctx, "size-unit."+entry.SizeUnit) }</td>
</tr>
}
</tbody>

View File

@ -17,4 +17,10 @@ en:
autoplay: Autoplay
footer:
author: Alexander ❝Arav❞ Andreev
privacy: Privacy statements
privacy: Privacy statements
size-unit:
B: "B"
KiB: "KiB"
MiB: "MiB"
GiB: "GiB"
TiB: "TiB"

View File

@ -17,4 +17,10 @@ ru:
autoplay: Автовоспроизведение
footer:
author: Александр «Arav» Андреев
privacy: О приватности
privacy: О приватности
size-unit:
B: "Б"
KiB: "КиБ"
MiB: "МиБ"
GiB: "ГиБ"
TiB: "ТиБ"