l10n: localizable upload progress messages

Currenly the data rate in throughput_string(...) method is
output by simple strbuf_humanise_bytes(...) call and '/s' append.
But for proper translation of such string the translator needs
full context.

Add strbuf_humanise_rate(...) method to properly print out
localizable version of data rate ('3.5 MiB/s' etc) with full context.

Strings with the units in strbuf_humanise_bytes(...) are marked
for translation.

Signed-off-by: Dimitriy Ryazantcev <dimitriy.ryazantcev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Dimitriy Ryazantcev 2019-07-02 21:22:48 +03:00 committed by Junio C Hamano
parent 8dca754b1e
commit 8f354a1fae
3 changed files with 44 additions and 7 deletions

View File

@ -150,8 +150,7 @@ static void throughput_string(struct strbuf *buf, uint64_t total,
strbuf_addstr(buf, ", "); strbuf_addstr(buf, ", ");
strbuf_humanise_bytes(buf, total); strbuf_humanise_bytes(buf, total);
strbuf_addstr(buf, " | "); strbuf_addstr(buf, " | ");
strbuf_humanise_bytes(buf, rate * 1024); strbuf_humanise_rate(buf, rate * 1024);
strbuf_addstr(buf, "/s");
} }
void display_throughput(struct progress *progress, uint64_t total) void display_throughput(struct progress *progress, uint64_t total)

View File

@ -811,25 +811,57 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
strbuf_add_urlencode(sb, s, strlen(s), reserved); strbuf_add_urlencode(sb, s, strlen(s), reserved);
} }
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes) static void strbuf_humanise(struct strbuf *buf, off_t bytes,
int humanise_rate)
{ {
if (bytes > 1 << 30) { if (bytes > 1 << 30) {
strbuf_addf(buf, "%u.%2.2u GiB", strbuf_addf(buf,
humanise_rate == 0 ?
/* TRANSLATORS: IEC 80000-13:2008 gibibyte */
_("%u.%2.2u GiB") :
/* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
_("%u.%2.2u GiB/s"),
(unsigned)(bytes >> 30), (unsigned)(bytes >> 30),
(unsigned)(bytes & ((1 << 30) - 1)) / 10737419); (unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
} else if (bytes > 1 << 20) { } else if (bytes > 1 << 20) {
unsigned x = bytes + 5243; /* for rounding */ unsigned x = bytes + 5243; /* for rounding */
strbuf_addf(buf, "%u.%2.2u MiB", strbuf_addf(buf,
humanise_rate == 0 ?
/* TRANSLATORS: IEC 80000-13:2008 mebibyte */
_("%u.%2.2u MiB") :
/* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
_("%u.%2.2u MiB/s"),
x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20); x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
} else if (bytes > 1 << 10) { } else if (bytes > 1 << 10) {
unsigned x = bytes + 5; /* for rounding */ unsigned x = bytes + 5; /* for rounding */
strbuf_addf(buf, "%u.%2.2u KiB", strbuf_addf(buf,
humanise_rate == 0 ?
/* TRANSLATORS: IEC 80000-13:2008 kibibyte */
_("%u.%2.2u KiB") :
/* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
_("%u.%2.2u KiB/s"),
x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10); x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
} else { } else {
strbuf_addf(buf, "%u bytes", (unsigned)bytes); strbuf_addf(buf,
humanise_rate == 0 ?
/* TRANSLATORS: IEC 80000-13:2008 byte */
Q_("%u byte", "%u bytes", (unsigned)bytes) :
/* TRANSLATORS: IEC 80000-13:2008 byte/second */
Q_("%u byte/s", "%u bytes/s", (unsigned)bytes),
(unsigned)bytes);
} }
} }
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
{
strbuf_humanise(buf, bytes, 0);
}
void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
{
strbuf_humanise(buf, bytes, 1);
}
void strbuf_add_absolute_path(struct strbuf *sb, const char *path) void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
{ {
if (!*path) if (!*path)

View File

@ -372,6 +372,12 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
*/ */
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes); void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
/**
* Append the given byte rate as a human-readable string (i.e. 12.23 KiB/s,
* 3.50 MiB/s).
*/
void strbuf_humanise_rate(struct strbuf *buf, off_t bytes);
/** /**
* Add a formatted string to the buffer. * Add a formatted string to the buffer.
*/ */