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:
Emir SARI 2024-02-28 01:06:15 +03:00 committed by Adrien Destugues
parent 1ee3891ab8
commit 1f5daef0bf
4 changed files with 86 additions and 90 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de. * 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. * Distributed under the terms of the MIT License.
*/ */
@ -13,6 +14,7 @@
#include <OS.h> #include <OS.h>
#include <String.h> #include <String.h>
#include <StringForRate.h> #include <StringForRate.h>
#include <StringForSize.h>
#include "SystemInfo.h" #include "SystemInfo.h"
@ -146,7 +148,7 @@ void
DataSource::Print(BString& text, int64 value) const DataSource::Print(BString& text, int64 value) const
{ {
text = ""; text = "";
text << value; fNumberFormat.Format(text, (int32)value);
} }
@ -292,19 +294,11 @@ void
MemoryDataSource::Print(BString& text, int64 value) const MemoryDataSource::Print(BString& text, int64 value) const
{ {
char buffer[32]; char buffer[32];
snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f MiB"), value / 1048576.0); string_for_size(value, buffer, sizeof(buffer));
text = buffer; text = buffer;
} }
const char*
MemoryDataSource::Unit() const
{
return B_TRANSLATE("MiB");
}
// #pragma mark - // #pragma mark -
@ -856,7 +850,9 @@ CPUFrequencyDataSource::CopyForCPU(int32 cpu) const
void void
CPUFrequencyDataSource::Print(BString& text, int64 value) const 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 void
CPUUsageDataSource::Print(BString& text, int64 value) const CPUUsageDataSource::Print(BString& text, int64 value) const
{ {
char buffer[32]; text = "";
snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0); fNumberFormat.SetPrecision(1);
fNumberFormat.FormatPercent(text, value / 1000.0);
text = buffer;
} }
@ -1147,10 +1142,9 @@ CPUCombinedUsageDataSource::Copy() const
void void
CPUCombinedUsageDataSource::Print(BString& text, int64 value) const CPUCombinedUsageDataSource::Print(BString& text, int64 value) const
{ {
char buffer[32]; text = "";
snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0); fNumberFormat.SetPrecision(1);
fNumberFormat.FormatPercent(text, value / 1000.0);
text = buffer;
} }
@ -1263,11 +1257,10 @@ PageFaultsDataSource::Copy() const
void void
PageFaultsDataSource::Print(BString& text, int64 value) const PageFaultsDataSource::Print(BString& text, int64 value) const
{ {
char buffer[32]; BString printedPageFaults;
snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f faults/s"), fNumberFormat.SetPrecision(1);
value / 1024.0); fNumberFormat.Format(printedPageFaults, value / 1024.0);
text.SetToFormat(B_TRANSLATE("%s faults/s"), printedPageFaults.String());
text = buffer;
} }

View File

@ -7,6 +7,7 @@
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <NumberFormat.h>
#include <String.h> #include <String.h>
class SystemInfo; class SystemInfo;
@ -61,6 +62,8 @@ protected:
int64 fMaximum; int64 fMaximum;
bigtime_t fInterval; bigtime_t fInterval;
rgb_color fColor; rgb_color fColor;
mutable BNumberFormat fNumberFormat;
}; };
@ -70,7 +73,6 @@ public:
virtual ~MemoryDataSource(); virtual ~MemoryDataSource();
virtual void Print(BString& text, int64 value) const; virtual void Print(BString& text, int64 value) const;
virtual const char* Unit() const;
}; };

View File

@ -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. * Distributed under the terms of the MIT License.
*/ */
@ -7,9 +7,12 @@
#include <stdio.h> #include <stdio.h>
#include <Catalog.h>
#include <NumberFormat.h>
#include <StringFormat.h> #include <StringFormat.h>
#include <SystemCatalog.h> #include <SystemCatalog.h>
using BPrivate::gSystemCatalog; using BPrivate::gSystemCatalog;
@ -23,44 +26,43 @@ namespace BPrivate {
const char* const char*
string_for_rate(double rate, char* string, size_t stringSize) string_for_rate(double rate, char* string, size_t stringSize)
{ {
double kib = rate / 1024.0; BString printedRate;
if (kib < 1.0) {
BString tmp; double value = rate / 1024.0;
BStringFormat format( if (value < 1.0) {
gSystemCatalog.GetString(B_TRANSLATE_MARK( BStringFormat format(
"{0, plural, one{# byte/s} other{# bytes/s}}"), B_TRANSLATE_MARK_ALL("{0, plural, one{# byte/s} other{# bytes/s}}",
B_TRANSLATION_CONTEXT, "bytes per second")); B_TRANSLATION_CONTEXT, "bytes per second"));
format.Format(tmp, (int)rate);
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; return string;
} }
double mib = kib / 1024.0;
if (mib < 1.0) { const char* kFormats[] = {
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB/s"); B_TRANSLATE_MARK_ALL("%s KiB/s", B_TRANSLATION_CONTEXT, "units per second"),
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, B_TRANSLATE_MARK_ALL("%s MiB/s", B_TRANSLATION_CONTEXT, "units per second"),
B_TRANSLATION_CONTEXT, "KiB per second"), kib); B_TRANSLATE_MARK_ALL("%s GiB/s", B_TRANSLATION_CONTEXT, "units per second"),
return string; 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) { BNumberFormat numberFormat;
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB/s"); numberFormat.SetPrecision(2);
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, numberFormat.Format(printedRate, value);
B_TRANSLATION_CONTEXT, "MiB per second"), mib); snprintf(string, stringSize, gSystemCatalog.GetString(kFormats[index], B_TRANSLATION_CONTEXT,
return string; "units per second"), printedRate.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);
return string; return string;
} }
} // namespace BPrivate } // namespace BPrivate

View File

@ -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. * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@ -10,9 +10,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <NumberFormat.h>
#include <StringFormat.h> #include <StringFormat.h>
#include <SystemCatalog.h> #include <SystemCatalog.h>
using BPrivate::gSystemCatalog; using BPrivate::gSystemCatalog;
@ -26,43 +28,40 @@ namespace BPrivate {
const char* const char*
string_for_size(double size, char* string, size_t stringSize) string_for_size(double size, char* string, size_t stringSize)
{ {
double kib = size / 1024.0; BString printedSize;
if (kib < 1.0) {
const char* trKey = B_TRANSLATE_MARK(
"{0, plural, one{# byte} other{# bytes}}");
BString tmp; double value = size / 1024.0;
if (value < 1024.0) {
BStringFormat format( BStringFormat format(
gSystemCatalog.GetString(trKey, B_TRANSLATION_CONTEXT)); B_TRANSLATE_MARK_ALL("{0, plural, one{# byte} other{# bytes}}",
format.Format(tmp, (int)size); 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; return string;
} }
double mib = kib / 1024.0;
if (mib < 1.0) { const char* kFormats[] = {
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB"); B_TRANSLATE_MARK_ALL("%s KiB", B_TRANSLATION_CONTEXT, "size unit"),
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, B_TRANSLATE_MARK_ALL("%s MiB", B_TRANSLATION_CONTEXT, "size unit"),
B_TRANSLATION_CONTEXT), kib); B_TRANSLATE_MARK_ALL("%s GiB", B_TRANSLATION_CONTEXT, "size unit"),
return string; 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) { BNumberFormat numberFormat;
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB"); numberFormat.SetPrecision(2);
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, numberFormat.Format(printedSize, value);
B_TRANSLATION_CONTEXT), mib); snprintf(string, stringSize, gSystemCatalog.GetString(kFormats[index], B_TRANSLATION_CONTEXT,
return string; "size unit"), printedSize.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);
return string; return string;
} }