ActivityMonitor: use BNumberFormat for i18n
Change-Id: I3179f84cbaee25624c2f4a7b092a28b5281a5f16 Reviewed-on: https://review.haiku-os.org/c/haiku/+/7480 Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org> Reviewed-by: Niels Sascha Reedijk <niels.reedijk@gmail.com>
This commit is contained in:
parent
1ee3891ab8
commit
1f5daef0bf
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Copyright 2024, Emir SARI, emir_sari@icloud.com.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
@ -13,6 +14,7 @@
|
|||
#include <OS.h>
|
||||
#include <String.h>
|
||||
#include <StringForRate.h>
|
||||
#include <StringForSize.h>
|
||||
|
||||
#include "SystemInfo.h"
|
||||
|
||||
|
@ -146,7 +148,7 @@ void
|
|||
DataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
text = "";
|
||||
text << value;
|
||||
fNumberFormat.Format(text, (int32)value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -292,19 +294,11 @@ void
|
|||
MemoryDataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f MiB"), value / 1048576.0);
|
||||
|
||||
string_for_size(value, buffer, sizeof(buffer));
|
||||
text = buffer;
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
MemoryDataSource::Unit() const
|
||||
{
|
||||
return B_TRANSLATE("MiB");
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark -
|
||||
|
||||
|
||||
|
@ -856,7 +850,9 @@ CPUFrequencyDataSource::CopyForCPU(int32 cpu) const
|
|||
void
|
||||
CPUFrequencyDataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
text.SetToFormat("%" PRId64 " MHz", value / 1000000);
|
||||
BString printedFrequency;
|
||||
fNumberFormat.Format(printedFrequency, (int32)(value / 1000000));
|
||||
text.SetToFormat("%s MHz", printedFrequency.String());
|
||||
}
|
||||
|
||||
|
||||
|
@ -1002,10 +998,9 @@ CPUUsageDataSource::CopyForCPU(int32 cpu) const
|
|||
void
|
||||
CPUUsageDataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0);
|
||||
|
||||
text = buffer;
|
||||
text = "";
|
||||
fNumberFormat.SetPrecision(1);
|
||||
fNumberFormat.FormatPercent(text, value / 1000.0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1147,10 +1142,9 @@ CPUCombinedUsageDataSource::Copy() const
|
|||
void
|
||||
CPUCombinedUsageDataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0);
|
||||
|
||||
text = buffer;
|
||||
text = "";
|
||||
fNumberFormat.SetPrecision(1);
|
||||
fNumberFormat.FormatPercent(text, value / 1000.0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1263,11 +1257,10 @@ PageFaultsDataSource::Copy() const
|
|||
void
|
||||
PageFaultsDataSource::Print(BString& text, int64 value) const
|
||||
{
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f faults/s"),
|
||||
value / 1024.0);
|
||||
|
||||
text = buffer;
|
||||
BString printedPageFaults;
|
||||
fNumberFormat.SetPrecision(1);
|
||||
fNumberFormat.Format(printedPageFaults, value / 1024.0);
|
||||
text.SetToFormat(B_TRANSLATE("%s faults/s"), printedPageFaults.String());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
|
||||
#include <InterfaceDefs.h>
|
||||
#include <NumberFormat.h>
|
||||
#include <String.h>
|
||||
|
||||
class SystemInfo;
|
||||
|
@ -61,6 +62,8 @@ protected:
|
|||
int64 fMaximum;
|
||||
bigtime_t fInterval;
|
||||
rgb_color fColor;
|
||||
|
||||
mutable BNumberFormat fNumberFormat;
|
||||
};
|
||||
|
||||
|
||||
|
@ -70,7 +73,6 @@ public:
|
|||
virtual ~MemoryDataSource();
|
||||
|
||||
virtual void Print(BString& text, int64 value) const;
|
||||
virtual const char* Unit() const;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2018, Haiku Inc. All rights reserved.
|
||||
* Copyright 2012-2024, Haiku Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
@ -7,9 +7,12 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <Catalog.h>
|
||||
#include <NumberFormat.h>
|
||||
#include <StringFormat.h>
|
||||
#include <SystemCatalog.h>
|
||||
|
||||
|
||||
using BPrivate::gSystemCatalog;
|
||||
|
||||
|
||||
|
@ -23,44 +26,43 @@ namespace BPrivate {
|
|||
const char*
|
||||
string_for_rate(double rate, char* string, size_t stringSize)
|
||||
{
|
||||
double kib = rate / 1024.0;
|
||||
if (kib < 1.0) {
|
||||
BString tmp;
|
||||
BStringFormat format(
|
||||
gSystemCatalog.GetString(B_TRANSLATE_MARK(
|
||||
"{0, plural, one{# byte/s} other{# bytes/s}}"),
|
||||
B_TRANSLATION_CONTEXT, "bytes per second"));
|
||||
format.Format(tmp, (int)rate);
|
||||
BString printedRate;
|
||||
|
||||
double value = rate / 1024.0;
|
||||
if (value < 1.0) {
|
||||
BStringFormat format(
|
||||
B_TRANSLATE_MARK_ALL("{0, plural, one{# byte/s} other{# bytes/s}}",
|
||||
B_TRANSLATION_CONTEXT, "bytes per second"));
|
||||
|
||||
format.Format(printedRate, (int)rate);
|
||||
strlcpy(string, gSystemCatalog.GetString(printedRate.String(), B_TRANSLATION_CONTEXT,
|
||||
"bytes per second"), stringSize);
|
||||
|
||||
strlcpy(string, tmp.String(), stringSize);
|
||||
return string;
|
||||
}
|
||||
double mib = kib / 1024.0;
|
||||
if (mib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB/s");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT, "KiB per second"), kib);
|
||||
return string;
|
||||
|
||||
const char* kFormats[] = {
|
||||
B_TRANSLATE_MARK_ALL("%s KiB/s", B_TRANSLATION_CONTEXT, "units per second"),
|
||||
B_TRANSLATE_MARK_ALL("%s MiB/s", B_TRANSLATION_CONTEXT, "units per second"),
|
||||
B_TRANSLATE_MARK_ALL("%s GiB/s", B_TRANSLATION_CONTEXT, "units per second"),
|
||||
B_TRANSLATE_MARK_ALL("%s TiB/s", B_TRANSLATION_CONTEXT, "units per second")
|
||||
};
|
||||
|
||||
size_t index = 0;
|
||||
while (index < B_COUNT_OF(kFormats) && value >= 1024.0) {
|
||||
value /= 1024.0;
|
||||
index++;
|
||||
}
|
||||
double gib = mib / 1024.0;
|
||||
if (gib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB/s");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT, "MiB per second"), mib);
|
||||
return string;
|
||||
}
|
||||
double tib = gib / 1024.0;
|
||||
if (tib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f GiB/s");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT, "GiB per second"), gib);
|
||||
return string;
|
||||
}
|
||||
const char* trKey = B_TRANSLATE_MARK("%.2f TiB/s");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT, "TiB per second"), tib);
|
||||
|
||||
BNumberFormat numberFormat;
|
||||
numberFormat.SetPrecision(2);
|
||||
numberFormat.Format(printedRate, value);
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(kFormats[index], B_TRANSLATION_CONTEXT,
|
||||
"units per second"), printedRate.String());
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2010-2019 Haiku Inc. All rights reserved.
|
||||
* Copyright 2010-2024 Haiku Inc. All rights reserved.
|
||||
* Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
@ -10,9 +10,11 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <NumberFormat.h>
|
||||
#include <StringFormat.h>
|
||||
#include <SystemCatalog.h>
|
||||
|
||||
|
||||
using BPrivate::gSystemCatalog;
|
||||
|
||||
|
||||
|
@ -26,43 +28,40 @@ namespace BPrivate {
|
|||
const char*
|
||||
string_for_size(double size, char* string, size_t stringSize)
|
||||
{
|
||||
double kib = size / 1024.0;
|
||||
if (kib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK(
|
||||
"{0, plural, one{# byte} other{# bytes}}");
|
||||
BString printedSize;
|
||||
|
||||
BString tmp;
|
||||
double value = size / 1024.0;
|
||||
if (value < 1024.0) {
|
||||
BStringFormat format(
|
||||
gSystemCatalog.GetString(trKey, B_TRANSLATION_CONTEXT));
|
||||
format.Format(tmp, (int)size);
|
||||
B_TRANSLATE_MARK_ALL("{0, plural, one{# byte} other{# bytes}}",
|
||||
B_TRANSLATION_CONTEXT, "size unit"));
|
||||
|
||||
format.Format(printedSize, (int)size);
|
||||
strlcpy(string, gSystemCatalog.GetString(printedSize.String(), B_TRANSLATION_CONTEXT,
|
||||
"size unit"), stringSize);
|
||||
|
||||
strlcpy(string, tmp.String(), stringSize);
|
||||
return string;
|
||||
}
|
||||
double mib = kib / 1024.0;
|
||||
if (mib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT), kib);
|
||||
return string;
|
||||
|
||||
const char* kFormats[] = {
|
||||
B_TRANSLATE_MARK_ALL("%s KiB", B_TRANSLATION_CONTEXT, "size unit"),
|
||||
B_TRANSLATE_MARK_ALL("%s MiB", B_TRANSLATION_CONTEXT, "size unit"),
|
||||
B_TRANSLATE_MARK_ALL("%s GiB", B_TRANSLATION_CONTEXT, "size unit"),
|
||||
B_TRANSLATE_MARK_ALL("%s TiB", B_TRANSLATION_CONTEXT, "size unit")
|
||||
};
|
||||
|
||||
size_t index = 0;
|
||||
while (index < B_COUNT_OF(kFormats) && value >= 1024.0) {
|
||||
value /= 1024.0;
|
||||
index++;
|
||||
}
|
||||
double gib = mib / 1024.0;
|
||||
if (gib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT), mib);
|
||||
return string;
|
||||
}
|
||||
double tib = gib / 1024.0;
|
||||
if (tib < 1.0) {
|
||||
const char* trKey = B_TRANSLATE_MARK("%3.2f GiB");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT), gib);
|
||||
return string;
|
||||
}
|
||||
const char* trKey = B_TRANSLATE_MARK("%.2f TiB");
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
||||
B_TRANSLATION_CONTEXT), tib);
|
||||
|
||||
BNumberFormat numberFormat;
|
||||
numberFormat.SetPrecision(2);
|
||||
numberFormat.Format(printedSize, value);
|
||||
snprintf(string, stringSize, gSystemCatalog.GetString(kFormats[index], B_TRANSLATION_CONTEXT,
|
||||
"size unit"), printedSize.String());
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue